Class: OpenAPISourceTools::Output
- Inherits:
-
Object
- Object
- OpenAPISourceTools::Output
- Defined in:
- lib/openapi/sourcetools/output.rb
Overview
Output indentation helper class. Exposed as Gen.output for use from templates.
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#last_indent ⇒ Object
Returns the value of attribute last_indent.
Instance Method Summary collapse
-
#initialize(cfg = OutputConfiguration.new) ⇒ Output
constructor
A new instance of Output.
-
#join(blocks, separator = "\n") ⇒ Object
Takes an array of code blocks/lines or integers/booleans and produces indented output using the separator character.
- #json(something) ⇒ Object
- #pretty_json(something) ⇒ Object
- #yaml(something) ⇒ Object
Constructor Details
#initialize(cfg = OutputConfiguration.new) ⇒ Output
Returns a new instance of Output.
37 38 39 40 |
# File 'lib/openapi/sourcetools/output.rb', line 37 def initialize(cfg = OutputConfiguration.new) @config = cfg @last_indent = 0 end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
34 35 36 |
# File 'lib/openapi/sourcetools/output.rb', line 34 def config @config end |
#last_indent ⇒ Object
Returns the value of attribute last_indent.
35 36 37 |
# File 'lib/openapi/sourcetools/output.rb', line 35 def last_indent @last_indent end |
Instance Method Details
#join(blocks, separator = "\n") ⇒ Object
Takes an array of code blocks/lines or integers/booleans and produces indented output using the separator character. Set class attributes to obtain desired outcome.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/openapi/sourcetools/output.rb', line 52 def join(blocks, separator = "\n") indented = [] blocks.flatten! indent = 0 blocks.each do |block| if block.nil? next elsif block.is_a?(Integer) indent += block indent = 0 if indent.negative? elsif block.is_a?(TrueClass) indent += @config.indent_step elsif block.is_a?(FalseClass) indent -= @config.indent_step indent = 0 if indent.negative? else block = block.to_s unless block.is_a?(String) if block.empty? indented.push('') next end if indent.zero? indented.push(block) next end if @config.tab_replaces_count.positive? tabs = @config.tab * (indent / @config.tab_replaces_count) chars = @config.indent_character * (indent % @config.tab_replaces_count) else tabs = '' chars = @config.indent_character * indent end lines = block.lines(chomp: true) lines.each do |line| indented.push("#{tabs}#{chars}#{line}") end end end @last_indent = indent indented.join(separator) end |
#json(something) ⇒ Object
98 99 100 |
# File 'lib/openapi/sourcetools/output.rb', line 98 def json(something) JSON.generate(something) end |
#pretty_json(something) ⇒ Object
102 103 104 |
# File 'lib/openapi/sourcetools/output.rb', line 102 def pretty_json(something) JSON.generate(something, @config.prettified_json) end |
#yaml(something) ⇒ Object
94 95 96 |
# File 'lib/openapi/sourcetools/output.rb', line 94 def yaml(something) YAML.dump(something) end |