Class: Logical::Naf::Pickler

Inherits:
Object
  • Object
show all
Includes:
Af::Application::Component
Defined in:
app/models/logical/naf/pickler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(naf_version, preservables) ⇒ Pickler

Returns a new instance of Pickler.



11
12
13
14
15
16
# File 'app/models/logical/naf/pickler.rb', line 11

def initialize(naf_version, preservables)
  @preservables = preservables
  @naf_version = naf_version
  @preserves = {}
  @context = {}
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'app/models/logical/naf/pickler.rb', line 6

def context
  @context
end

#naf_versionObject (readonly)

Returns the value of attribute naf_version.



6
7
8
# File 'app/models/logical/naf/pickler.rb', line 6

def naf_version
  @naf_version
end

#preservablesObject (readonly)

Returns the value of attribute preservables.



6
7
8
# File 'app/models/logical/naf/pickler.rb', line 6

def preservables
  @preservables
end

#preservesObject (readonly)

Returns the value of attribute preserves.



6
7
8
# File 'app/models/logical/naf/pickler.rb', line 6

def preserves
  @preserves
end

Instance Method Details

#generic_pickle(instance, associations = nil, ignored_attributes = [:created_at, :updated_at]) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/logical/naf/pickler.rb', line 18

def generic_pickle(instance, associations = nil, ignored_attributes = [:created_at, :updated_at])
  instance_attributes = instance.attributes.symbolize_keys
  ignored_attributes.each do |ignored_attribute|
    instance_attributes.delete(ignored_attribute.to_sym)
  end

  unless associations
    associations = {}
    instance_attributes.keys.select{|key| key.to_s =~ /_id$/}.each do |key|
      association_name = key.to_s[0..-4].to_sym
      association = instance.association(association_name) rescue nil
      if association
        associations[key] = association.options[:class_name].constantize.name
      end
    end
  end

  return Hash[instance_attributes.map { |key,value|
                if associations[key]
                  [key, { association_model_name: associations[key], association_value: value }]
                else
                  [key,value]
                end
              } ]
end

#pickle_jarObject



66
67
68
69
70
71
72
# File 'app/models/logical/naf/pickler.rb', line 66

def pickle_jar
  {
    version: @naf_version,
    preserved_at: Time.now,
    preserves: @preserves
  }
end

#preserveObject



44
45
46
47
48
# File 'app/models/logical/naf/pickler.rb', line 44

def preserve
  @preservables.each do |model|
    preserve_model(model)
  end
end

#preserve_model(model) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/logical/naf/pickler.rb', line 50

def preserve_model(model)
  if model.respond_to?(:pickleables)
    pickables = model.pickleables(self)
  else
    pickables = model.all
  end

  @preserves[model.name] = pickables.map do |instance|
    if instance.respond_to?(:pickle)
      instance.pickle(self)
    else
      generic_pickle(instance)
    end
  end
end