Module: DataMapper::Enum::ClassMethods

Defined in:
lib/dm/enum.rb

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dm/enum.rb', line 23

def [](name)
  unless @cache_update_flag
    raise RuntimeError.new("Looks like you forgot to call `#{self}.update_enums_cache!` before using `#{self}[#{name.inspect}]`")
  end

  case name
  when String, Symbol
    @enums_cache[name.to_sym]
  when self
    name
  else
    nil
  end
end

#acts_as_enumerated(options = {}) ⇒ Object



9
10
11
12
# File 'lib/dm/enum.rb', line 9

def acts_as_enumerated(options = {})
  @name_property = options.delete(:name_property) || :name
  @enums_cache = {}
end

#update_enums_cache!Object



14
15
16
17
18
19
20
21
# File 'lib/dm/enum.rb', line 14

def update_enums_cache!
  @enums_cache = {}
  @cache_update_flag = true
  all.each do |model|
    name = model.send(@name_property).to_sym
    @enums_cache[name] = model
  end
end