Module: Wrest::Components::Container::InstanceMethods
- Defined in:
- lib/wrest/components/container.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(attributes = {}) ⇒ Object
Sets up any class to act like an attributes container by creating two variables, @attributes and @interface.
-
#method_missing(method_sym, *arguments) ⇒ Object
Creates getter, setter and query methods for attributes on the first call.
- #respond_to?(method_name, include_private = false) ⇒ Boolean
-
#serialise_using(translator, options = {}) ⇒ Object
A translator is a anything that knows how to serialise a Hash.
- #to_xml(options = {}) ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_sym, *arguments) ⇒ Object
Creates getter, setter and query methods for attributes on the first call.
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/wrest/components/container.rb', line 166 def method_missing(method_sym, *arguments) method_name = method_sym.to_s attribute_name = method_name.gsub(/(\?$)|(=$)/, '') if @attributes.include?(attribute_name.to_sym) || method_name.last == '=' || method_name.last == '?' case method_name.last when '=' self.instance_eval Container.build_attribute_setter(attribute_name) when '?' self.instance_eval Container.build_attribute_queryer(attribute_name) else self.instance_eval Container.build_attribute_getter(attribute_name) end send(method_sym, *arguments) else super(method_sym, *arguments) end end |
Instance Method Details
#[](key) ⇒ Object
152 153 154 |
# File 'lib/wrest/components/container.rb', line 152 def [](key) @attributes[key.to_sym] end |
#[]=(key, value) ⇒ Object
156 157 158 |
# File 'lib/wrest/components/container.rb', line 156 def []=(key, value) @attributes[key.to_sym] = value end |
#initialize(attributes = {}) ⇒ Object
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.
129 130 131 |
# File 'lib/wrest/components/container.rb', line 129 def initialize(attributes = {}) @attributes = HashWithIndifferentAccess.new(attributes) end |
#respond_to?(method_name, include_private = false) ⇒ Boolean
160 161 162 |
# File 'lib/wrest/components/container.rb', line 160 def respond_to?(method_name, include_private = false) super(method_name, include_private) ? true : @attributes.include?(method_name.to_s.gsub(/(\?$)|(=$)/, '').to_sym) end |
#serialise_using(translator, options = {}) ⇒ Object
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.
144 145 146 |
# File 'lib/wrest/components/container.rb', line 144 def serialise_using(translator, = {}) translator.serialise(@attributes, ) end |
#to_xml(options = {}) ⇒ Object
148 149 150 |
# File 'lib/wrest/components/container.rb', line 148 def to_xml( = {}) serialise_using(Wrest::Components::Translators::Xml, {:root => self.class.element_name}.merge()) end |