Sha256: cdc1a84b64946675ffe1c7ea7eac222f86db75fa94af2c7f9f8adbeea6fd7877

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 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

    # Mongoid 8 hack
    def is_a?(o)
      return true if o == Class
      super
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
russian_phone-0.8.0 lib/russian_phone/field.rb