Class: ValidatesTimeliness::Validator

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

Constant Summary collapse

RESTRICTION_METHODS =
{
  :equal_to     => :==,
  :before       => :<, 
  :after        => :>, 
  :on_or_before => :<=,
  :on_or_after  => :>=,
  :between      => lambda {|v, r| (r.first..r.last).include?(v) } 
}
VALID_OPTIONS =
[
  :on, :if, :unless, :allow_nil, :empty, :allow_blank, :blank,
  :with_time, :with_date, :ignore_usec,
  :invalid_time_message, :invalid_date_message, :invalid_datetime_message
] + RESTRICTION_METHODS.keys.map {|option| [option, "#{option}_message".to_sym] }.flatten

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Validator

Returns a new instance of Validator.



31
32
33
34
35
36
# File 'lib/validates_timeliness/validator.rb', line 31

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

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



29
30
31
# File 'lib/validates_timeliness/validator.rb', line 29

def configuration
  @configuration
end

#typeObject (readonly)

Returns the value of attribute type.



29
30
31
# File 'lib/validates_timeliness/validator.rb', line 29

def type
  @type
end

Class Method Details

.evaluate_option_value(value, type, record) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/validates_timeliness/validator.rb', line 159

def evaluate_option_value(value, type, record)
  case value
  when Time, Date, DateTime
    value
  when Symbol
    evaluate_option_value(record.send(value), type, record)
  when Proc
    evaluate_option_value(value.call(record), type, record)
  when Array
    value.map {|r| evaluate_option_value(r, type, record) }.sort
  when Range
    evaluate_option_value([value.first, value.last], type, record)
  else
    record.class.parse_date_time(value, type, false)
  end
end

.type_cast_value(value, type, ignore_usec = false) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/validates_timeliness/validator.rb', line 176

def type_cast_value(value, type, ignore_usec=false)
  if value.is_a?(Array)
    value.map {|v| type_cast_value(v, type, ignore_usec) }
  else
    value = case type
    when :time
      value.to_dummy_time
    when :date
      value.to_date
    when :datetime
      if value.is_a?(DateTime) || value.is_a?(Time)
        value.to_time
      else
        value.to_time(ValidatesTimeliness.default_timezone)
      end
    else
      nil
    end
    if ignore_usec && value.is_a?(Time)
      ::ActiveRecord::Base.send(:make_time, Array(value).reverse[4..9])
    else
      value
    end
  end
end

Instance Method Details

#call(record, attr_name, value) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/validates_timeliness/validator.rb', line 38

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

  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