Class: MongoMapper::Key
Constant Summary collapse
- NativeTypes =
DateTime and Date are currently not supported by mongo’s bson so just use Time
[String, Float, Time, Integer, Boolean, Array, Hash]
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
- #embedded_document? ⇒ Boolean
- #get(value) ⇒ Object
-
#initialize(*args) ⇒ Key
constructor
A new instance of Key.
- #native? ⇒ Boolean
- #set(value) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Key
Returns a new instance of Key.
8 9 10 11 12 13 |
# File 'lib/mongomapper/key.rb', line 8 def initialize(*args) = args. @name, @type = args.shift.to_s, args.shift self. = ( || {}).symbolize_keys self.default_value = self..delete(:default) end |
Instance Attribute Details
#default_value ⇒ Object
Returns the value of attribute default_value.
6 7 8 |
# File 'lib/mongomapper/key.rb', line 6 def default_value @default_value end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/mongomapper/key.rb', line 6 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
6 7 8 |
# File 'lib/mongomapper/key.rb', line 6 def @options end |
#type ⇒ Object
Returns the value of attribute type.
6 7 8 |
# File 'lib/mongomapper/key.rb', line 6 def type @type end |
Instance Method Details
#==(other) ⇒ Object
15 16 17 |
# File 'lib/mongomapper/key.rb', line 15 def ==(other) @name == other.name && @type == other.type end |
#embedded_document? ⇒ Boolean
27 28 29 |
# File 'lib/mongomapper/key.rb', line 27 def type.respond_to?(:embeddable?) && type. end |
#get(value) ⇒ Object
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mongomapper/key.rb', line 31 def get(value) return default_value if value.nil? && !default_value.nil? if type == Array value || [] elsif type == Hash HashWithIndifferentAccess.new(value || {}) else value end end |
#native? ⇒ Boolean
23 24 25 |
# File 'lib/mongomapper/key.rb', line 23 def native? @native ||= NativeTypes.include?(type) || type.nil? end |
#set(value) ⇒ Object
19 20 21 |
# File 'lib/mongomapper/key.rb', line 19 def set(value) typecast(value) end |