class ToFactory::Generation::Attribute

Attributes

parser[W]

Public Class Methods

new(attribute, value) click to toggle source
# File lib/to_factory/generation/attribute.rb, line 6
def initialize(attribute, value)
  @attribute = attribute
  @value = value
end

Public Instance Methods

format(value, nested=false) click to toggle source
# File lib/to_factory/generation/attribute.rb, line 26
def format(value, nested=false)
  case value
  when Time, DateTime
    inspect_time(value)
  when Date
    value.to_s.inspect
  when BigDecimal
    "BigDecimal.new(#{value.to_s.inspect})"
  when Hash
    inspect_hash(value, nested)
  when Array
    inspect_array(value, nested)
  when String
    validate_parseable!(value).inspect
  else
    value.inspect
  end
end
inspect_value(value, nested=false) click to toggle source
# File lib/to_factory/generation/attribute.rb, line 16
def inspect_value(value, nested=false)
  formatted = format(value, nested)

  if !value.is_a?(Hash) && !nested
    formatted = " #{formatted}"
  end

  formatted
end
to_s() click to toggle source
# File lib/to_factory/generation/attribute.rb, line 11
def to_s
  setter = "#{@attribute}#{inspect_value(@value)}"
  "    #{setter}"
end

Private Instance Methods

inspect_array(value, nested) click to toggle source
# File lib/to_factory/generation/attribute.rb, line 79
def inspect_array(value, nested)
  values = value.map{|v| format(v, nested)}.join(", ")
  "[#{values}]"
end
inspect_hash(value, nested) click to toggle source
# File lib/to_factory/generation/attribute.rb, line 67
def inspect_hash(value, nested)
  formatted = value.keys.inject([]) do |a, key|
    a << key_value_pair(key, value)
  end.join(', ')

  if nested
    "{#{formatted}}"
  else
    "({#{formatted}})"
  end
end
inspect_time(value) click to toggle source
# File lib/to_factory/generation/attribute.rb, line 61
def inspect_time(value)
  value = value.strftime("%Y-%m-%dT%H:%M %Z").inspect
  value.gsub! /GMT/, "UTC"
  value
end
key_value_pair(key, value) click to toggle source
# File lib/to_factory/generation/attribute.rb, line 84
def key_value_pair(key, value)
  formatted_key = inspect_value(key, true)
  formatted_value = inspect_value(value.fetch(key), true)
  "#{formatted_key} => #{formatted_value}"
end
parse(value) click to toggle source
# File lib/to_factory/generation/attribute.rb, line 53
def parse(value)
  @parser ||= RubyParser.new
  @parser.parse(value)
  true
rescue
  false
end
validate_parseable!(value) click to toggle source
# File lib/to_factory/generation/attribute.rb, line 47
def validate_parseable!(value)
  return value if parse(value)

  "ToFactory: RubyParser exception parsing this attribute"
end