Class: SimpleMapper::Attributes::Manager

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(apply_to = nil) ⇒ Manager

Returns a new instance of Manager.



157
158
159
# File 'lib/simple_mapper/attributes.rb', line 157

def initialize(apply_to = nil)
  self.applies_to = apply_to if apply_to
end

Instance Attribute Details

#applies_toObject

Returns the value of attribute applies_to.



155
156
157
# File 'lib/simple_mapper/attributes.rb', line 155

def applies_to
  @applies_to
end

Instance Method Details

#attributesObject



161
162
163
# File 'lib/simple_mapper/attributes.rb', line 161

def attributes
  @attributes ||= {}
end

#create_anonymous_mapper(&block) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'lib/simple_mapper/attributes.rb', line 180

def create_anonymous_mapper(&block)
  mapper = Class.new do
    include SimpleMapper::Attributes
    def self.decode(*arg)
      new(*arg)
    end
  end
  mapper.module_eval &block if block_given?
  mapper
end

#create_attribute(name, options = {}) ⇒ Object



165
166
167
168
# File 'lib/simple_mapper/attributes.rb', line 165

def create_attribute(name, options = {})
  attrib_class = options[:attribute_class] || SimpleMapper::Attribute
  attrib_class.new(name, options)
end

#install_attribute(attr, object) ⇒ Object



170
171
172
173
174
175
176
177
178
# File 'lib/simple_mapper/attributes.rb', line 170

def install_attribute(attr, object)
  read_body = Proc.new { read_attribute(attr) }
  write_body = Proc.new {|value| write_attribute(attr, value)}
  applies_to.module_eval do
    define_method(attr, &read_body)
    define_method(:"#{attr}=", &write_body)
  end
  attributes[attr] = object
end