Class: MongoMapper::Plugins::Keys::Key
- Defined in:
- lib/mongo_mapper/plugins/keys/key.rb
Instance Attribute Summary collapse
-
#default_value ⇒ Object
Returns the value of attribute default_value.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #embeddable? ⇒ Boolean
- #get(value) ⇒ Object
-
#initialize(*args) ⇒ Key
constructor
A new instance of Key.
- #number? ⇒ Boolean
- #set(value) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Key
Returns a new instance of Key.
8 9 10 11 12 13 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 8 def initialize(*args) = args. @name, @type = args.shift.to_s, args.shift self. = ( || {}).symbolize_keys self.default_value = self.[:default] end |
Instance Attribute Details
#default_value ⇒ Object
Returns the value of attribute default_value.
6 7 8 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 6 def default_value @default_value end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 6 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 6 def @options end |
#type ⇒ Object
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
#==(other) ⇒ Object
15 16 17 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 15 def ==(other) @name == other.name && @type == other.type end |
#embeddable? ⇒ Boolean
19 20 21 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 19 def type.respond_to?(:embeddable?) && type. ? true : false end |
#get(value) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 27 def get(value) if value.nil? && !default_value.nil? if default_value.respond_to?(:call) return default_value.call else return default_value end end type.from_mongo(value) end |
#number? ⇒ Boolean
23 24 25 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 23 def number? [Integer, Float].include?(type) end |
#set(value) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 39 def set(value) type.to_mongo(value).tap do |values| if [:typecast].present? values.map! { |v| typecast_class.to_mongo(v) } end end end |