Class: WorkflowTemplate::Workflow::Description

Inherits:
Object
  • Object
show all
Defined in:
lib/workflow_template/workflow/description.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDescription

Returns a new instance of Description.



29
30
31
32
33
# File 'lib/workflow_template/workflow/description.rb', line 29

def initialize
  @initialize = []
  @perform = []
  @finalize = []
end

Class Method Details

.describe(workflow) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/workflow_template/workflow/description.rb', line 12

def self.describe(workflow)
  description = new
  registry.each do |group, blocks|
    blocks.each_value do |block|
      result = block.call(workflow)
      next if result.nil?

      description.add!(result, group)
    end
  end
  description.format
end

.register(group, key, &block) ⇒ Object



8
9
10
# File 'lib/workflow_template/workflow/description.rb', line 8

def self.register(group, key, &block)
  registry[group][key] = block
end

.registryObject



25
26
27
# File 'lib/workflow_template/workflow/description.rb', line 25

def self.registry
  @registry ||= { initialize: {}, perform: {}, finalize: {} }
end

Instance Method Details

#add!(description, group) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/workflow_template/workflow/description.rb', line 35

def add!(description, group)
  case group
  when :initialize then @initialize << description
  when :perform then @perform << description
  when :finalize then @finalize << description
  else raise Error, "Unexpected group: #{group}"
  end
end

#formatObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/workflow_template/workflow/description.rb', line 44

def format
  [@initialize, @perform, @finalize].filter_map do |group|
    next if group.empty?

    formatted = group.map do |element|
      element.is_a?(String) ? element : element.format(level: 0)
    end
    formatted.join("\n")
  end.join("\n\n") << "\n"
end