Class: Depot::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/depot/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

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

#entriesObject (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

#modelsObject (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