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.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/wrest/components/attributes_container.rb', line 102

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 '='
      @interface.module_eval AttributesContainer.build_attribute_setter(attribute_name)
    when '?'
      @interface.module_eval AttributesContainer.build_attribute_queryer(attribute_name)
    else
      @interface.module_eval AttributesContainer.build_attribute_getter(attribute_name)
    end
    send(method_sym, *arguments)
  else
    super(method_sym, *arguments)
  end
end

Instance Method Details

#[](key) ⇒ Object



88
89
90
# File 'lib/wrest/components/attributes_container.rb', line 88

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

#[]=(key, value) ⇒ Object



92
93
94
# File 'lib/wrest/components/attributes_container.rb', line 92

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.



82
83
84
85
86
# File 'lib/wrest/components/attributes_container.rb', line 82

def initialize(attributes = {})
  @attributes = attributes.symbolize_keys
  @interface = Module.new
  self.extend @interface
end

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

Returns:

  • (Boolean)


96
97
98
# File 'lib/wrest/components/attributes_container.rb', line 96

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