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.
Constructor Details
#initialize(cfg = OutputConfiguration.new) ⇒ Output
Returns a new instance of Output.
28 29 30 31 |
# File 'lib/openapi/sourcetools/output.rb', line 28 def initialize(cfg = OutputConfiguration.new) @config = cfg @last_indent = 0 end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
25 26 27 |
# File 'lib/openapi/sourcetools/output.rb', line 25 def config @config end |
#last_indent ⇒ Object
Returns the value of attribute last_indent.
26 27 28 |
# File 'lib/openapi/sourcetools/output.rb', line 26 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.
43 44 45 46 47 48 49 50 51 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 |
# File 'lib/openapi/sourcetools/output.rb', line 43 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 |