Class: Store
- Inherits:
-
Object
show all
- Defined in:
- lib/store.rb,
lib/store/ref.rb,
lib/store/repo.rb,
lib/store/query.rb,
lib/store/version.rb,
lib/store/data_mapper.rb,
lib/store/entity_mapper.rb
Defined Under Namespace
Classes: DataMapper, EntityMapper, Query, Ref, Repo
Constant Summary
collapse
- VERSION =
"0.0.6"
Instance Method Summary
collapse
Constructor Details
#initialize(entity_classes, data_mapping, entity_mapping) ⇒ Store
Returns a new instance of Store.
9
10
11
12
13
14
|
# File 'lib/store.rb', line 9
def initialize(entity_classes, data_mapping, entity_mapping)
@entity_classes = entity_classes
@resolver_data_mapper = Resolver::DataMapper.new(data_mapping)
@resolver_entity_mapper = Resolver::EntityMapper.new(self, entity_mapping)
@entity_references = EntityReferences.new
end
|
Instance Method Details
#build(entity_class_name, attributes = {}) ⇒ Object
16
17
18
19
20
21
22
23
24
|
# File 'lib/store.rb', line 16
def build(entity_class_name, attributes = {})
entity = @entity_classes[entity_class_name].new
attributes.each do |attribute, value|
entity.public_send :"#{attribute}=", value
end
entity
end
|
#bulk_load(refs) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/store.rb', line 82
def bulk_load(refs)
data_mapper = data_mapper(refs.first)
entity_class = @entity_classes[refs.first.entity_type]
references = refs.map(&:reference)
mapped_entities = data_mapper.bulk_find(references)
entities = []
references.each_with_index do |reference, i|
mapped_entity = mapped_entities[i]
entity = entity_mapper(refs.first).unmap(entity_class, mapped_entity)
save_entity_reference reference, entity
entities << entity
end
entities
end
|
#load(refs) ⇒ Object
62
63
64
65
66
67
68
|
# File 'lib/store.rb', line 62
def load(refs)
if refs.kind_of? Array
bulk_load refs
else
single_load refs
end
end
|
#query(query) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/store.rb', line 47
def query(query)
data_mapper = data_mapper(query)
entity_class = @entity_classes[query.entity]
raise_unless_data_mapper_found(query, data_mapper)
mapped_entities_with_references = data_mapper.select(query) || {}
mapped_entities_with_references.map do |reference, mapped_entity|
entity = entity_mapper(query).unmap(entity_class, mapped_entity)
save_entity_reference reference, entity
entity
end
end
|
#remove(entity) ⇒ Object
37
38
39
40
41
42
43
44
45
|
# File 'lib/store.rb', line 37
def remove(entity)
data_mapper = data_mapper(entity)
raise_unless_data_mapper_found(entity, data_mapper)
reference = find_entity_reference(entity)
data_mapper.delete(reference)
remove_entity_reference entity
end
|
#save(entities) ⇒ Object
26
27
28
29
30
31
32
33
34
35
|
# File 'lib/store.rb', line 26
def save(entities)
if entities.kind_of? Array
entities.map do |entity|
save_entity(entity)
end
else
entity = entities
save_entity(entity)
end
end
|
#single_load(ref) ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/store.rb', line 70
def single_load(ref)
data_mapper = data_mapper(ref)
entity_class = @entity_classes[ref.entity_type]
reference = ref.reference
mapped_entity = data_mapper.single_find(reference)
entity = entity_mapper(ref).unmap(entity_class, mapped_entity)
save_entity_reference reference, entity
entity
end
|