class SimpleParams::ValidationMatchers::CoercionMatcher

Attributes

attribute[RW]

Public Class Methods

new(attribute) click to toggle source
Calls superclass method
# File lib/simple_params/validation_matchers/coercion_matcher.rb, line 10
def initialize(attribute)
  super(attribute)
  @attribute = attribute
end

Public Instance Methods

description() click to toggle source
# File lib/simple_params/validation_matchers/coercion_matcher.rb, line 69
def description
  "Expect #{@attribute} to coerce into #{@expected_coerce}"
end
failure_message_for_should() click to toggle source
# 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_not() click to toggle source
# 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) click to toggle source
# File lib/simple_params/validation_matchers/coercion_matcher.rb, line 15
def into(value)
  @expected_coerce = value
  self
end
matches?(subject) click to toggle source
Calls superclass method
# 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

Private Instance Methods

class_matches(target, comparison) click to toggle source
# File lib/simple_params/validation_matchers/coercion_matcher.rb, line 82
def class_matches(target, comparison)
  target.class.name.to_s == comparison.name.to_s
end