Class: Shoulda::Lotus::Matchers::CoerceAttributeMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/lotus/matchers/coerce_attribute_matcher.rb

Constant Summary collapse

TYPES =
{
  'Array'      => [1, [1]],
  'BigDecimal' => ['x', nil],
  'Boolean'    => ['0303', false],
  'Date'       => [false, nil],
  'DateTime'   => ['x', nil],
  'Float'      => ['x', nil],
  'Hash'       => ['รง', nil],
  'Integer'    => ['x', nil],
  'Pathname'   => [true, nil],
  'Set'        => [nil, Set.new],
  'String'     => [1, '1'],
  'Symbol'     => [Hash.new, nil],
  'Time'       => ['uy', nil],
}

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ CoerceAttributeMatcher



25
26
27
# File 'lib/shoulda/lotus/matchers/coerce_attribute_matcher.rb', line 25

def initialize(attribute)
  @attribute = attribute
end

Instance Method Details

#descriptionObject



34
35
36
# File 'lib/shoulda/lotus/matchers/coerce_attribute_matcher.rb', line 34

def description
  "coerce '#{@attribute}' to '#{@type}'"
end

#failure_messageObject



38
39
40
# File 'lib/shoulda/lotus/matchers/coerce_attribute_matcher.rb', line 38

def failure_message
  "does coerce '#{@attribute}' to '#{@type}'"
end

#failure_message_when_negatedObject



42
43
44
# File 'lib/shoulda/lotus/matchers/coerce_attribute_matcher.rb', line 42

def failure_message_when_negated
  "does not coerce '#{@attribute}' to '#{@type}'"
end

#matches?(target) ⇒ Boolean



29
30
31
32
# File 'lib/shoulda/lotus/matchers/coerce_attribute_matcher.rb', line 29

def matches?(target)
  target.send("#{@attribute}=", TYPES[@type.to_s].first)
  target.send(@attribute) == TYPES[@type.to_s].last
end

#to(type) ⇒ Object



46
47
48
49
# File 'lib/shoulda/lotus/matchers/coerce_attribute_matcher.rb', line 46

def to(type)
  @type = type
  self
end