module ValidatesTimeliness::ORM::Mongoid::ClassMethods

You need define the fields before you define the validations. It is best to use the plugin parser to avoid errors on a bad field value in Mongoid. Parser will return nil rather than error.

Public Instance Methods

timeliness_attribute_type(attr_name) click to toggle source
# File lib/validates_timeliness/orm/mongoid.rb, line 25
def timeliness_attribute_type(attr_name)
  {
    Date => :date,
    Time => :time,
    DateTime => :datetime
  }[fields[database_field_name(attr_name)].type] || :datetime
end
timeliness_validation_for(attr_names, type) click to toggle source

Mongoid has no bulk attribute method definition hook. It defines them with each field definition. So we likewise define them after each validation is defined.

Calls superclass method
# File lib/validates_timeliness/orm/mongoid.rb, line 18
def timeliness_validation_for(attr_names, type)
  super
  attr_names.each do |attr_name|
    define_timeliness_write_method(attr_name)
  end
end

Protected Instance Methods

define_timeliness_write_method(attr_name) click to toggle source
# File lib/validates_timeliness/orm/mongoid.rb, line 41
        def define_timeliness_write_method(attr_name)
          generated_timeliness_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
            def #{attr_name}=(value)                      # def publish_date=(value)
              @timeliness_cache ||= {}                    #   @timeliness_cache ||= {}
              @timeliness_cache['#{attr_name}'] = value   #   @timeliness_cache['publish_date'] = value
              @attributes['#{attr_name}'] = super         #   @attributes['publish_date']
            end                                           # end
          STR
        end
generated_timeliness_methods() click to toggle source
# File lib/validates_timeliness/orm/mongoid.rb, line 51
def generated_timeliness_methods
  @generated_timeliness_methods ||= Module.new do |_m|
    extend Mutex_m
  end
  @generated_timeliness_methods.tap { |mod| include mod }
end
timeliness_type_cast_code(attr_name, var_name) click to toggle source
# File lib/validates_timeliness/orm/mongoid.rb, line 35
def timeliness_type_cast_code(attr_name, var_name)
  type = timeliness_attribute_type(attr_name)

  "#{var_name} = Timeliness::Parser.parse(value, :#{type})"
end