Class: RailsWorkflow::Context

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/rails_workflow/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject

Returns the value of attribute data.



6
7
8
# File 'app/models/rails_workflow/context.rb', line 6

def data
  @data
end

Instance Method Details

#init_dataObject



17
18
19
# File 'app/models/rails_workflow/context.rb', line 17

def init_data
  self.data = prepare_body(body).with_indifferent_access
end

#prepare_body(body) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/rails_workflow/context.rb', line 21

def prepare_body(body)
  if body.is_a? Array
    body.map do |element|
      prepare_body element
    end
  elsif body.is_a? Hash

    if body.keys == %w[id class]
      body['class'].constantize.find(body['id'])
    else
      res = {}
      body.each_pair do |key, value|
        res[key] = prepare_body(value)
      end
      res
    end

  else
    body
  end
end

#prepare_data(data) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/rails_workflow/context.rb', line 43

def prepare_data(data)
  if data.is_a? ActiveRecord::Base
    { id: data.id, class: data.class.to_s }
  elsif data.is_a? Array
    data.map do |element|
      prepare_data element
    end
  elsif data.is_a? Hash
    res = {}
    data.each_pair do |key, value|
      res[key] = prepare_data(value)
    end
    res
  else
    data
  end
end

#serialize_dataObject



13
14
15
# File 'app/models/rails_workflow/context.rb', line 13

def serialize_data
  self.body = prepare_data(data)
end