Module: Wrest::Components::Container::ClassMethods
- Defined in:
- lib/wrest/components/container.rb
Instance Method Summary collapse
-
#always_has(*attribute_names) ⇒ Object
This macro explicitly creates getter, setter and query methods on an Container, overriding any existing methods with the same names.
-
#element_name ⇒ Object
This is the name of the class in snake-case, with any parent module names removed.
-
#typecast(cast_map) ⇒ Object
This is a convenience macro which includes Wrest::Components::Container::Typecaster into the class (effectively overwriting this method) before delegating to the actual typecast method that is a part of that module.
Instance Method Details
#always_has(*attribute_names) ⇒ Object
This macro explicitly creates getter, setter and query methods on an Container, overriding any existing methods with the same names. This can be used when attribute names clash with existing method names; an example would be Rails REST resources which frequently make use an attribute named id
which clashes with Object#id. Also, this can be used as a performance optimisation if the incoming attributes are known beforehand.
92 93 94 95 96 97 98 99 100 |
# File 'lib/wrest/components/container.rb', line 92 def always_has(*attribute_names) attribute_names.each do |attribute_name| class_eval( Container.build_attribute_getter(attribute_name) + Container.build_attribute_setter(attribute_name) + Container.build_attribute_queryer(attribute_name) ) end end |
#element_name ⇒ Object
This is the name of the class in snake-case, with any parent module names removed.
The class will use as the root element when serialised to xml after replacing underscores with hyphens.
This method can be overidden should you need a different name.
122 123 124 |
# File 'lib/wrest/components/container.rb', line 122 def element_name @element_name ||= Utils.string_underscore(Utils.string_demodulize(name)) end |
#typecast(cast_map) ⇒ Object
This is a convenience macro which includes Wrest::Components::Container::Typecaster into the class (effectively overwriting this method) before delegating to the actual typecast method that is a part of that module. This saves us the effort of explicitly doing the include. Easy to use API is king.
Remember that using typecast carries a performance penalty. See Wrest::Components::Container::Typecaster for the actual docs.
110 111 112 113 |
# File 'lib/wrest/components/container.rb', line 110 def typecast(cast_map) class_eval { include Wrest::Components::Container::Typecaster } typecast cast_map end |