Sha256: 3650cd77911e7a298ef50dac1e511aa9a13d4602419f9b755e65d4ddbe46dcfe

Contents?: true

Size: 711 Bytes

Versions: 3

Compression:

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

3 entries across 3 versions & 2 rubygems

Version Path
djsun-mongo_mapper-0.5.0.1 lib/mongo_mapper/key.rb
mongo_mapper-0.5.1 lib/mongo_mapper/key.rb
mongo_mapper-0.5.0 lib/mongo_mapper/key.rb