Module: ElasticAttributes::ClassMethods
- Defined in:
- lib/elastic_attributes.rb
Instance Method Summary collapse
-
#attribute(name, type_or_options = nil, options = {}) ⇒ Object
Define an attribute in the current class.
-
#from(data) ⇒ Object
Create an object from data.
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, = nil, = {}) if .is_a?(Hash) = else [:type] = if end attr_accessor name self.attributes ||= {} self.attributes[name] = self.default_attribute = name if [: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 |