Class: ActionDoc::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/actiondoc/generator.rb

Overview

The documentation generator class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, options) ⇒ Generator

Initialize an instance of this ‘Generator` class



35
36
37
38
39
# File 'lib/actiondoc/generator.rb', line 35

def initialize(args, options)
  @args = args
  @options = options
  @padding = options.fetch(:padding, :nice)
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



32
33
34
# File 'lib/actiondoc/generator.rb', line 32

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



32
33
34
# File 'lib/actiondoc/generator.rb', line 32

def options
  @options
end

#paddingObject (readonly)

Returns the value of attribute padding.



32
33
34
# File 'lib/actiondoc/generator.rb', line 32

def padding
  @padding
end

Instance Method Details

#check_action_ymlObject



63
64
65
66
67
68
69
# File 'lib/actiondoc/generator.rb', line 63

def check_action_yml
  @path_to_action_yml = @args.first || 'action.yml'
  return if File.readable?(@path_to_action_yml)

  warn "#{@path_to_action_yml} not found! Aborting..."
  exit 1
end

#construct_inputs_table(action) ⇒ Object



85
86
87
88
89
# File 'lib/actiondoc/generator.rb', line 85

def construct_inputs_table(action)
  return unless action.inputs && !action.inputs.empty?

  TableLayouts::Nice.new(Input.members_as_headers, action.inputs).layout
end

#read_action_ymlObject



71
72
73
74
75
76
77
# File 'lib/actiondoc/generator.rb', line 71

def read_action_yml
  yaml = YAML.safe_load(File.read(@path_to_action_yml)).deep_symbolize_keys
  inputs = yaml.fetch(:inputs, {}).map do |k, v|
    Input.new(k, v[:description].chomp, v[:required] ? 'Required' : 'No', v[:default])
  end
  Action.new(yaml[:name], yaml[:description].chomp, inputs)
end

#read_template(action) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/actiondoc/generator.rb', line 48

def read_template(action)
  options_template = options[:template]
  if options_template
    unless File.readable?(options_template)
      warn "Template file #{options_template} not found! Aborting..."
      exit 1
    end
    ERB.new(File.read(options_template), trim_mode: '-')
  elsif action.inputs && !action.inputs.empty?
    ERB.new(DEFAULT_TEMPLATE, trim_mode: '-')
  else
    ERB.new(TEMPLATE_WITHOUT_INPUTS, trim_mode: '-')
  end
end

#render_template(template, action) ⇒ Object



79
80
81
82
83
# File 'lib/actiondoc/generator.rb', line 79

def render_template(template, action)
  inputs_table = construct_inputs_table(action)
  model = TemplateModel.new(action, inputs_table)
  puts template.result(model.instance_eval { binding })
end

#runObject



41
42
43
44
45
46
# File 'lib/actiondoc/generator.rb', line 41

def run
  check_action_yml
  action = read_action_yml
  template = read_template(action)
  render_template(template, action)
end