Module: Wrest::Components::Container::ClassMethods

Defined in:
lib/wrest/components/container.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) 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.



88
89
90
91
92
93
94
95
96
# File 'lib/wrest/components/container.rb', line 88

def always_has(*attribute_names)
  attribute_names.each do |attribute_name|
    self.class_eval(
    Container.build_attribute_getter(attribute_name) +
    Container.build_attribute_setter(attribute_name) +
    Container.build_attribute_queryer(attribute_name)
    )
  end
end

- (Object) 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.



118
119
120
# File 'lib/wrest/components/container.rb', line 118

def element_name
  @element_name ||= ActiveSupport::Inflector.demodulize(self.name).underscore.underscore
end

- (Object) 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.



106
107
108
109
# File 'lib/wrest/components/container.rb', line 106

def typecast(cast_map)
  self.class_eval{ include Wrest::Components::Container::Typecaster }
  self.typecast cast_map
end