Module: EasyMapper::Model
- Defined in:
- lib/easy_mapper/model.rb,
lib/easy_mapper/model/class_macros.rb,
lib/easy_mapper/model/query_methods.rb
Defined Under Namespace
Modules: ClassMacros, QueryMethods
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(cls) ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
# File 'lib/easy_mapper/model.rb', line 9
def self.included(cls)
cls.extend ClassMacros
cls.extend QueryMethods
db_adapter = EasyMapper::Config.db_adapter
cls.repository EasyMapper::DbRepository.new(cls, db_adapter)
cls.class_exec do
def initialize(initial_values = {})
@object = initial_values.select do |key, _|
self.class.attributes.include? key
end
all_associations = associations_to_one + associations_to_many
defined_assoc_names = all_associations.map(&:name)
@associations = initial_values.select do |key, _|
defined_assoc_names.include? key
end
associations_to_many
.reject { |assoc| initial_values.include? assoc.name }
.each do |assoc|
@associations[assoc.name] = []
end
end
end
def save
associations_to_one.each do |assoc_to_one|
result = @associations[assoc_to_one.name].save
@object[assoc_to_one.id_column] = result.id
end
if id
repository.update(id, @object)
else
@object[:id] = repository.next_id
repository.create(@object)
end
associations_to_many.each do |assoc_to_many|
@associations[assoc_to_many.name].each do |model|
model.public_send "#{assoc_to_many.mapped_by}=", id
model.save
end
end
self
end
def delete
raise Errors::DeleteUnsavedRecordError unless id
associations_to_many.each do |assoc_to_many|
@associations[assoc_to_many.name].each(&:delete)
end
repository.delete(id: id)
associations_to_one.each do |assoc_to_one|
result = @associations[assoc_to_one.name].delete
end
end
def ==(other)
return id == other.id if id && other.id
equal? other
end
private
def primary_keys
self.class.primary_keys
end
def repository
self.class.repository
end
def associations_to_many
self.class.associations_to_many
end
def associations_to_one
self.class.associations_to_one
end
end
|
Instance Method Details
#==(other) ⇒ Object
74
75
76
77
|
# File 'lib/easy_mapper/model.rb', line 74
def ==(other)
return id == other.id if id && other.id
equal? other
end
|
#associations_to_many ⇒ Object
89
90
91
|
# File 'lib/easy_mapper/model.rb', line 89
def associations_to_many
self.class.associations_to_many
end
|
#associations_to_one ⇒ Object
93
94
95
|
# File 'lib/easy_mapper/model.rb', line 93
def associations_to_one
self.class.associations_to_one
end
|
#delete ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# File 'lib/easy_mapper/model.rb', line 60
def delete
raise Errors::DeleteUnsavedRecordError unless id
associations_to_many.each do |assoc_to_many|
@associations[assoc_to_many.name].each(&:delete)
end
repository.delete(id: id)
associations_to_one.each do |assoc_to_one|
result = @associations[assoc_to_one.name].delete
end
end
|
#primary_keys ⇒ Object
81
82
83
|
# File 'lib/easy_mapper/model.rb', line 81
def primary_keys
self.class.primary_keys
end
|
#repository ⇒ Object
85
86
87
|
# File 'lib/easy_mapper/model.rb', line 85
def repository
self.class.repository
end
|
#save ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/easy_mapper/model.rb', line 37
def save
associations_to_one.each do |assoc_to_one|
result = @associations[assoc_to_one.name].save
@object[assoc_to_one.id_column] = result.id
end
if id
repository.update(id, @object)
else
@object[:id] = repository.next_id
repository.create(@object)
end
associations_to_many.each do |assoc_to_many|
@associations[assoc_to_many.name].each do |model|
model.public_send "#{assoc_to_many.mapped_by}=", id
model.save
end
end
self
end
|