Class: LlmCostTracker::Charges::Cost

Inherits:
Data
  • Object
show all
Defined in:
lib/llm_cost_tracker/charges/cost.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#componentsObject (readonly)

Returns the value of attribute components

Returns:

  • (Object)

    the current value of components



7
8
9
# File 'lib/llm_cost_tracker/charges/cost.rb', line 7

def components
  @components
end

#currencyObject (readonly)

Returns the value of attribute currency

Returns:

  • (Object)

    the current value of currency



7
8
9
# File 'lib/llm_cost_tracker/charges/cost.rb', line 7

def currency
  @currency
end

#totalObject (readonly)

Returns the value of attribute total

Returns:

  • (Object)

    the current value of total



7
8
9
# File 'lib/llm_cost_tracker/charges/cost.rb', line 7

def total
  @total
end

Class Method Details

.from_h(attributes) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/llm_cost_tracker/charges/cost.rb', line 8

def self.from_h(attributes)
  components = attributes.key?(:components) ? attributes[:components] : attributes.except(:total_cost, :currency)
  total = attributes.fetch(:total) { attributes[:total_cost] }
  new(
    components: components.transform_values { |value| BigDecimal(value.to_s) }.freeze,
    total: total && BigDecimal(total.to_s),
    currency: attributes[:currency]
  )
end

Instance Method Details

#to_hObject



18
19
20
21
22
23
24
# File 'lib/llm_cost_tracker/charges/cost.rb', line 18

def to_h
  {
    components: components.transform_values { |value| value.to_s("F") },
    total: total&.to_s("F"),
    currency: currency
  }
end