Class: Depot::Base
- Inherits:
-
Object
- Object
- Depot::Base
- Defined in:
- lib/depot/base.rb
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#logging(value = true) ⇒ Object
Returns the value of attribute logging.
-
#models ⇒ Object
readonly
Returns the value of attribute models.
Instance Method Summary collapse
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #inject(*args) ⇒ Object
- #method_missing(name, &block) ⇒ Object
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/depot/base.rb', line 6 def initialize @entries = {} @logging = false @models = Set.new if defined?(ActiveRecord) subclasses = ActiveRecord::Base.send(:subclasses).map do |m| m.name.downcase.pluralize.gsub(/::/, "_").to_sym end @models.merge(subclasses) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, &block) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/depot/base.rb', line 32 def method_missing(name, &block) if @models.include? name and block_given? klass = name.to_s.gsub("_", "::").singularize.classify.constantize Context.new(klass, self).instance_eval(&block) elsif entries.has_key? name @entries[name] else super end end |
Instance Attribute Details
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
3 4 5 |
# File 'lib/depot/base.rb', line 3 def entries @entries end |
#logging(value = true) ⇒ Object
Returns the value of attribute logging.
4 5 6 |
# File 'lib/depot/base.rb', line 4 def logging @logging end |
#models ⇒ Object (readonly)
Returns the value of attribute models.
3 4 5 |
# File 'lib/depot/base.rb', line 3 def models @models end |
Instance Method Details
#inject(*args) ⇒ Object
28 29 30 |
# File 'lib/depot/base.rb', line 28 def inject(*args) @models.merge args.map(&:to_sym) end |