Module Wrest::Components::Container

  1. lib/wrest/components/container/alias_accessors.rb
  2. lib/wrest/components/container/typecaster.rb
  3. lib/wrest/components/container.rb
  4. show all

Adds behaviour allowing a class to contain attributes and providing support for dynamic getters, setters and query methods. These methods are added at runtime, on the first invocation and on a per instance basis. respond_to? however will respond as though they are all already present. This means that two different instances of the same Container could well have different attribute getters/setters/query methods.

Note that the first call to a particular getter/setter/query method will be slower because the method is defined at that point; subsequent calls will be much faster.

Also keep in mind that attribute getter/setter/query methods will not override any existing methods on the class.

In situations where this is a problem, such as a client consuming Rails REST services where id is a common attribute and clashes with Object#id, it is recommended to create getter/setter/query methods on the class (which affects all instances) using the always_has macro.

If you’re implementing your own initialize method remember to delegate to the default initialize of Container by invoking super(attributes)


class ShenCoin
  include Wrest::Components::Container
  include Wrest::Components::Container::Typecaster

  always_has   :id
  typecast         :id   =>  as_integer
coin = => '5', :chi_count => 500, :owner => 'Kai Wren')    # => 5
coin.owner # => 'Kai Wren'