Class: CurlyMustache::Attributes::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/curly_mustache/attributes/manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



9
10
11
# File 'lib/curly_mustache/attributes/manager.rb', line 9

def initialize
  @definitions = {}
end

Instance Attribute Details

#definitionsObject (readonly)

Returns the value of attribute definitions.



7
8
9
# File 'lib/curly_mustache/attributes/manager.rb', line 7

def definitions
  @definitions
end

Instance Method Details

#[](name) ⇒ Object



27
28
29
30
31
# File 'lib/curly_mustache/attributes/manager.rb', line 27

def [](name)
  name = name.to_s
  raise AttributeNotDefinedError, "#{name} is not defined" unless @definitions.has_key?(name)
  @definitions[name]
end

#define(klass, name, type, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/curly_mustache/attributes/manager.rb', line 13

def define(klass, name, type, options = {})
  @definitions[name.to_s] = Definition.new(name, type, options)
  
  klass.class_eval <<-eval
    def #{name}; read_attribute(:#{name}); end
    def #{name}=(value); write_attribute(:#{name}, value); end
  eval
  
  # This is so ghetto, but these are the hoops we have to jump through
  # to get ActiveModel::Dirty working with inheritance.
  klass.undefine_attribute_methods
  klass.define_attribute_methods(@definitions.keys.collect(&:to_sym))
end

#dupObject



33
34
35
36
37
# File 'lib/curly_mustache/attributes/manager.rb', line 33

def dup
  returning(self.class.new) do |new_manager|
    new_manager.instance_variable_set("@definitions", @definitions.dup)
  end
end