Class: MongoODM::Document::Fields::Field
- Inherits:
-
Object
- Object
- MongoODM::Document::Fields::Field
- Defined in:
- lib/mongo_odm/document/fields.rb
Instance Attribute Summary collapse
-
#name ⇒ Symbol
readonly
The name of this field in the Document.
-
#options ⇒ Hash
readonly
Configuration options.
-
#type ⇒ Class
readonly
The Ruby type of field.
Instance Method Summary collapse
-
#default ⇒ Object
The default value for this field if defined, or nil.
-
#initialize(name, type, options = {}) ⇒ Field
constructor
Creates a new document field.
-
#type_cast(value) ⇒ Object
Attempt to coerce the passed value into this field’s type.
Constructor Details
#initialize(name, type, options = {}) ⇒ Field
Creates a new document field.
25 26 27 28 29 |
# File 'lib/mongo_odm/document/fields.rb', line 25 def initialize(name, type, = {}) @name = name.to_sym @type = type @options = . end |
Instance Attribute Details
#name ⇒ Symbol (readonly)
Returns the name of this field in the Document.
14 15 16 |
# File 'lib/mongo_odm/document/fields.rb', line 14 def name @name end |
#options ⇒ Hash (readonly)
Returns configuration options.
18 19 20 |
# File 'lib/mongo_odm/document/fields.rb', line 18 def @options end |
#type ⇒ Class (readonly)
Returns the Ruby type of field.
16 17 18 |
# File 'lib/mongo_odm/document/fields.rb', line 16 def type @type end |
Instance Method Details
#default ⇒ Object
Returns The default value for this field if defined, or nil.
32 33 34 35 36 37 |
# File 'lib/mongo_odm/document/fields.rb', line 32 def default if .has_key?(:default) default = [:default] type_cast(default.respond_to?(:call) ? default.call : default) end end |
#type_cast(value) ⇒ Object
Attempt to coerce the passed value into this field’s type
43 44 45 46 |
# File 'lib/mongo_odm/document/fields.rb', line 43 def type_cast(value) raise MongoODM::Errors::TypeCastMissing.new(value, @type) unless @type.respond_to?(:type_cast) @type.type_cast(value) end |