Module: Rails::Initializable::ClassMethods

Defined in:
lib/rails/initializable.rb

Instance Method Summary collapse

Instance Method Details

#initializer(name, opts = {}, &blk) ⇒ Object

Raises:

  • (ArgumentError)

77
78
79
80
81
# File 'lib/rails/initializable.rb', line 77

def initializer(name, opts = {}, &blk)
  raise ArgumentError, "A block must be passed when defining an initializer" unless blk
  opts[:after] ||= initializers.last.name unless initializers.empty? || initializers.find { |i| i.name == opts[:before] }
  initializers << Initializer.new(name, nil, opts, &blk)
end

#initializersObject


60
61
62
# File 'lib/rails/initializable.rb', line 60

def initializers
  @initializers ||= Collection.new
end

#initializers_chainObject


64
65
66
67
68
69
70
71
# File 'lib/rails/initializable.rb', line 64

def initializers_chain
  initializers = Collection.new
  ancestors.reverse_each do |klass|
    next unless klass.respond_to?(:initializers)
    initializers = initializers + klass.initializers
  end
  initializers
end

#initializers_for(binding) ⇒ Object


73
74
75
# File 'lib/rails/initializable.rb', line 73

def initializers_for(binding)
  Collection.new(initializers_chain.map { |i| i.bind(binding) })
end