Module Tap::Templater::Utils
In: lib/tap/templater.rb

Utility methods for Templater; mostly string manipulations useful in creating documentation.

Methods

comment   indent   module_nest   nest   yamlize  

Public Instance methods

Comments out the string.

[Source]

    # File lib/tap/templater.rb, line 68
68:       def comment(str)
69:         str.split("\n").collect {|line| "# #{line}" }.join("\n")
70:       end

[Source]

     # File lib/tap/templater.rb, line 120
120:       def indent(indent, str)
121:         lines = str.kind_of?(Array) ? str : str.split("\n")
122:         lines.collect! {|line| "#{indent}#{line}" }
123:         lines.join("\n")
124:       end

Nest the return of the block in the nesting module.

 module_nest('Some::Nested') { "class Const\nend" }
 # => %Q{
 # module Some
 #   module Nested
 #     class Const
 #     end
 #   end
 # end
 # }.strip

[Source]

     # File lib/tap/templater.rb, line 112
112:       def module_nest(const_name, indent="  ", line_sep="\n")
113:         nesting = const_name.split(/::/).collect do |name|
114:           ["module #{name}", "end"]
115:         end
116:         
117:         nest(nesting, indent, line_sep) { yield }
118:       end

Nest the return of the block in the nesting lines.

 nest([["\nmodule Some", "end\n"],["module Nested", "end"]]) { "class Const\nend" }
 # => %Q{
 # module Some
 #   module Nested
 #     class Const
 #     end
 #   end
 # end
 # }

[Source]

    # File lib/tap/templater.rb, line 84
84:       def nest(nesting, indent="  ", line_sep="\n")
85:         content = yield
86:         return content if nesting.empty?
87:         
88:         depth = nesting.length
89:         lines = [indent * depth + content.gsub(/#{line_sep}/, line_sep + indent * depth)]
90: 
91:         nesting.reverse_each do |(start_line, end_line)|
92:           depth -= 1
93:           lines.unshift(indent * depth + start_line)
94:           lines << (indent * depth + end_line)
95:         end
96: 
97:         lines.join(line_sep)
98:       end

yamlize converts the object to YAML (using to_yaml), omitting the header and final newline:

  {'key' => 'value'}.to_yaml           # => "--- \nkey: value\n"
  yamlize {'key' => 'value'}           # => "key: value"

[Source]

    # File lib/tap/templater.rb, line 63
63:       def yamlize(object)
64:         object == nil ? "~" : YAML.dump(object)[4...-1].strip
65:       end

[Validate]