Class: QDM::Quantity

Inherits:
Object
  • Object
show all
Defined in:
app/models/qdm/basetypes/quantity.rb

Overview

Represents a Quantity

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#unitObject

Returns the value of attribute unit.



4
5
6
# File 'app/models/qdm/basetypes/quantity.rb', line 4

def unit
  @unit
end

#valueObject

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

#mongoizeObject

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