class Grouik::Loader

Main class loader

loads files during “Grouik::Loader#load_all“

Attributes

attempts[R]
basedir[RW]
errors[R]
ignores[RW]
paths[R]
stats[R]

Public Class Methods

new() { |self| ... } click to toggle source
# File src/lib/grouik/loader.rb, line 29
def initialize
  self.basedir = '.'
  self.ignores = []

  @loadeds   = []
  @errors    = {}
  @loadables = []
  @attempts  = 0
  @stats     = nil

  return self unless block_given?

  yield self
  register
end

Public Instance Methods

attempts_maxcount() click to toggle source

@return [Fixnum]

# File src/lib/grouik/loader.rb, line 122
def attempts_maxcount
  (self.loadables.size**2) + 1
end
format(options = {}) click to toggle source

Format using a formatter

@param [Hash] options @return [String]

# File src/lib/grouik/loader.rb, line 113
def format(options = {})
  Grouik.get(:formatter).format(load_all.loadables, options).to_s
end
ignores=(ignores) click to toggle source

@param [Array<String|Regexp>] ignores

# File src/lib/grouik/loader.rb, line 46
def ignores=(ignores)
  @ignores = helpers.get(:loader).make_ignores(ignores)
end
load_all() click to toggle source

@return [self]

# File src/lib/grouik/loader.rb, line 92
def load_all
  return @loadeds.clone unless @loadeds.empty?

  loadables = self.loadables
  @loadeds  = []
  @errors   = {}

  @stats = Benchmark.measure do
    loop do
      loadables = process_loadables(loadables)
      break if loadables.nil? or (loadables and loadables.empty?)
    end
  end
  @loadeds.compact
  self
end
loadables() click to toggle source

Get loadables

@return [Array<Grouik::Loadable>]

# File src/lib/grouik/loader.rb, line 72
def loadables
  if @loadables.empty?
    self.basedir do
      @loadables = helpers.get(:loader)
                          .collect_loadables(paths)
                          .ignores(ignores)
    end
  end

  @loadables.clone
end
loaded?() click to toggle source
# File src/lib/grouik/loader.rb, line 117
def loaded?
  self.loadables.size == @loadeds.size
end
paths=(paths) click to toggle source

@param [Array<String>] paths

# File src/lib/grouik/loader.rb, line 51
def paths=(paths)
  @paths = paths.to_a.map { |path| Pathname.new(path.to_s) }
end
register() click to toggle source

Register paths

@return [self]

# File src/lib/grouik/loader.rb, line 63
def register
  helpers.get(:loader).register_paths(basedir, @paths)

  self
end

Protected Instance Methods

process_loadables(processables) click to toggle source
# File src/lib/grouik/loader.rb, line 128
def process_loadables(processables)
  processables.each_with_index do |loadable, index|
    return [] if attempts >= attempts_maxcount or processables.empty?

    @attempts += 1
    loaded = nil
    begin
      loaded = loadable.load(helpers.get(:loader).pwd.join(basedir))
    rescue StandardError => e
      unless @errors[loadable.to_s]
        @errors[loadable.to_s] = OpenStruct.new(
          source: loadable.to_s,
          message: e.message.lines[0].strip.freeze,
          line: e.backtrace[0].split(':')[1],
          error: e
        ).freeze
      end
      next
    end

    next if loaded.nil?

    @loadeds.push(processables.delete_at(index))
    # when loadable is loaded, then error is removed
    @errors.delete(loadable.to_s)
  end
  processables
end