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

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

Constant Summary collapse

@@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'}
}

Instance Method Summary collapse

Constructor Details

#initialize(attribute, options) ⇒ ValidateTimeliness

Returns a new instance of ValidateTimeliness.



13
14
15
16
17
# File 'lib/validates_timeliness/spec/rails/matchers/validate_timeliness.rb', line 13

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

Instance Method Details

#compile_error_messagesObject



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

def compile_error_messages
  messages = validator.send(:error_messages)
  @messages = messages.inject({}) {|h, (k, v)| h[k] = v.gsub(/ (\%s|\{\{\w*\}\})/, ''); h }
end

#descriptionObject



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

def description
  "have validated #{options[:type]} attribute #{expected.inspect}"
end

#failure_messageObject



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

def failure_message
  "expected model to validate #{options[:type]} attribute #{expected.inspect} with #{last_failure}"
end

#matches?(record) ⇒ Boolean

Returns:

  • (Boolean)


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

def matches?(record)
  @record = record
  type = options[:type]
  
  invalid_value = @@test_values[type][:fail]
  valid_value   = parse_and_cast(@@test_values[type][:pass])
  valid = error_matching(invalid_value, /#{messages["invalid_#{type}".to_sym]}/) &&
      no_error_matching(valid_value, /#{messages["invalid_#{type}".to_sym]}/)

  valid = test_option(:before, :-) if options[:before] && valid
  valid = test_option(:after, :+) if options[:after] && valid
  
  valid = test_option(:on_or_before, :+, :modify_on => :invalid) if options[:on_or_before] && valid
  valid = test_option(:on_or_after, :-, :modify_on => :invalid) if options[:on_or_after] && valid

  return valid
end

#negative_failure_messageObject



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

def negative_failure_message
  "expected not to validate #{options[:type]} attribute #{expected.inspect}"
end