Class: GreenPepper::TypeConverter

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/greenpepper/converter.rb

Instance Method Summary collapse

Constructor Details

#initializeTypeConverter

Returns a new instance of TypeConverter.



15
16
17
# File 'lib/greenpepper/converter.rb', line 15

def initialize
  @converters = []
end

Instance Method Details

#add_conversion_type(type) ⇒ Object



64
65
66
# File 'lib/greenpepper/converter.rb', line 64

def add_conversion_type(type)
   @converters << type 
end

#auto_convert_object(fixture, name, expected_format, object) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/greenpepper/converter.rb', line 55

def auto_convert_object(fixture, name, expected_format, object)
  format_f = format_func name
  if fixture.respond_to?(format_f)
    return fixture.send(format_f, object)
  else
    return format_object(expected_format, object)
  end
end

#auto_convert_string(fixture, name, string) ⇒ Object

Encapsulate all the type conversion logic for fixtures



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/greenpepper/converter.rb', line 38

def auto_convert_string(fixture, name, string)
  parse_f = conversion_func name
  if fixture.respond_to?(parse_f)
    if string.is_a? Array
      return fixture.send(parse_f, *string)
    else
      return fixture.send(parse_f, string)
    end
  else
    if string.is_a? Array
      return string.collect{|s| convert_string s}
    else
      return convert_string(string)
    end
  end
end

#convert_string(string) ⇒ Object



19
20
21
22
23
24
25
26
# File 'lib/greenpepper/converter.rb', line 19

def convert_string(string)
  @converters.each { |type|
    if type.can_parse? string
      return type.convert.call(string)
    end
  }
  string
end

#format_object(expected_string_format, object) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/greenpepper/converter.rb', line 28

def format_object(expected_string_format, object)
  @converters.each { |type|
    if type.can_parse? expected_string_format
      return type.format.call(object, expected_string_format)
    end
  }
  object.to_s
end