Class: Iugu::Object
- Inherits:
-
Object
show all
- Defined in:
- lib/iugu/object.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}) ⇒ Object
Returns a new instance of Object.
10
11
12
13
|
# File 'lib/iugu/object.rb', line 10
def initialize(attributes = {})
@unsaved_attributes = Set.new
set_attributes attributes
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
26
27
28
29
30
31
|
# File 'lib/iugu/object.rb', line 26
def method_missing(name, *args)
return super unless name.to_s.end_with? '='
return super if name.to_s.start_with? 'id'
add_accessor(name.to_s[0...-1])
return send(name, args[0])
end
|
Instance Attribute Details
Returns the value of attribute errors.
6
7
8
|
# File 'lib/iugu/object.rb', line 6
def errors
@errors
end
|
Instance Method Details
#add_accessor(name) ⇒ Object
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/iugu/object.rb', line 15
def add_accessor(name)
metaclass.instance_eval do
define_method(name.to_s) { @attributes[name.to_s] }
define_method(name.to_s + "=") do |value|
@attributes[name.to_s] = value
@unsaved_attributes.add name.to_s
end unless name.to_s == 'id'
end
self.class.__send__ :attr_accessor, name.to_sym
end
|
#attributes ⇒ Object
33
34
35
|
# File 'lib/iugu/object.rb', line 33
def attributes
@attributes
end
|
#copy(object) ⇒ Object
41
42
43
44
45
|
# File 'lib/iugu/object.rb', line 41
def copy(object)
@unsaved_attributes = Set.new
@attributes = {}
set_attributes object.attributes
end
|
#modified_attributes ⇒ Object
37
38
39
|
# File 'lib/iugu/object.rb', line 37
def modified_attributes
Iugu::Utils.intersect @unsaved_attributes, @attributes
end
|
#set_attributes(attributes, unsaved = false) ⇒ Object
47
48
49
50
51
52
53
|
# File 'lib/iugu/object.rb', line 47
def set_attributes(attributes, unsaved = false)
@attributes = Iugu::Utils.stringify_keys(attributes)
@attributes.each do |k, v|
add_accessor(k)
end
@unsaved_attributes = @attributes.keys.to_set if unsaved
end
|