Class: Spec::Rails::Matchers::ValidateTimeliness

Inherits:
Object
  • Object
show all
Defined in:
lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb

Constant Summary collapse

VALIDITY_TEST_VALUES =
{
  :date     => {:pass => '2000-01-01', :fail => '2000-01-32'},
  :time     => {:pass => '12:00',      :fail => '25:00'},
  :datetime => {:pass => '2000-01-01 00:00:00', :fail => '2000-01-32 00:00:00'}
}
OPTION_TEST_SETTINGS =
{
  :equal_to     => { :method => :+, :modify_on => :invalid },
  :before       => { :method => :-, :modify_on => :valid },
  :after        => { :method => :+, :modify_on => :valid },
  :on_or_before => { :method => :+, :modify_on => :invalid },
  :on_or_after  => { :method => :-, :modify_on => :invalid }
}

Instance Method Summary collapse

Constructor Details

#initialize(attribute, options) ⇒ ValidateTimeliness

Returns a new instance of ValidateTimeliness.



20
21
22
23
# File 'lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb', line 20

def initialize(attribute, options)
  @expected, @options = attribute, options
  @validator = ValidatesTimeliness::Validator.new(options)
end

Instance Method Details

#descriptionObject



49
50
51
# File 'lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb', line 49

def description
  "have validated #{@type} attribute #{@expected.inspect}"
end

#failure_messageObject



41
42
43
# File 'lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb', line 41

def failure_message
  "expected model to validate #{@type} attribute #{@expected.inspect} with #{@last_failure}"
end

#matches?(record) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb', line 25

def matches?(record)
  @record = record
  @type = @options[:type]
  
  valid = test_validity

  valid = test_option(:equal_to)     if valid && @options[:equal_to]
  valid = test_option(:before)       if valid && @options[:before]
  valid = test_option(:after)        if valid && @options[:after]
  valid = test_option(:on_or_before) if valid && @options[:on_or_before]
  valid = test_option(:on_or_after)  if valid && @options[:on_or_after]
  valid = test_between               if valid && @options[:between]

  return valid
end

#negative_failure_messageObject



45
46
47
# File 'lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb', line 45

def negative_failure_message
  "expected not to validate #{@type} attribute #{@expected.inspect}"
end