Class: Perpetuity::Mapper
- Inherits:
-
Object
- Object
- Perpetuity::Mapper
show all
- Includes:
- DataInjectable
- Defined in:
- lib/perpetuity/mapper.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#give_id_to, #inject_attribute, #inject_data
Constructor Details
#initialize(registry = Perpetuity.mapper_registry) ⇒ Mapper
Returns a new instance of Mapper.
14
15
16
|
# File 'lib/perpetuity/mapper.rb', line 14
def initialize registry=Perpetuity.mapper_registry
@mapper_registry = registry
end
|
Instance Attribute Details
#mapper_registry ⇒ Object
Returns the value of attribute mapper_registry.
12
13
14
|
# File 'lib/perpetuity/mapper.rb', line 12
def mapper_registry
@mapper_registry
end
|
Class Method Details
.attribute(name, options = {}) ⇒ Object
27
28
29
30
|
# File 'lib/perpetuity/mapper.rb', line 27
def self.attribute name, options = {}
type = options.fetch(:type) { nil }
attribute_set << Attribute.new(name, type, options)
end
|
.attribute_set ⇒ Object
23
24
25
|
# File 'lib/perpetuity/mapper.rb', line 23
def self.attribute_set
@attribute_set ||= AttributeSet.new
end
|
.attributes ⇒ Object
32
33
34
|
# File 'lib/perpetuity/mapper.rb', line 32
def self.attributes
attribute_set.map(&:name)
end
|
.data_source(configuration = Perpetuity.configuration) ⇒ Object
76
77
78
|
# File 'lib/perpetuity/mapper.rb', line 76
def self.data_source(configuration=Perpetuity.configuration)
configuration.data_source
end
|
.id(&block) ⇒ Object
139
140
141
142
143
144
145
|
# File 'lib/perpetuity/mapper.rb', line 139
def self.id &block
if block_given?
@id = block
else
@id ||= -> { nil }
end
end
|
.index(attribute, options = {}) ⇒ Object
36
37
38
|
# File 'lib/perpetuity/mapper.rb', line 36
def self.index attribute, options={}
data_source.index mapped_class, attribute_set[attribute], options
end
|
.map(klass, registry = Perpetuity.mapper_registry) ⇒ Object
18
19
20
21
|
# File 'lib/perpetuity/mapper.rb', line 18
def self.map klass, registry=Perpetuity.mapper_registry
registry[klass] = self
@mapped_class = klass
end
|
.mapped_class ⇒ Object
88
89
90
|
# File 'lib/perpetuity/mapper.rb', line 88
def self.mapped_class
@mapped_class
end
|
.validate(&block) ⇒ Object
158
159
160
|
# File 'lib/perpetuity/mapper.rb', line 158
def self.validate &block
validations.instance_exec(&block)
end
|
.validations ⇒ Object
162
163
164
|
# File 'lib/perpetuity/mapper.rb', line 162
def self.validations
@validations ||= ValidationSet.new
end
|
Instance Method Details
#all ⇒ Object
100
101
102
|
# File 'lib/perpetuity/mapper.rb', line 100
def all
retrieve
end
|
#attributes ⇒ Object
52
53
54
|
# File 'lib/perpetuity/mapper.rb', line 52
def attributes
self.class.attributes
end
|
#count ⇒ Object
84
85
86
|
# File 'lib/perpetuity/mapper.rb', line 84
def count
data_source.count mapped_class
end
|
#data_source ⇒ Object
80
81
82
|
# File 'lib/perpetuity/mapper.rb', line 80
def data_source
self.class.data_source
end
|
#delete(object) ⇒ Object
117
118
119
|
# File 'lib/perpetuity/mapper.rb', line 117
def delete object
data_source.delete object, mapped_class
end
|
#delete_all ⇒ Object
56
57
58
|
# File 'lib/perpetuity/mapper.rb', line 56
def delete_all
data_source.delete_all mapped_class
end
|
#find(id) ⇒ Object
113
114
115
|
# File 'lib/perpetuity/mapper.rb', line 113
def find id
retrieve(id: id).first
end
|
#first ⇒ Object
96
97
98
|
# File 'lib/perpetuity/mapper.rb', line 96
def first
retrieve.limit(1).first
end
|
#indexes ⇒ Object
44
45
46
|
# File 'lib/perpetuity/mapper.rb', line 44
def indexes
data_source.indexes(mapped_class)
end
|
#insert(object) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
|
# File 'lib/perpetuity/mapper.rb', line 60
def insert object
raise "#{object} is invalid and cannot be persisted." unless self.class.validations.valid?(object)
serializable_attributes = serialize(object)
if o_id = object.instance_exec(&self.class.id)
serializable_attributes[:id] = o_id
end
new_id = data_source.insert mapped_class, serializable_attributes
give_id_to object, new_id
new_id
end
|
#load_association!(object, attribute) ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/perpetuity/mapper.rb', line 121
def load_association! object, attribute
objects = Array(object)
identity_map = IdentityMap.new(objects, attribute, mapper_registry)
objects.each do |object|
reference = object.send(attribute)
if reference.is_a? Array
refs = reference
real_objects = refs.map { |ref| identity_map[ref.klass][ref.id] }
inject_attribute object, attribute, real_objects
else
klass = reference.klass
id = reference.id
inject_attribute object, attribute, identity_map[klass][id]
end
end
end
|
#mapped_class ⇒ Object
92
93
94
|
# File 'lib/perpetuity/mapper.rb', line 92
def mapped_class
self.class.mapped_class
end
|
#reindex! ⇒ Object
48
49
50
|
# File 'lib/perpetuity/mapper.rb', line 48
def reindex!
indexes.each { |index| data_source.activate_index! index }
end
|
#remove_index!(index) ⇒ Object
40
41
42
|
# File 'lib/perpetuity/mapper.rb', line 40
def remove_index! index
data_source.remove_index index
end
|
#retrieve(criteria = {}) ⇒ Object
104
105
106
|
# File 'lib/perpetuity/mapper.rb', line 104
def retrieve criteria={}
Perpetuity::Retrieval.new mapped_class, criteria, data_source
end
|
#save(object) ⇒ Object
154
155
156
|
# File 'lib/perpetuity/mapper.rb', line 154
def save object
update object, serialize(object), false
end
|
#select(&block) ⇒ Object
108
109
110
111
|
# File 'lib/perpetuity/mapper.rb', line 108
def select &block
query = data_source.class::Query.new(&block).to_db
retrieve query
end
|
#serialize(object) ⇒ Object
72
73
74
|
# File 'lib/perpetuity/mapper.rb', line 72
def serialize object
Serializer.new(self, mapper_registry).serialize(object)
end
|
#update(object, new_data, update_in_memory = true) ⇒ Object
147
148
149
150
151
152
|
# File 'lib/perpetuity/mapper.rb', line 147
def update object, new_data, update_in_memory = true
id = object.is_a?(mapped_class) ? object.id : object
inject_data object, new_data if update_in_memory
data_source.update mapped_class, id, new_data
end
|