Module: Wrest::Components::AttributesContainer::InstanceMethods

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments) ⇒ Object

Creates getter, setter and query methods for attributes on the first call.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/wrest/components/attributes_container.rb', line 119

def method_missing(method_sym, *arguments)
  method_name = method_sym.to_s
  attribute_name = method_name.gsub(/(\?$)|(=$)/, '')
  if @attributes.include?(attribute_name.to_sym) || method_name.last == '='
    case method_name.last
    when '='
      self.instance_eval AttributesContainer.build_attribute_setter(attribute_name)
    when '?'
      self.instance_eval AttributesContainer.build_attribute_queryer(attribute_name)
    else
      self.instance_eval AttributesContainer.build_attribute_getter(attribute_name)
    end
    send(method_sym, *arguments)
  else
    super(method_sym, *arguments)
  end
end

Instance Method Details

#[](key) ⇒ Object



105
106
107
# File 'lib/wrest/components/attributes_container.rb', line 105

def [](key)
  @attributes[key.to_sym]
end

#[]=(key, value) ⇒ Object



109
110
111
# File 'lib/wrest/components/attributes_container.rb', line 109

def []=(key, value)
  @attributes[key.to_sym] = value
end

#initialize(attributes = {}) ⇒ Object

Sets up any class to act like an attributes container by creating two variables, @attributes and @interface. Remember not to use these two variable names when using AttributesContainer in your own class.



101
102
103
# File 'lib/wrest/components/attributes_container.rb', line 101

def initialize(attributes = {})
  @attributes = attributes.symbolize_keys
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/wrest/components/attributes_container.rb', line 113

def respond_to?(method_name, include_private = false)
  super(method_name, include_private) ? true : @attributes.include?(method_name.to_s.gsub(/(\?$)|(=$)/, '').to_sym)
end