Module: Wrest::Components::Container::InstanceMethods

Defined in:
lib/wrest/components/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.



178
179
180
181
182
183
184
185
186
187
# File 'lib/wrest/components/container.rb', line 178

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[-1] == '=' || method_name[-1] == '?'
    generate_methods!(attribute_name, method_name)
    send(method_sym, *arguments)
  else
    super(method_sym, *arguments)
  end
end

Instance Method Details

#[](key) ⇒ Object



159
160
161
# File 'lib/wrest/components/container.rb', line 159

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

#[]=(key, value) ⇒ Object



163
164
165
# File 'lib/wrest/components/container.rb', line 163

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 Container in your own class.



133
134
135
# File 'lib/wrest/components/container.rb', line 133

def initialize(attributes = {})
  @attributes = HashWithIndifferentAccess.new(attributes)
end

#respond_to_missing?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


167
168
169
170
171
172
173
174
# File 'lib/wrest/components/container.rb', line 167

def respond_to_missing?(method_name, *)
  if super.respond_to?(method_name)
    true
  else
    @attributes.include?(method_name.to_s.gsub(/(\?$)|(=$)/,
                                               '').to_sym)
  end
end

#serialise_using(translator, options = {}) ⇒ Object

A translator is a anything that knows how to serialise a Hash. It must needs have a method named ‘serialise’ that accepts a hash and configuration options, and returns the serialised result (leaving the hash unchanged, of course).

Examples for JSON and XML can be found under Wrest::Components::Translators. These serialised output of these translators will work out of the box for Rails applications; you may need to roll your own for anything else.

Note: When serilising to XML, if you want the name of the class as the name of the root node then you should use the Container#to_xml helper.



148
149
150
151
152
153
# File 'lib/wrest/components/container.rb', line 148

def serialise_using(translator, options = {})
  payload = {
    self.class.element_name => @attributes.dup
  }
  translator.serialise(payload, options)
end

#to_xml(options = {}) ⇒ Object



155
156
157
# File 'lib/wrest/components/container.rb', line 155

def to_xml(options = {})
  serialise_using(Wrest::Components::Translators::Xml, { root: self.class.element_name }.merge(options))
end