Class: ValidatesTimeliness::Validator
- Inherits:
-
Object
- Object
- ValidatesTimeliness::Validator
- Defined in:
- lib/validates_timeliness/validator.rb
Constant Summary collapse
- RESTRICTION_METHODS =
{ :is_at => :==, :equal_to => :==, :before => :<, :after => :>, :on_or_before => :<=, :on_or_after => :>=, :between => lambda {|v, r| (r.first..r.last).include?(v) } }
- VALID_OPTION_KEYS =
[ :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
- DEFAULT_OPTIONS =
{ :on => :save, :type => :datetime, :allow_nil => false, :allow_blank => false, :ignore_usec => false }
Instance Attribute Summary collapse
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .dummy_time(value) ⇒ Object
- .error_value_format_for(type) ⇒ Object
- .evaluate_option_value(value, type, record) ⇒ Object
- .type_cast_value(value, type, ignore_usec = false) ⇒ Object
Instance Method Summary collapse
- #call(record, attr_name, value) ⇒ Object
- #error_messages ⇒ Object
-
#initialize(configuration) ⇒ Validator
constructor
A new instance of Validator.
Constructor Details
#initialize(configuration) ⇒ Validator
Returns a new instance of Validator.
29 30 31 32 33 |
# File 'lib/validates_timeliness/validator.rb', line 29 def initialize(configuration) @configuration = DEFAULT_OPTIONS.merge(configuration) @type = @configuration.delete(:type) (@configuration) end |
Instance Attribute Details
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
27 28 29 |
# File 'lib/validates_timeliness/validator.rb', line 27 def configuration @configuration end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
27 28 29 |
# File 'lib/validates_timeliness/validator.rb', line 27 def type @type end |
Class Method Details
.dummy_time(value) ⇒ Object
217 218 219 220 221 222 223 224 225 |
# File 'lib/validates_timeliness/validator.rb', line 217 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_format_for(type) ⇒ Object
164 165 166 167 168 169 170 171 |
# File 'lib/validates_timeliness/validator.rb', line 164 def error_value_format_for(type) if defined?(I18n) # work around for syntax check in vendored I18n for Rails <= 2.3.3 I18n.t('validates_timeliness.error_value_formats')[type] || error_value_formats[type] else error_value_formats[type] end end |
.evaluate_option_value(value, type, record) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/validates_timeliness/validator.rb', line 173 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
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/validates_timeliness/validator.rb', line 191 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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/validates_timeliness/validator.rb', line 35 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_messages ⇒ Object
50 51 52 |
# File 'lib/validates_timeliness/validator.rb', line 50 def @error_messages ||= ::ActiveRecord::Errors..merge() end |