class DataFormatter::Value

Attributes

indentation[R]
is_key[R]
lang[R]
value[R]

Public Class Methods

new(args) click to toggle source
# File lib/data_formatter/value.rb, line 5
def initialize(args)
  @value = args.fetch(:data)
  @is_key = args.fetch(:is_key, false)
  @indentation = args.fetch(:indentation)
  @lang = args.fetch(:lang, "ruby")
end

Public Instance Methods

to_s() click to toggle source
# File lib/data_formatter/value.rb, line 12
def to_s
  if is?(Numeric)
    format_number
  elsif is?(String)
    format_string
  elsif is?(Symbol)
    format_symbol
  elsif is?(TrueClass, FalseClass)
    format_boolean
  elsif is?(NilClass)
    format_nil
  elsif is?(Hash)
    format_hash
  elsif is?(Array)
    format_array
  else
    raise "ValueFormatter value is of unsupported type: #{ value.class.name }"
  end.to_s
end

Protected Instance Methods

format_array() click to toggle source
# File lib/data_formatter/value.rb, line 65
def format_array
  ArrayCollection.new(data: value, indentation: indentation, lang: lang)
end
format_boolean() click to toggle source
# File lib/data_formatter/value.rb, line 53
def format_boolean
  mark_up(data: value ? "true" : "false", kind: "boolean" )
end
format_hash() click to toggle source
# File lib/data_formatter/value.rb, line 61
def format_hash
  HashCollection.new(data: value, indentation: indentation, lang: lang)
end
format_nil() click to toggle source
# File lib/data_formatter/value.rb, line 57
def format_nil
  mark_up(data: (lang == "js" ? "null" : "nil"), kind: "nil" )
end
format_number() click to toggle source
# File lib/data_formatter/value.rb, line 38
def format_number
  mark_up(data: value, kind: "number" )
end
format_string() click to toggle source
# File lib/data_formatter/value.rb, line 46
def format_string
  escaped_value = { "\a" => '\a', "\b" => '\b', "\r" => '\r', "\n" => '\n', "\t" => '\t' }.inject(value) do |memo, pair|
    memo.gsub(pair[0], pair[1])
  end
  mark_up(data: escaped_value, surround: '"', kind: "string" )
end
format_symbol() click to toggle source
# File lib/data_formatter/value.rb, line 42
def format_symbol
  mark_up(data: value.inspect, kind: "symbol" )
end
is?(*klass) click to toggle source
# File lib/data_formatter/value.rb, line 34
def is?(*klass)
  klass.any? { |k| value.kind_of?(k) }
end
mark_up(args) click to toggle source
# File lib/data_formatter/value.rb, line 69
def mark_up(args)
  Tag.new(content: args.fetch(:data), surround: args[:surround], css_class: [is_key ? "key" : nil,  args[:kind]]).to_s
end