Module Wrest::Components::Container::InstanceMethods

  1. lib/wrest/components/container.rb

Methods

public class

  1. new

public instance

  1. []
  2. []=
  3. method_missing
  4. respond_to?
  5. serialise_using
  6. to_xml

Public class methods

new (attributes = {})

Sets up any class to act like an attributes container by creating two variables, @attributes and @interface. Remember not to use these two variable names when using Container in your own class.

[show source]
     # File lib/wrest/components/container.rb, line 129
129:       def initialize(attributes = {})
130:         @attributes = HashWithIndifferentAccess.new(attributes)
131:       end

Public instance methods

[] (key)
[show source]
     # File lib/wrest/components/container.rb, line 152
152:       def [](key)
153:         @attributes[key.to_sym]
154:       end
[]= (key, value)
[show source]
     # File lib/wrest/components/container.rb, line 156
156:       def []=(key, value)
157:         @attributes[key.to_sym] = value
158:       end
method_missing (method_sym, *arguments)

Creates getter, setter and query methods for attributes on the first call.

[show source]
     # File lib/wrest/components/container.rb, line 166
166:       def method_missing(method_sym, *arguments)
167:         method_name = method_sym.to_s
168:         attribute_name = method_name.gsub(/(\?$)|(=$)/, '')
169:         if @attributes.include?(attribute_name.to_sym) || method_name.last == '=' || method_name.last == '?'
170:           case method_name.last
171:           when '='
172:             self.instance_eval Container.build_attribute_setter(attribute_name)
173:           when '?'
174:             self.instance_eval Container.build_attribute_queryer(attribute_name)
175:           else
176:             self.instance_eval Container.build_attribute_getter(attribute_name)
177:           end
178:           send(method_sym, *arguments)
179:         else
180:           super(method_sym, *arguments)
181:         end
182:       end
respond_to? (method_name, include_private = false)
[show source]
     # File lib/wrest/components/container.rb, line 160
160:       def respond_to?(method_name, include_private = false)
161:         super(method_name, include_private) ? true : @attributes.include?(method_name.to_s.gsub(/(\?$)|(=$)/, '').to_sym)
162:       end
serialise_using (translator, options = {})

A translator is a anything that knows how to serialise a Hash. It must needs have a method named ‘serialise’ that accepts a hash and configuration options, and returns the serialised result (leaving the hash unchanged, of course).

Examples for JSON and XML can be found under Wrest::Components::Translators. These serialised output of these translators will work out of the box for Rails applications; you may need to roll your own for anything else.

Note: When serilising to XML, if you want the name of the class as the name of the root node then you should use the Container#to_xml helper.

[show source]
     # File lib/wrest/components/container.rb, line 144
144:       def serialise_using(translator, options = {})
145:         translator.serialise(@attributes, options)
146:       end
to_xml (options = {})
[show source]
     # File lib/wrest/components/container.rb, line 148
148:       def to_xml(options = {})
149:         serialise_using(Wrest::Components::Translators::Xml, {:root => self.class.element_name}.merge(options))
150:       end