Module: Dynamini::Attributes

Included in:
Base
Defined in:
lib/dynamini/attributes.rb

Constant Summary collapse

ADDABLE_TYPES =
[:set, :array, :integer, :float, :time, :date]
DELETED_TOKEN =
'__deleted__'

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dynamini/attributes.rb', line 75

def method_missing(name, *args, &block)
  if write_method?(name)
    write_attribute(attribute_name(name), args.first)
  elsif was_method?(name)
    __was(name)
  elsif args.empty? && read_method?(name)
    read_attribute(name)
  else
    super
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



7
8
9
# File 'lib/dynamini/attributes.rb', line 7

def attributes
  @attributes
end

Instance Method Details

#add_to(attribute, value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dynamini/attributes.rb', line 26

def add_to(attribute, value)
  complain_about(attribute) unless self.class.handles[attribute]
  old_value = read_attribute(attribute)
  add_value = self.class.attribute_callback(TypeHandler::SETTER_PROCS,  self.class.handles[attribute], value, true)
  if ADDABLE_TYPES.include? self.class.handles[attribute][:format]
    @attributes[attribute] ? @attributes[attribute] += add_value : @attributes[attribute] = add_value
  else
    complain_about(attribute)
  end
  record_change(attribute, old_value, add_value, 'ADD')
  self
end

#assign_attributes(attributes) ⇒ Object



9
10
11
12
13
14
# File 'lib/dynamini/attributes.rb', line 9

def assign_attributes(attributes)
  attributes.each do |key, value|
    write_attribute(key, value)
  end
  nil
end

#delete_attribute(attribute) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/dynamini/attributes.rb', line 39

def delete_attribute(attribute)
  if @attributes[attribute]
    old_value = read_attribute(attribute)
    record_change(attribute, old_value, DELETED_TOKEN, 'DELETE')
    @attributes.delete(attribute)
  end
end

#delete_attribute!(attribute) ⇒ Object



47
48
49
50
# File 'lib/dynamini/attributes.rb', line 47

def delete_attribute!(attribute)
  delete_attribute(attribute)
  save!
end

#handled_attributesObject



52
53
54
55
56
# File 'lib/dynamini/attributes.rb', line 52

def handled_attributes
  attributes.each_with_object({}) do |(attribute_name, _value), result|
    result[attribute_name.to_sym] = send(attribute_name.to_sym)
  end
end

#inspectObject



58
59
60
61
# File 'lib/dynamini/attributes.rb', line 58

def inspect
  attrib_string = handled_attributes.map { |(a, v)| "#{a}: #{v.inspect}" }.join(', ')
  "#<#{self.class} #{attrib_string}>"
end

#update_attribute(key, value, options = {}) ⇒ Object



16
17
18
19
# File 'lib/dynamini/attributes.rb', line 16

def update_attribute(key, value, options = {})
  write_attribute(key, value)
  save!(options)
end

#update_attributes(attributes, options = {}) ⇒ Object



21
22
23
24
# File 'lib/dynamini/attributes.rb', line 21

def update_attributes(attributes, options = {})
  assign_attributes(attributes)
  save!(options)
end