Class: Plyushkin::Persistence

Inherits:
Object
  • Object
show all
Defined in:
lib/plyushkin/persistence.rb

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Persistence

Returns a new instance of Persistence.



3
4
5
6
7
# File 'lib/plyushkin/persistence.rb', line 3

def initialize(model)
  @model = model
  @callbacks = {}
  @filters = {}
end

Instance Method Details

#load(id) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/plyushkin/persistence.rb', line 26

def load(id)
  @properties = {}
  cached(id).each do |name, values|
    property = Plyushkin::Property.load(name, model.registered_types[name.to_sym], values,
                                        :callbacks               => @callbacks[name.to_sym],
                                        :ignore_unchanged_values => model.ignore_unchanged_values[name.to_sym],
                                        :filter                  => @filters[name.to_sym])
    @properties[name.to_sym] = property
  end if id
  add_missing_properties
end

#propertiesObject



9
10
11
# File 'lib/plyushkin/persistence.rb', line 9

def properties
  @properties
end

#register_callback(name, callback, &block) ⇒ Object



38
39
40
# File 'lib/plyushkin/persistence.rb', line 38

def register_callback(name, callback, &block)
  @callbacks[name] = { callback => block }
end

#register_filter(name, &block) ⇒ Object



42
43
44
# File 'lib/plyushkin/persistence.rb', line 42

def register_filter(name, &block)
  @filters[name] = block
end

#save(id) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/plyushkin/persistence.rb', line 13

def save(id)
  hash = {}
  props = @properties || {}

  props.each do |name, property|
    hash[name] = property.value_hashes
  end
  model.service.put(model.name, id, hash)
  model.cache.write(get_key(model.name, id), hash)

  props.values.each(&:mark_persisted)
end