Class: Tractor::Model::Mapper
- Inherits:
-
Base
- Object
- Base
- Tractor::Model::Mapper
show all
- Defined in:
- lib/tractor/model/mapper.rb
Class Attribute Summary collapse
Class Method Summary
collapse
Methods inherited from Base
#add_to_associations, #add_to_indices, after_create, after_destroy, after_save, all, association, attribute, count, create, #delete_from_indices, #destroy, exists?, find, find_by_attribute, find_by_id, getter, ids, index, #initialize, #remove_from_associations, run_callbacks, #save, setter, #to_h, #update
Methods included from Dirty
all, key, #mark
Class Attribute Details
.dependencies ⇒ Object
Returns the value of attribute dependencies.
5
6
7
|
# File 'lib/tractor/model/mapper.rb', line 5
def dependencies
@dependencies
end
|
Class Method Details
.attribute_mapper(server_instance) ⇒ Object
63
64
65
66
67
68
69
70
|
# File 'lib/tractor/model/mapper.rb', line 63
def attribute_mapper(server_instance)
attributes = {}
self.attributes.each do |name, options|
server_value = server_instance.respond_to?(options[:map]) ? server_instance.send(options[:map]) : nil
attributes[name] = server_value
end
attributes
end
|
.create_from_instance(server_instance) ⇒ Object
26
27
28
29
30
31
32
33
34
|
# File 'lib/tractor/model/mapper.rb', line 26
def create_from_instance(server_instance)
hydrate_attributes(server_instance) do |attributes|
begin
return self.create(attributes)
rescue DuplicateKeyError
return find_from_instance(server_instance)
end
end
end
|
.dependencies_met?(server_instance) ⇒ Boolean
76
77
78
79
80
81
|
# File 'lib/tractor/model/mapper.rb', line 76
def dependencies_met?(server_instance)
dependencies.each do |klass, options|
return false unless dependency_met_for?(server_instance, klass)
end
return true
end
|
.dependency_met_for?(server_instance, klass) ⇒ Boolean
72
73
74
|
# File 'lib/tractor/model/mapper.rb', line 72
def dependency_met_for?(server_instance, klass)
!!klass.find_by_id(server_instance.send(dependencies[klass][:key_name]))
end
|
.depends_on(klass, options = {}) ⇒ Object
7
8
9
10
11
12
|
# File 'lib/tractor/model/mapper.rb', line 7
def depends_on(klass, options = {})
dependencies[klass] = options
end
|
.ensure_dependencies_met(server_instance, hydrate_only = false, &block) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/tractor/model/mapper.rb', line 83
def ensure_dependencies_met(server_instance, hydrate_only=false, &block)
return if !hydrate_only && dependencies_met?(server_instance)
dependencies.each do |klass, options|
if hydrate_only || klass.find_by_id(server_instance.send(options[:key_name])).nil?
server_instances = server_instance.send(options[:method_name])
server_instances = server_instances.is_a?(Array) ? server_instances : [server_instances]
server_instances.each do |obj|
if hydrate_only
klass.hydrate_attributes(obj, hydrate_only, &block)
else
klass.create_from_instance(obj)
end
end
end
end
end
|
.find_from_instance(server_instance) ⇒ Object
22
23
24
|
# File 'lib/tractor/model/mapper.rb', line 22
def find_from_instance(server_instance)
self.find_by_id(server_instance.id)
end
|
.hydrate_attributes(server_instance, hydrate_only = false, &block) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/tractor/model/mapper.rb', line 51
def hydrate_attributes(server_instance, hydrate_only=false, &block)
attributes = attribute_mapper(server_instance)
if hydrate_only
ensure_dependencies_met(server_instance, hydrate_only, &block)
block.call([self.to_s, attributes])
else
ensure_dependencies_met(server_instance, hydrate_only)
yield attributes
end
return attributes
end
|
.remove(server_id) ⇒ Object
45
46
47
48
49
|
# File 'lib/tractor/model/mapper.rb', line 45
def remove(server_id)
obj_to_destroy = self.find_by_id(server_id)
return false if obj_to_destroy.nil?
obj_to_destroy.destroy
end
|
.representation_for(server_instance) ⇒ Object
14
15
16
17
18
19
20
|
# File 'lib/tractor/model/mapper.rb', line 14
def representation_for(server_instance)
if exists?(server_instance.id)
update_from_instance(server_instance)
else
create_from_instance(server_instance)
end
end
|
.update_from_instance(server_instance) ⇒ Object
36
37
38
39
40
41
42
43
|
# File 'lib/tractor/model/mapper.rb', line 36
def update_from_instance(server_instance)
existing_record = find_from_instance(server_instance)
raise "Cannot update an object that doesn't exist." unless existing_record
hydrate_attributes(server_instance) do |attributes|
return existing_record.update(attributes)
end
end
|