Module Wrest::Components::Container::ClassMethods

  1. lib/wrest/components/container.rb


public instance

  1. always_has
  2. element_name
  3. typecast

Public instance methods

always_has (*attribute_names)

This macro explicitly creates getter, setter and query methods on an Container, overriding any exisiting 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.

[show source]
    # File lib/wrest/components/container.rb, line 88
88:       def always_has(*attribute_names)
89:         attribute_names.each do |attribute_name|
90:           self.class_eval(
91:           Container.build_attribute_getter(attribute_name) +
92:           Container.build_attribute_setter(attribute_name) +
93:           Container.build_attribute_queryer(attribute_name)
94:           )
95:         end
96:       end
element_name ()

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.

[show source]
     # File lib/wrest/components/container.rb, line 118
118:       def element_name
119:         @element_name ||= ActiveSupport::Inflector.demodulize(
120:       end
typecast (cast_map)

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.

[show source]
     # File lib/wrest/components/container.rb, line 106
106:       def typecast(cast_map)
107:         self.class_eval{ include Wrest::Components::Container::Typecaster }
108:         self.typecast cast_map
109:       end