Class: MongoMapper::Plugins::Keys::Key
- Inherits:
-
Object
- Object
- MongoMapper::Plugins::Keys::Key
- Defined in:
- lib/mongo_mapper/plugins/keys/key.rb
Constant Summary
- ID_STR =
'_id'
Instance Attribute Summary (collapse)
-
- (Object) default
Returns the value of attribute default.
-
- (Object) name
Returns the value of attribute name.
-
- (Object) options
Returns the value of attribute options.
-
- (Object) type
Returns the value of attribute type.
Instance Method Summary (collapse)
- - (Object) ==(other)
- - (Boolean) default?
- - (Object) default_value
- - (Boolean) embeddable?
- - (Object) get(value)
-
- (Key) initialize(*args)
constructor
A new instance of Key.
- - (Boolean) number?
- - (Object) set(value)
Constructor Details
- (Key) initialize(*args)
A new instance of Key
10 11 12 13 14 15 16 17 18 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 10 def initialize(*args) = args. @name, @type = args.shift.to_s, args.shift self. = ( || {}).symbolize_keys if .key?(:default) self.default = self.[:default] end end |
Instance Attribute Details
- (Object) default
Returns the value of attribute default
6 7 8 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 6 def default @default end |
- (Object) name
Returns the value of attribute name
6 7 8 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 6 def name @name end |
- (Object) options
Returns the value of attribute options
6 7 8 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 6 def @options end |
- (Object) type
Returns the value of attribute type
6 7 8 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 6 def type @type end |
Instance Method Details
- (Object) ==(other)
20 21 22 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 20 def ==(other) @name == other.name && @type == other.type end |
- (Boolean) default?
39 40 41 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 39 def default? .key?(:default) end |
- (Object) default_value
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 62 def default_value return unless default? if default.respond_to?(:call) default.call else # Using Marshal is easiest way to get a copy of mutable objects # without getting an error on immutable objects Marshal.load(Marshal.dump(default)) end end |
- (Boolean) embeddable?
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 24 def # This is ugly, but it's fast. We can't use ||= because false is an expected and common value. @embeddable = @embeddable != nil ? @embeddable : begin if type.respond_to?(:embeddable?) type. else false end end end |
- (Object) get(value)
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 43 def get(value) # Special Case: Generate default _id on access value = default_value if value.nil? and name == ID_STR if [:typecast] type.from_mongo(value).map! { |v| typecast_class.from_mongo(v) } else type.from_mongo(value) end end |
- (Boolean) number?
35 36 37 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 35 def number? type == Integer || type == Float end |
- (Object) set(value)
54 55 56 57 58 59 60 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 54 def set(value) type.to_mongo(value).tap do |values| if [:typecast] values.map! { |v| typecast_class.to_mongo(v) } end end end |