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,
  :with_time, :with_date, :ignore_usec, :format,
  :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.


24
25
26
27
28
29
# File 'lib/validates_timeliness/validator.rb', line 24

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


22
23
24
# File 'lib/validates_timeliness/validator.rb', line 22

def configuration
  @configuration
end

#typeObject (readonly)

Returns the value of attribute type


22
23
24
# File 'lib/validates_timeliness/validator.rb', line 22

def type
  @type
end

Class Method Details

.default_error_messagesObject


156
157
158
159
160
161
162
# File 'lib/validates_timeliness/validator.rb', line 156

def default_error_messages
  if defined?(I18n)
    I18n.t('activerecord.errors.messages')
  else
    ::ActiveRecord::Errors.default_error_messages
  end
end

.dummy_time(value) ⇒ Object


220
221
222
223
224
225
226
227
228
# File 'lib/validates_timeliness/validator.rb', line 220

def dummy_time(value)
  if value.is_a?(Time) || value.is_a?(DateTime)
    time = [value.hour, value.min, value.sec]
  else
    time = [0,0,0]
  end
  dummy_date = ValidatesTimeliness::Formats.dummy_date_for_time_type
  ValidatesTimeliness::Parser.make_time(dummy_date + time)
end

.error_value_formatsObject


164
165
166
167
168
169
170
# File 'lib/validates_timeliness/validator.rb', line 164

def error_value_formats
  if defined?(I18n)
    I18n.t('validates_timeliness.error_value_formats')
  else
    @@error_value_formats
  end
end

.error_value_formats=(formats) ⇒ Object


172
173
174
# File 'lib/validates_timeliness/validator.rb', line 172

def error_value_formats=(formats)
  @@error_value_formats = formats
end

.evaluate_option_value(value, type, record) ⇒ Object


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/validates_timeliness/validator.rb', line 176

def evaluate_option_value(value, type, record)
  case value
  when Time, Date
    value
  when Symbol
    evaluate_option_value(record.send(value), type, record)
  when Proc
    result = value.arity > 0 ? value.call(record) : value.call
    evaluate_option_value(result, 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
    ValidatesTimeliness::Parser.parse(value, type, :strict => false)
  end
end

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


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/validates_timeliness/validator.rb', line 194

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
      dummy_time(value)
    when :date
      value.to_date
    when :datetime
      if value.is_a?(Time) || value.is_a?(DateTime)
        value.to_time
      else
        value.to_time(ValidatesTimeliness.default_timezone)
      end
    else
      nil
    end
    if ignore_usec && value.is_a?(Time)
      ValidatesTimeliness::Parser.make_time(Array(value).reverse[4..9])
    else
      value
    end
  end
end

Instance Method Details

#call(record, attr_name, value) ⇒ Object


31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/validates_timeliness/validator.rb', line 31

def call(record, attr_name, value)
  raw_value = raw_value(record, attr_name) || value

  if value.is_a?(String) || configuration[:format]
    value = ValidatesTimeliness::Parser.parse(raw_value, type, :strict => false, :format => configuration[:format])
  end

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

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

  validate_restrictions(record, attr_name, value)
end

#error_messagesObject


46
47
48
# File 'lib/validates_timeliness/validator.rb', line 46

def error_messages
  @error_messages ||= self.class.default_error_messages.merge(custom_error_messages)
end