Class: ValidatesTimeliness::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/validates_timeliness/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Validator

Returns a new instance of Validator.



16
17
18
19
20
# File 'lib/validates_timeliness/validator.rb', line 16

def initialize(configuration)
  defaults = { :on => :save, :type => :datetime, :allow_nil => false, :allow_blank => false }
  @configuration = defaults.merge(configuration)
  @type = @configuration.delete(:type)
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



14
15
16
# File 'lib/validates_timeliness/validator.rb', line 14

def configuration
  @configuration
end

#typeObject (readonly)

Returns the value of attribute type.



14
15
16
# File 'lib/validates_timeliness/validator.rb', line 14

def type
  @type
end

Instance Method Details

#call(record, attr_name) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/validates_timeliness/validator.rb', line 22

def call(record, attr_name)
  value     = record.send(attr_name)
  value     = record.class.parse_date_time(value, type, false) if value.is_a?(String)
  raw_value = raw_value(record, attr_name)

  return if (raw_value.nil? && configuration[:allow_nil]) || (raw_value.blank? && configuration[:allow_blank])

  add_error(record, attr_name, :blank) and return if raw_value.blank?
   
  add_error(record, attr_name, "invalid_#{type}".to_sym) and return unless value

  validate_restrictions(record, attr_name, value)
end