Module: Dynattribs

Defined in:
lib/dynattribs.rb,
lib/dynattribs/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

this bit of ma allows us to “include” class methods (instead of the normal “extend”)with the mixin. which is needed as the dynamic_attr_accessor method needs to be invoked as a class method as it is used in the class definition, rather than from within a instance method



53
54
55
# File 'lib/dynattribs.rb', line 53

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#get_dynamic_attr(attr_name) ⇒ Object

a general method for retieving a named attribute from the encoded data



30
31
32
# File 'lib/dynattribs.rb', line 30

def get_dynamic_attr(attr_name)
  mapped_data[attr_name]
end

#mapped_dataObject

parse and return the json encoded values from the encoded data stored in the nominated attribute



12
13
14
15
# File 'lib/dynattribs.rb', line 12

def mapped_data
  return {} if self[dynamic_attributes_backing_field_name].nil?
  JSON.parse(self[dynamic_attributes_backing_field_name])
end

#mapped_data=(hash) ⇒ Object

JSON encode and store a hash of attributes



18
19
20
# File 'lib/dynattribs.rb', line 18

def mapped_data=hash
  self[dynamic_attributes_backing_field_name] = hash.to_json
end

#set_dynamic_attr(attr_name, value) ⇒ Object

a general method to set the value of the named attribute in the data map



36
37
38
39
40
# File 'lib/dynattribs.rb', line 36

def set_dynamic_attr(attr_name, value)
  data_map = self.mapped_data
  data_map[attr_name] = value
  self.mapped_data = data_map
end