Sha256: 6ed56d003bfd2a2e6d75f3c25afc771d91700edd224e96406da97f1236f1b749

Contents?: true

Size: 778 Bytes

Versions: 57

Compression:

Stored size: 778 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 number?
      [Integer, Float].include?(type)
    end

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

Version data entries

57 entries across 57 versions & 7 rubygems

Version Path
mrkurt-mongo_mapper-0.6.11 lib/mongo_mapper/key.rb
mongo_mapper-unstable-2010.1.6 lib/mongo_mapper/key.rb
mongo_mapper-unstable-2010.1.5 lib/mongo_mapper/key.rb
mongo_mapper-unstable-2010.1.4 lib/mongo_mapper/key.rb
tpitale-mongo_mapper-0.6.10 lib/mongo_mapper/key.rb
mrkurt-mongo_mapper-0.6.10 lib/mongo_mapper/key.rb
mongo_mapper-0.6.10 lib/mongo_mapper/key.rb
mrkurt-mongo_mapper-0.6.9 lib/mongo_mapper/key.rb
mongo_mapper-0.6.9 lib/mongo_mapper/key.rb
mrkurt-mongo_mapper-0.6.8 lib/mongo_mapper/key.rb
tpitale-mongo_mapper-0.6.9 lib/mongo_mapper/key.rb
mongo_mapper-unstable-2009.12.30 lib/mongo_mapper/key.rb
mongo_mapper-unstable-2009.12.29 lib/mongo_mapper/key.rb
mongo_mapper-unstable-2009.12.23 lib/mongo_mapper/key.rb
mongo_mapper-unstable-2009.12.22 lib/mongo_mapper/key.rb
mongo_mapper-unstable-2009.12.16 lib/mongo_mapper/key.rb
mongo_mapper-0.6.8 lib/mongo_mapper/key.rb
mongo_mapper-unstable-2009.12.14 lib/mongo_mapper/key.rb
mongo_mapper-0.6.7 lib/mongo_mapper/key.rb
mongo_mapper-0.6.6 lib/mongo_mapper/key.rb