Class: QDM::Quantity
- Inherits:
-
Object
- Object
- QDM::Quantity
- Defined in:
- app/models/qdm/basetypes/quantity.rb
Overview
Represents a Quantity
Instance Attribute Summary collapse
-
#unit ⇒ Object
Returns the value of attribute unit.
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
-
.demongoize(object) ⇒ Object
Get the object as it was stored in the database, and instantiate this custom class from it.
-
.evolve(object) ⇒ Object
Converts the object that was supplied to a criteria and converts it into a database friendly form.
-
.mongoize(object) ⇒ Object
Takes any possible object and converts it to how it would be stored in the database.
Instance Method Summary collapse
-
#initialize(value, unit) ⇒ Quantity
constructor
Value and unit are required.
-
#mongoize ⇒ Object
Converts an object of this instance into a database friendly value.
Constructor Details
#initialize(value, unit) ⇒ Quantity
Value and unit are required.
7 8 9 10 |
# File 'app/models/qdm/basetypes/quantity.rb', line 7 def initialize(value, unit) @value = value @unit = unit end |
Instance Attribute Details
#unit ⇒ Object
Returns the value of attribute unit.
4 5 6 |
# File 'app/models/qdm/basetypes/quantity.rb', line 4 def unit @unit end |
#value ⇒ Object
Returns the value of attribute value.
4 5 6 |
# File 'app/models/qdm/basetypes/quantity.rb', line 4 def value @value end |
Class Method Details
.demongoize(object) ⇒ Object
Get the object as it was stored in the database, and instantiate this custom class from it.
The array elements in demongoize are the same 5 elements used in mongoize, i.e. [ value, unit ].
23 24 25 26 27 28 |
# File 'app/models/qdm/basetypes/quantity.rb', line 23 def demongoize(object) return nil unless object object = object.symbolize_keys QDM::Quantity.new(object[:value], object[:unit]) if object.is_a?(Hash) end |
.evolve(object) ⇒ Object
Converts the object that was supplied to a criteria and converts it into a database friendly form.
45 46 47 48 49 50 |
# File 'app/models/qdm/basetypes/quantity.rb', line 45 def evolve(object) case object when QDM::Quantity then object.mongoize else object end end |
.mongoize(object) ⇒ Object
Takes any possible object and converts it to how it would be stored in the database.
32 33 34 35 36 37 38 39 40 41 |
# File 'app/models/qdm/basetypes/quantity.rb', line 32 def mongoize(object) case object when nil then nil when QDM::Quantity then object.mongoize when Hash object = object.symbolize_keys QDM::Quantity.new(object[:value], object[:unit]).mongoize else object end end |
Instance Method Details
#mongoize ⇒ Object
Converts an object of this instance into a database friendly value.
13 14 15 |
# File 'app/models/qdm/basetypes/quantity.rb', line 13 def mongoize { value: @value, unit: @unit, _type: 'QDM::Quantity' } end |