Module: Wrest::Components::Container::AliasAccessors::ClassMethods
- Defined in:
- lib/wrest/components/container/alias_accessors.rb
Instance Method Summary collapse
-
#alias_accessors(alias_map) ⇒ Object
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.
Instance Method Details
#alias_accessors(alias_map) ⇒ Object
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 user.sex. 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
end
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.
57 58 59 60 61 62 63 64 65 |
# File 'lib/wrest/components/container/alias_accessors.rb', line 57 def alias_accessors(alias_map) alias_map.each do |attribute_name, alias_name| class_eval( AliasAccessors.build_aliased_attribute_getter(attribute_name, alias_name) + AliasAccessors.build_aliased_attribute_setter(attribute_name, alias_name) + AliasAccessors.build_aliased_attribute_queryer(attribute_name, alias_name) ) end end |