Sha256: 768387245cf49feb322df4c719368c6a66abf0a71401c2f02853a42b559282ff

Contents?: true

Size: 804 Bytes

Versions: 50

Compression:

Stored size: 804 Bytes

Contents

require 'active_support/core_ext/hash/indifferent_access'

module MongoModel
  module Types
    class Custom < Object
      def initialize(type)
        @type = type
      end
      
      def cast(value)
        if value.is_a?(@type)
          value
        elsif @type.respond_to?(:cast)
          @type.cast(value)
        else
          @type.new(value)
        end
      end
      
      def to_mongo(value)
        if value.respond_to?(:to_mongo)
          value.to_mongo
        else
          value
        end
      end
      
      def from_mongo(value)
        if @type.respond_to?(:from_mongo)
          value = value.with_indifferent_access if value.respond_to?(:with_indifferent_access)
          @type.from_mongo(value)
        else
          value
        end
      end
    end
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
mongomodel-0.5.5 lib/mongomodel/support/types/custom.rb
mongomodel-0.5.4 lib/mongomodel/support/types/custom.rb
mongomodel-0.5.3 lib/mongomodel/support/types/custom.rb
mongomodel-0.5.2 lib/mongomodel/support/types/custom.rb
mongomodel-0.5.1 lib/mongomodel/support/types/custom.rb
mongomodel-0.5.0 lib/mongomodel/support/types/custom.rb
mongomodel-0.4.9 lib/mongomodel/support/types/custom.rb
mongomodel-0.4.8 lib/mongomodel/support/types/custom.rb
mongomodel-0.4.7 lib/mongomodel/support/types/custom.rb
mongomodel-0.4.6 lib/mongomodel/support/types/custom.rb
mongomodel-0.4.5 lib/mongomodel/support/types/custom.rb
mongomodel-0.4.4 lib/mongomodel/support/types/custom.rb
mongomodel-0.4.3 lib/mongomodel/support/types/custom.rb
mongomodel-0.4.2 lib/mongomodel/support/types/custom.rb
mongomodel-0.4.1 lib/mongomodel/support/types/custom.rb
mongomodel-0.4.0 lib/mongomodel/support/types/custom.rb
mongomodel-0.3.6 lib/mongomodel/support/types/custom.rb
mongomodel-0.3.5 lib/mongomodel/support/types/custom.rb
mongomodel-0.3.4 lib/mongomodel/support/types/custom.rb
mongomodel-0.3.3 lib/mongomodel/support/types/custom.rb