Module: Tap::Support::Templater::Utils
- Included in:
- Tap::Support::Templater
- Defined in:
- lib/tap/support/templater.rb
Overview
Utility methods for Templater; mostly string manipulations useful in creating documentation.
Instance Method Summary collapse
-
#comment(str) ⇒ Object
Comments out the string.
-
#module_nest(const_name, indent = " ", line_sep = "\n") ⇒ Object
Nest the return of the block in the nesting module.
-
#nest(nesting, indent = " ", line_sep = "\n") ⇒ Object
Nest the return of the block in the nesting lines.
-
#yamlize(object) ⇒ Object
yamlize converts the object to YAML (using to_yaml), omitting the header and final newline:.
Instance Method Details
#comment(str) ⇒ Object
Comments out the string.
69 70 71 |
# File 'lib/tap/support/templater.rb', line 69 def comment(str) str.split("\n").collect {|line| "# #{line}" }.join("\n") end |
#module_nest(const_name, indent = " ", line_sep = "\n") ⇒ Object
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
113 114 115 116 117 118 119 |
# File 'lib/tap/support/templater.rb', line 113 def module_nest(const_name, indent=" ", line_sep="\n") nesting = const_name.split(/::/).collect do |name| ["module #{name}", "end"] end nest(nesting, indent, line_sep) { yield } end |
#nest(nesting, indent = " ", line_sep = "\n") ⇒ Object
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
# }
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/tap/support/templater.rb', line 85 def nest(nesting, indent=" ", line_sep="\n") content = yield return content if nesting.empty? depth = nesting.length lines = [indent * depth + content.gsub(/#{line_sep}/, line_sep + indent * depth)] nesting.reverse_each do |(start_line, end_line)| depth -= 1 lines.unshift(indent * depth + start_line) lines << (indent * depth + end_line) end lines.join(line_sep) end |
#yamlize(object) ⇒ Object
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"
64 65 66 |
# File 'lib/tap/support/templater.rb', line 64 def yamlize(object) object == nil ? "~" : YAML.dump(object)[4...-1].strip end |