class DataConverter
Constants
- ESCAPE_REGEXP
- ESCAPE_VALUES
- VERSION
Attributes
data[R]
Public Class Methods
new(data)
click to toggle source
# File lib/data-converter.rb, line 13 def initialize(data) @data = data end
parse(data)
click to toggle source
# File lib/data-converter.rb, line 17 def self.parse(data) constructor = self.new(data) constructor.convert end
Public Instance Methods
convert(data = nil)
click to toggle source
# File lib/data-converter.rb, line 22 def convert(data = nil) data = @data unless data case data when Array tag(:ul, convert_to_li(data), :escaped => true) when Hash tag(:dl, convert_to_dl_childs(data), :escaped => true) else data end end
Private Instance Methods
check_data?(data)
click to toggle source
# File lib/data-converter.rb, line 37 def check_data?(data) data.is_a?(Array) or data.is_a?(Hash) end
convert_to_dl_childs(data)
click to toggle source
# File lib/data-converter.rb, line 51 def convert_to_dl_childs(data) childs = [] data.each_pair do | key, value | childs << tag(:dt, key) if check_data?(value) childs << tag(:dd, convert(value), :escaped => true) else childs << tag(:dd, value) end end childs.join("\n") end
convert_to_li(data)
click to toggle source
# File lib/data-converter.rb, line 41 def convert_to_li(data) data.map do |caption| if check_data?(caption) tag(:li, convert(caption), :escaped => true) else tag(:li, caption) end end.join("\n") end
escape_html(str)
click to toggle source
# File lib/data-converter.rb, line 69 def escape_html(str) str.to_s.gsub(ESCAPE_REGEXP) { |char| ESCAPE_VALUES[char] } end
tag(name, caption, options = {})
click to toggle source
# File lib/data-converter.rb, line 64 def tag(name, caption, options = {}) caption = caption.to_s "<#{name}>#{options[:escaped] ? caption : escape_html(caption)}</#{name}>" end