Module: ElasticAttributes::ClassMethods

Defined in:
lib/elastic_attributes.rb

Instance Method Summary collapse

Instance Method Details

#attribute(name, type_or_options = nil, options = {}) ⇒ Object

Define an attribute in the current class.

attribute :image, Image # Image.from(data) will be called on the input['image']
attribute :images, [Array, Image] # array of images - Image.from(data) will be called on all items in input['images'] array
attribute :text, :is_default => true # If the input is not a hash, it will be assigned to this attribute, and all other attributes will be nil


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/elastic_attributes.rb', line 9

def attribute(name, type_or_options = nil, options = {})
  if type_or_options.is_a?(Hash)
    options = type_or_options
  else
    options[:type] = type_or_options if type_or_options
  end
  attr_accessor name
  self.attributes ||= {}
  self.attributes[name] = options
  self.default_attribute = name if options[:is_default]
end

#from(data) ⇒ Object

Create an object from data



22
23
24
25
26
# File 'lib/elastic_attributes.rb', line 22

def from data
  obj = new
  obj.decode data
  obj
end