Class: Squake::Model::Carbon

Inherits:
T::Struct
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/squake/model/carbon.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_api_response(response_body) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/squake/model/carbon.rb', line 16

def self.from_api_response(response_body)
  Squake::Model::Carbon.new(
    quantity: BigDecimal(String(response_body.fetch(:carbon_quantity))),
    unit: CarbonUnit.deserialize(response_body.fetch(:carbon_unit)),
    items: response_body.fetch(:items, nil),
  )
end

Instance Method Details

#+(other) ⇒ Object



42
43
44
45
46
47
# File 'lib/squake/model/carbon.rb', line 42

def +(other)
  other_qty = CarbonUnit.convert(other.quantity, other.unit, to: CarbonUnit::GRAM)
  self_qty = CarbonUnit.convert(quantity, unit, to: CarbonUnit::GRAM)

  Carbon.new(quantity: other_qty + self_qty)
end

#fractionalObject



50
51
52
# File 'lib/squake/model/carbon.rb', line 50

def fractional
  quantity.to_f.round(unit.precision)
end

#in!(unit) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/squake/model/carbon.rb', line 29

def in!(unit)
  # this is a bit hacky, but it optimizes memory usages and we don't need `T.must`
  # everywhere if we were to use a prop instead of a const.
  @quantity = CarbonUnit.convert(quantity, self.unit, to: unit)
  @unit = unit
  self
end