Class: SimpleParams::ValidationMatchers::CoercionMatcher

Inherits:
ValidationMatcher
  • Object
show all
Defined in:
lib/simple_params/validation_matchers/coercion_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ CoercionMatcher



10
11
12
13
# File 'lib/simple_params/validation_matchers/coercion_matcher.rb', line 10

def initialize(attribute)
  super(attribute)
  @attribute = attribute
end

Instance Attribute Details

#attributeObject

Returns the value of attribute attribute.



8
9
10
# File 'lib/simple_params/validation_matchers/coercion_matcher.rb', line 8

def attribute
  @attribute
end

Instance Method Details

#descriptionObject



69
70
71
# File 'lib/simple_params/validation_matchers/coercion_matcher.rb', line 69

def description
  "Expect #{@attribute} to coerce into #{@expected_coerce}"
end

#failure_message_for_shouldObject



73
74
75
# File 'lib/simple_params/validation_matchers/coercion_matcher.rb', line 73

def failure_message_for_should
  "Expect #{@attribute} to coerce into #{@expected_coerce}"
end

#failure_message_for_should_notObject



77
78
79
# File 'lib/simple_params/validation_matchers/coercion_matcher.rb', line 77

def failure_message_for_should_not
  "Expect #{@attribute} to not coerce into #{@expected_coerce}"
end

#into(value) ⇒ Object



15
16
17
18
# File 'lib/simple_params/validation_matchers/coercion_matcher.rb', line 15

def into(value)
  @expected_coerce = value
  self
end

#matches?(subject) ⇒ Boolean



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/simple_params/validation_matchers/coercion_matcher.rb', line 20

def matches?(subject)
  super(subject)
  
  case @expected_coerce
  when :integer
    @subject.send("#{@attribute}=", "100.02")
    (@subject.send(attribute) == 100) &&
    @subject.send("raw_#{attribute}").is_a?(TYPE_MAPPINGS[:integer])
  when :string
    @subject.send("#{@attribute}=", 200)
    (@subject.send(attribute) == "200") &&
    class_matches(@subject.send("raw_#{attribute}"), TYPE_MAPPINGS[:string])
  when :decimal
    @subject.send("#{@attribute}=", "100")
    (@subject.send(attribute) == 100.0) &&
    class_matches(@subject.send("raw_#{attribute}"), TYPE_MAPPINGS[:decimal])
  when :datetime 
    @subject.send("#{@attribute}=", DateTime.new(2014,2,3))
    class_matches(@subject.send("raw_#{attribute}"), TYPE_MAPPINGS[:datetime])
  when :date
    @subject.send("#{@attribute}=", DateTime.new(2014,2,3,12,0,0))
    class_matches(@subject.send("raw_#{attribute}"), TYPE_MAPPINGS[:date])
  when :time
    @subject.send("#{@attribute}=", Time.new(2007,11,5,11,21,0, "-05:00"))
    class_matches(@subject.send("raw_#{attribute}"), TYPE_MAPPINGS[:time])
  when :float
    @subject.send("#{@attribute}=", "20")
    (@subject.send("raw_#{attribute}") == 20.0) &&
    class_matches(@subject.send("raw_#{attribute}"), TYPE_MAPPINGS[:float])
  when :boolean
    @subject.send("#{@attribute}=", 0)
    (@subject.send(attribute) == false) &&
    (@subject.send("raw_#{attribute}").is_a?(TrueClass) || @subject.send("raw_#{attribute}").is_a?(FalseClass))
  when :array
    @subject.send("#{@attribute}=", ["red, green, blue"])
    class_matches(@subject.send("raw_#{attribute}"), TYPE_MAPPINGS[:array])
  when :hash
    @subject.send("#{@attribute}=", { "dog"=>1, "cat"=>2, "fish"=>3 })
    class_matches(@subject.send("raw_#{attribute}"), TYPE_MAPPINGS[:hash])
  when :object
    @subject.send("#{@attribute}=", "programmer")
    @subject.send("raw_#{attribute}").is_a?(TYPE_MAPPINGS[:object])
  else
    false
  end


end