Class: MarkdownComposer::Plan

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_composer/plan.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Plan

Returns a new instance of Plan.



21
22
23
24
# File 'lib/markdown_composer/plan.rb', line 21

def initialize(config)
  @diagnostics = Diagnostics.new
  @config = normalize(config || {})
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/markdown_composer/plan.rb', line 5

def config
  @config
end

#diagnosticsObject (readonly)

Returns the value of attribute diagnostics.



5
6
7
# File 'lib/markdown_composer/plan.rb', line 5

def diagnostics
  @diagnostics
end

Class Method Details

.from_rows(row_hashes) ⇒ Object



13
14
15
# File 'lib/markdown_composer/plan.rb', line 13

def self.from_rows(row_hashes)
  new("compose" => Array(row_hashes).map { |row| normalize_hash(row) })
end

.invalid(code, message) ⇒ Object



7
8
9
10
11
# File 'lib/markdown_composer/plan.rb', line 7

def self.invalid(code, message)
  plan = new({})
  plan.diagnostics.error(code, message)
  plan
end

.normalize_hash(value) ⇒ Object



17
18
19
# File 'lib/markdown_composer/plan.rb', line 17

def self.normalize_hash(value)
  value.to_h.transform_keys { |key| key.to_s.downcase.tr(" ", "_") }
end

Instance Method Details

#outputObject



30
31
32
# File 'lib/markdown_composer/plan.rb', line 30

def output
  config.fetch("output", "markdown").to_s
end

#stepsObject



34
35
36
# File 'lib/markdown_composer/plan.rb', line 34

def steps
  Array(compose_config).map { |step| normalize_step(step) }
end

#to_hObject



42
43
44
45
# File 'lib/markdown_composer/plan.rb', line 42

def to_h
  canonical = config.reject { |key, _value| %w[steps transforms compose transform].include?(key) }
  canonical.merge("compose" => steps, "transform" => transforms, "output" => output)
end

#transformsObject



38
39
40
# File 'lib/markdown_composer/plan.rb', line 38

def transforms
  Array(transform_config).map.with_index { |transform, index| normalize_transform(transform, path: "transform[#{index}]") }
end

#versionObject



26
27
28
# File 'lib/markdown_composer/plan.rb', line 26

def version
  config.fetch("version", 1)
end