Class: MongoMapper::Key
- Inherits:
-
Object
- Object
- MongoMapper::Key
- Defined in:
- lib/mongomapper/key.rb
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, Ref]
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(name, type, options = {}) ⇒ Key
constructor
A new instance of Key.
- #native? ⇒ Boolean
- #set(value) ⇒ Object
Constructor Details
#initialize(name, type, options = {}) ⇒ Key
Returns a new instance of Key.
11 12 13 14 15 |
# File 'lib/mongomapper/key.rb', line 11 def initialize(name, type, ={}) @name, @type = name.to_s, type self. = .symbolize_keys self.default_value = .delete(:default) end |
Instance Attribute Details
#default_value ⇒ Object
Returns the value of attribute default_value.
9 10 11 |
# File 'lib/mongomapper/key.rb', line 9 def default_value @default_value end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/mongomapper/key.rb', line 9 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/mongomapper/key.rb', line 9 def @options end |
#type ⇒ Object
Returns the value of attribute type.
9 10 11 |
# File 'lib/mongomapper/key.rb', line 9 def type @type end |
Instance Method Details
#==(other) ⇒ Object
17 18 19 |
# File 'lib/mongomapper/key.rb', line 17 def ==(other) @name == other.name && @type == other.type end |
#embedded_document? ⇒ Boolean
29 30 31 |
# File 'lib/mongomapper/key.rb', line 29 def type.ancestors.include?(EmbeddedDocument) && !type.ancestors.include?(Document) end |
#get(value) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mongomapper/key.rb', line 33 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
25 26 27 |
# File 'lib/mongomapper/key.rb', line 25 def native? @native ||= NativeTypes.include?(type) end |
#set(value) ⇒ Object
21 22 23 |
# File 'lib/mongomapper/key.rb', line 21 def set(value) typecast(value) end |