Sha256: 34a9484aa77ccccfd5b0d33fca81ed9a2531b906e685c4d75c18851418bc2d97

Contents?: true

Size: 1.26 KB

Versions: 7

Compression:

Stored size: 1.26 KB

Contents

# coding: utf-8

module RussianPhone
  class Field
    attr_accessor :options

    def initialize(options = {})
      @options = Number.process_options(options)
    end

    ::Mongoid::Fields.option :validate do |model, field, value|
      if value
        model.validates_with(RussianPhone::FormatValidator, fields: [field.name])
      end
    end

    ::Mongoid::Fields.option :required do |model, field, value|
      if value
        model.validates_with(RussianPhone::PresenceValidator, fields: [field.name])
      end
    end

    # Get the object as it was stored in the database, and instantiate
    # this custom class from it.
    def demongoize(object)
      RussianPhone::Number.new(object, @options)
    end

    # Takes any possible object and converts it to how it would be
    # stored in the database.
    def mongoize(object)
      case object
        when RussianPhone::Number then object.mongoize
        when String then RussianPhone::Number.new(object, @options).mongoize
        else object
      end
    end

    # Converts the object that was supplied to a criteria and converts it
    # into a database friendly form.
    def evolve(object)
      case object
        when RussianPhone::Number then object.mongoize
        else object
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
russian_phone-0.7.0 lib/russian_phone/field.rb
russian_phone-0.6.1 lib/russian_phone/field.rb
russian_phone-0.6.0 lib/russian_phone/field.rb
russian_phone-0.5.3 lib/russian_phone/field.rb
russian_phone-0.5.2 lib/russian_phone/field.rb
russian_phone-0.5.1 lib/russian_phone/field.rb
russian_phone-0.5.0 lib/russian_phone/field.rb