Module: Tap::Templater::Utils
- Included in:
- Tap::Templater
- Defined in:
- lib/tap/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.
- #indent(indent, str) ⇒ Object
-
#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.
68 69 70 |
# File 'lib/tap/templater.rb', line 68 def comment(str) str.split("\n").collect {|line| "# #{line}" }.join("\n") end |
#indent(indent, str) ⇒ Object
120 121 122 123 124 |
# File 'lib/tap/templater.rb', line 120 def indent(indent, str) lines = str.kind_of?(Array) ? str : str.split("\n") lines.collect! {|line| "#{indent}#{line}" } lines.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
112 113 114 115 116 117 118 |
# File 'lib/tap/templater.rb', line 112 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
# }
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/tap/templater.rb', line 84 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"
63 64 65 |
# File 'lib/tap/templater.rb', line 63 def yamlize(object) object == nil ? "~" : YAML.dump(object)[4...-1].strip end |