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
25
26
27
28
29
30
|
# File 'lib/iugu/object.rb', line 25
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
|
# File 'lib/iugu/object.rb', line 15
def add_accessor(name)
singleton_class.class_eval do
define_method(name.to_s) { self.attributes[name.to_s] }
define_method(name.to_s + "=") do |value|
self.attributes[name.to_s] = value
self.unsaved_attributes.add name.to_s
end unless name.to_s == 'id'
end
end
|
#attributes ⇒ Object
36
37
38
|
# File 'lib/iugu/object.rb', line 36
def attributes
@attributes
end
|
#copy(object) ⇒ Object
44
45
46
47
48
|
# File 'lib/iugu/object.rb', line 44
def copy(object)
@unsaved_attributes = Set.new
@attributes = {}
set_attributes object.attributes
end
|
#modified_attributes ⇒ Object
40
41
42
|
# File 'lib/iugu/object.rb', line 40
def modified_attributes
Iugu::Utils.intersect @unsaved_attributes, @attributes
end
|
#set_attributes(attributes, unsaved = false) ⇒ Object
50
51
52
53
54
55
56
|
# File 'lib/iugu/object.rb', line 50
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
|
#unsaved_attributes ⇒ Object
32
33
34
|
# File 'lib/iugu/object.rb', line 32
def unsaved_attributes
@unsaved_attributes
end
|