Class: Perpetuity::Mapper

Inherits:
Object
  • Object
show all
Includes:
DataInjectable
Defined in:
lib/perpetuity/mapper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataInjectable

#give_id_to, #inject_attribute, #inject_data

Constructor Details

#initialize(registry = Perpetuity.mapper_registry) ⇒ Mapper

Returns a new instance of Mapper.



13
14
15
# File 'lib/perpetuity/mapper.rb', line 13

def initialize registry=Perpetuity.mapper_registry
  @mapper_registry = registry
end

Instance Attribute Details

#mapper_registryObject (readonly)

Returns the value of attribute mapper_registry.



11
12
13
# File 'lib/perpetuity/mapper.rb', line 11

def mapper_registry
  @mapper_registry
end

Class Method Details

.attribute(name, options = {}) ⇒ Object



26
27
28
29
# File 'lib/perpetuity/mapper.rb', line 26

def self.attribute name, options = {}
  type = options.fetch(:type) { nil }
  attribute_set << Attribute.new(name, type, options)
end

.attribute_setObject



22
23
24
# File 'lib/perpetuity/mapper.rb', line 22

def self.attribute_set
  @attribute_set ||= AttributeSet.new
end

.attributesObject



31
32
33
# File 'lib/perpetuity/mapper.rb', line 31

def self.attributes
  attribute_set.map(&:name)
end

.data_source(configuration = Perpetuity.configuration) ⇒ Object



71
72
73
# File 'lib/perpetuity/mapper.rb', line 71

def self.data_source(configuration=Perpetuity.configuration)
  configuration.data_source
end

.id(&block) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/perpetuity/mapper.rb', line 146

def self.id &block
  if block_given?
    @id = block
  else
    @id ||= -> { nil }
  end
end

.index(attribute, options = {}) ⇒ Object



35
36
37
# File 'lib/perpetuity/mapper.rb', line 35

def self.index attribute, options={}
  data_source.index mapped_class, attribute_set[attribute], options
end

.map(klass, registry = Perpetuity.mapper_registry) ⇒ Object



17
18
19
20
# File 'lib/perpetuity/mapper.rb', line 17

def self.map klass, registry=Perpetuity.mapper_registry
  registry[klass] = self
  @mapped_class = klass
end

.mapped_classObject



193
194
195
# File 'lib/perpetuity/mapper.rb', line 193

def self.mapped_class
  @mapped_class
end

.validate(&block) ⇒ Object



177
178
179
# File 'lib/perpetuity/mapper.rb', line 177

def self.validate &block
  validations.instance_exec(&block)
end

.validationsObject



181
182
183
# File 'lib/perpetuity/mapper.rb', line 181

def self.validations
  @validations ||= ValidationSet.new
end

Instance Method Details

#allObject



99
100
101
# File 'lib/perpetuity/mapper.rb', line 99

def all
  retrieve
end

#all?(&block) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/perpetuity/mapper.rb', line 83

def all? &block
  count(&block) == count
end

#any?(&block) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/perpetuity/mapper.rb', line 79

def any? &block
  count(&block) > 0
end

#attributesObject



51
52
53
# File 'lib/perpetuity/mapper.rb', line 51

def attributes
  self.class.attributes
end

#count(&block) ⇒ Object



75
76
77
# File 'lib/perpetuity/mapper.rb', line 75

def count &block
  data_source.count mapped_class, &block
end

#data_sourceObject



185
186
187
# File 'lib/perpetuity/mapper.rb', line 185

def data_source
  self.class.data_source
end

#decrement(object, attribute, count = 1) ⇒ Object



171
172
173
174
175
# File 'lib/perpetuity/mapper.rb', line 171

def decrement object, attribute, count=1
  data_source.increment mapped_class, object.id, attribute, -count
rescue Moped::Errors::OperationFailure
  raise ArgumentError.new('Attempted to decrement a non-numeric value')
end

#delete(object) ⇒ Object



124
125
126
127
# File 'lib/perpetuity/mapper.rb', line 124

def delete object
  id = object.is_a?(PersistedObject) ? object.id : object
  data_source.delete id, mapped_class
end

#delete_allObject



55
56
57
# File 'lib/perpetuity/mapper.rb', line 55

def delete_all
  data_source.delete_all mapped_class
end

#find(*args, &block) ⇒ Object Also known as: detect



109
110
111
112
113
114
115
116
# File 'lib/perpetuity/mapper.rb', line 109

def find *args, &block
  if block_given?
    select(&block).first
  else
    id = args.first
    select { |object| object.id == id }.first
  end
end

#firstObject



95
96
97
# File 'lib/perpetuity/mapper.rb', line 95

def first
  retrieve.limit(1).first
end

#increment(object, attribute, count = 1) ⇒ Object



165
166
167
168
169
# File 'lib/perpetuity/mapper.rb', line 165

def increment object, attribute, count=1
  data_source.increment mapped_class, object.id, attribute, count
rescue Moped::Errors::OperationFailure
  raise ArgumentError.new('Attempted to increment a non-numeric value')
end

#indexesObject



43
44
45
# File 'lib/perpetuity/mapper.rb', line 43

def indexes
  data_source.indexes(mapped_class)
end

#insert(object) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/perpetuity/mapper.rb', line 59

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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/perpetuity/mapper.rb', line 129

def load_association! object, attribute
  objects = Array(object)
  dereferencer = Dereferencer.new(mapper_registry)
  dereferencer.load objects.map { |obj| obj.instance_variable_get("@#{attribute}") }

  objects.each do |obj|
    reference = obj.instance_variable_get("@#{attribute}")
    if reference.is_a? Array
      refs = reference
      real_objects = refs.map { |ref| dereferencer[ref] }
      inject_attribute obj, attribute, real_objects
    else
      inject_attribute obj, attribute, dereferencer[reference]
    end
  end
end

#mapped_classObject



197
198
199
# File 'lib/perpetuity/mapper.rb', line 197

def mapped_class
  self.class.mapped_class
end

#none?(&block) ⇒ Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/perpetuity/mapper.rb', line 91

def none? &block
  !any?(&block)
end

#one?(&block) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/perpetuity/mapper.rb', line 87

def one? &block
  count(&block) == 1
end

#reindex!Object



47
48
49
# File 'lib/perpetuity/mapper.rb', line 47

def reindex!
  indexes.each { |index| data_source.activate_index! index }
end

#reject(&block) ⇒ Object



120
121
122
# File 'lib/perpetuity/mapper.rb', line 120

def reject &block
  retrieve data_source.negate_query(&block).to_db
end

#remove_index!(index) ⇒ Object



39
40
41
# File 'lib/perpetuity/mapper.rb', line 39

def remove_index! index
  data_source.remove_index index
end

#save(object) ⇒ Object



161
162
163
# File 'lib/perpetuity/mapper.rb', line 161

def save object
  update object, serialize(object), false
end

#select(&block) ⇒ Object Also known as: find_all



103
104
105
# File 'lib/perpetuity/mapper.rb', line 103

def select &block
  retrieve data_source.query(&block).to_db
end

#serialize(object) ⇒ Object



189
190
191
# File 'lib/perpetuity/mapper.rb', line 189

def serialize object
  data_source.serialize(object, self)
end

#update(object, new_data, update_in_memory = true) ⇒ Object



154
155
156
157
158
159
# File 'lib/perpetuity/mapper.rb', line 154

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