class Zapata::Printer

Public Class Methods

print(raw, args: false) click to toggle source
to_var_name(name) click to toggle source
# File lib/zapata/printer.rb, line 39
def to_var_name(name)
  name.to_s.split('::').last.underscore.delete('@')
end

Private Class Methods

all_keys_symbols?(hash) click to toggle source
# File lib/zapata/printer.rb, line 98
def all_keys_symbols?(hash)
  hash.keys.all? do |key|
    Parser::CurrentRuby.parse(key.to_s).type == :sym
  end
end
argize(value, type) click to toggle source
# File lib/zapata/printer.rb, line 70
def argize(value, type)
  case type
  when :array
    value = value[1...-1]
  when :hash
    value = value[2...-2]
  end

  return unless value.present?

  "(#{[value].flatten.join(', ')})"
end
array(given) click to toggle source
# File lib/zapata/printer.rb, line 45
def array(given)
  unnested = given.map { |el| unnest(el) }

  "[#{unnested.join(', ')}]"
end
hash(given) click to toggle source
# File lib/zapata/printer.rb, line 83
def hash(given)
  unnested = given.each_with_object({}) do |(key, val), obj|
    obj[unnest(key)] = unnest(val)
  end

  values = unnested.map do |key, val|
    print_hash_pair(key, val, all_keys_symbols?(unnested))
  end
  "{ #{values.join(', ')} }"
end
ivar(raw) click to toggle source
# File lib/zapata/printer.rb, line 61
def ivar(raw)
  RZpec::Writer.ivars << raw
  to_var_name(raw.value)
end
missing(raw) click to toggle source
# File lib/zapata/printer.rb, line 66
def missing(raw)
  print(Primitive::Raw.new(:str, "Missing \"#{raw.value}\""))
end
print_hash_pair(key, val, symbol_keys) click to toggle source
str(raw) click to toggle source
# File lib/zapata/printer.rb, line 51
def str(raw)
  # decide which one to use
  # "\"#{raw.value}\""
  "'#{raw.value}'"
end
sym(raw) click to toggle source
# File lib/zapata/printer.rb, line 57
def sym(raw)
  ":#{raw.value}"
end
unnest(raw) click to toggle source
# File lib/zapata/printer.rb, line 105
def unnest(raw)
  return raw unless raw.respond_to?(:value)

  if raw.value.is_a?(Primitive::Raw)
    print(unnest(raw.value))
  else
    print(raw)
  end
end