Module: Wrest::Mappers::AttributesContainer

Included in:
Resource::Base, SimpleResource
Defined in:
lib/wrest/mappers/attributes_container.rb

Overview

Adds behaviour allowing a class to contain attributes and providing support for dynamic getters, setters and query methods. These methods are added at runtime, on the first invocation and on a per instance basis.
respond_to? however will respond as though they are all already present. This means that two different instances of the same AttributesContainer could well have different attribute getters/setters/query methods.

Note that this means the first call to a particular method will be slower because the method is defined at that point; subsequent calls will be much faster.

If you're implementing your own initialize method remember to delegate to the default initialize of AttributesContainer by invoking super(attributes) Also keep in mind that attribute getter/setter/query methods will not override any existing methods on the class.

In situations where this is a problem, such as a client consuming Rails REST services where id is a common attribute and clashes with Object#id it is recommended to create getter/setter/query methods on the class (which affects all instances) using the has_attributes macro.

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Class Method Summary collapse

Class Method Details

.build_attribute_getter(attribute_name) ⇒ Object

:nodoc:



43
44
45
# File 'lib/wrest/mappers/attributes_container.rb', line 43

def self.build_attribute_getter(attribute_name) #:nodoc:
  "def #{attribute_name};@attributes[:#{attribute_name}];end;"
end

.build_attribute_queryer(attribute_name) ⇒ Object

:nodoc:



51
52
53
# File 'lib/wrest/mappers/attributes_container.rb', line 51

def self.build_attribute_queryer(attribute_name) #:nodoc:
  "def #{attribute_name}?;not @attributes[:#{attribute_name}].nil?;end;"
end

.build_attribute_setter(attribute_name) ⇒ Object

:nodoc:



47
48
49
# File 'lib/wrest/mappers/attributes_container.rb', line 47

def self.build_attribute_setter(attribute_name) #:nodoc:
  "def #{attribute_name}=(value);@attributes[:#{attribute_name}] = value;end;"
end

.included(klass) ⇒ Object

:nodoc:



38
39
40
41
# File 'lib/wrest/mappers/attributes_container.rb', line 38

def self.included(klass) #:nodoc:
  klass.extend AttributesContainer::ClassMethods
  klass.class_eval{ include AttributesContainer::InstanceMethods }
end