Sha256: 97c7b25936f4f864ab12ef3bfe2a997d3b3d548d69a0075d146bf054b69ba18d

Contents?: true

Size: 743 Bytes

Versions: 1

Compression:

Stored size: 743 Bytes

Contents

module MongoMapper
  class Key
    attr_accessor :name, :type, :options, :default_value
    
    def initialize(*args)
      options = args.extract_options!
      @name, @type = args.shift.to_s, args.shift
      self.options = (options || {}).symbolize_keys
      self.default_value = self.options.delete(:default)
    end

    def ==(other)
      @name == other.name && @type == other.type
    end

    def set(value)
      type.to_mongo(value)
    end

    def embeddable?
      type.respond_to?(:embeddable?) && type.embeddable? ? true : false
    end

    def get(value)
      if value.nil? && !default_value.nil?
        return default_value
      end
      
      type.from_mongo(value)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
djsun-mongomapper-0.4.1.2 lib/mongomapper/key.rb