Class: ActiveSnapshot::SnapshotItem

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/active_snapshot/models/snapshot_item.rb

Instance Method Summary collapse

Instance Method Details

#objectObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_snapshot/models/snapshot_item.rb', line 16

def object
  return @object if @object

  if ActiveSnapshot.config.storage_method_json?
    @object = JSON.parse(self[:object])
  elsif ActiveSnapshot.config.storage_method_yaml?
    yaml_method = "unsafe_load"

    if !YAML.respond_to?("unsafe_load")
      yaml_method = "load"
    end

    @object = YAML.send(yaml_method, self[:object])
  elsif ActiveSnapshot.config.storage_method_native_json?
    @object = self[:object]
  end
end

#object=(h) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/active_snapshot/models/snapshot_item.rb', line 34

def object=(h)
  @object = nil

  if ActiveSnapshot.config.storage_method_json?
    self[:object] = h.to_json
  elsif ActiveSnapshot.config.storage_method_yaml?
    self[:object] = YAML.dump(h)
  elsif ActiveSnapshot.config.storage_method_native_json?
    self[:object] = h
  end
end

#restore_item!Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/active_snapshot/models/snapshot_item.rb', line 46

def restore_item!
  ### Add any custom logic here

  if !item
    item_klass = item_type.constantize

    self.item = item_klass.new
  end

  item.assign_attributes(object)

  item.save!(validate: false, touch: false)
end