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

  1. lib/wrest/components/container/alias_accessors.rb


public instance

  1. alias_accessors

Public instance methods

alias_accessors (alias_map)

Creates an alias set of getter, setter and query methods for attributes that aren’t quite the way you’d like them to be; this is especially useful when you have no control over the source web sevice/resource.

For example, lets say that a particular resource exposes a User’s age as ‘a’ and sex as ’s’. Typically, you’d have to access it as user.a and user.s whereas you’s like to access it as user.age and This is where alias_accessors comes into the picture. Your User class would look somethig like this:

class User
  include Wrest::Components::Container

  alias_accessors  :a => :age,
                   :s => :sex

This would create the methods user.age, user.age= and user.age? which delegates to user.a, user.a= and user.a? respectively.

See examples/wow_realm_status.rb for a working example.

WARNING: If you try to create an alias with the same name as the attribute, and then use it, you will cause an infinite loop.

[show source]
    # File lib/wrest/components/container/alias_accessors.rb, line 54
54:         def alias_accessors(alias_map)
55:           alias_map.each do |attribute_name, alias_name|
56:             self.class_eval(
57:             AliasAccessors.build_aliased_attribute_getter(attribute_name, alias_name) +
58:             AliasAccessors.build_aliased_attribute_setter(attribute_name, alias_name) +
59:             AliasAccessors.build_aliased_attribute_queryer(attribute_name, alias_name)
60:             )
61:           end
62:         end