Class: Mushy::Flow

Inherits:
Object
  • Object
show all
Defined in:
lib/mushy/flow.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFlow

Returns a new instance of Flow.



9
10
11
12
# File 'lib/mushy/flow.rb', line 9

def initialize
  self.id = SecureRandom.uuid
  self.fluxs = []
end

Instance Attribute Details

#fluxsObject

Returns the value of attribute fluxs.



7
8
9
# File 'lib/mushy/flow.rb', line 7

def fluxs
  @fluxs
end

#idObject

Returns the value of attribute id.



6
7
8
# File 'lib/mushy/flow.rb', line 6

def id
  @id
end

Class Method Details

.build_flux(record) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/mushy/flow.rb', line 26

def self.build_flux record
  type = record[:type] || record['type'] || record[:flux] || record['flux'] || 'Flux'
  flux = Object.const_get("Mushy::#{type}").new
  flux.id = record[:id] || record['id'] || flux.id
  flux.type = type
  flux.config = SymbolizedHash.new(record[:config] || record['config'])
  flux.flow = Mushy::Flow.new
  flux
end

.parse(data) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mushy/flow.rb', line 42

def self.parse data
  data = JSON.parse data

  data_fluxs = data['fluxs'] || []
  data_fluxs.select { |x| x['parent'] }.map { |r| r["parents"] = [r["parent"]] }

  flow = new

  flow.fluxs = data_fluxs.map { |s| flow.build_flux s }

  fluxs_with_parent_ids = flow.fluxs.reduce({}) { |t, i| t[i.id] = []; t }
  data_fluxs.map { |r| fluxs_with_parent_ids[r['id']] = r['parents'] || [] }

  flow.fluxs.each do |flux|
    flux.parent_fluxs = flow.fluxs.select { |x| fluxs_with_parent_ids[flux.id].include?(x.id) }
  end

  flow
end

Instance Method Details

#adjust_data(data) ⇒ Object



20
21
22
23
24
# File 'lib/mushy/flow.rb', line 20

def adjust_data data
  fluxs
    .select { |x| x.respond_to? :adjust_data }
    .reduce(data) { |t, i| i.adjust_data t }
end

#build_flux(record) ⇒ Object



36
37
38
39
40
# File 'lib/mushy/flow.rb', line 36

def build_flux record
  Mushy::Flow.build_flux(record).tap do |flux|
    flux.flow = self
  end
end

#fluxs_for(event) ⇒ Object



14
15
16
17
18
# File 'lib/mushy/flow.rb', line 14

def fluxs_for event
  fluxs
    .select { |x| x.parent_fluxs.any? { |y| y.id == event.flux_id } }
    .flatten
end