Class: Mistri::Usage::Cost

Inherits:
Data
  • Object
show all
Defined in:
lib/mistri/usage.rb

Overview

Dollar amounts plus whether every contributing token had known pricing. Known stays outside the released Data shape so pattern matching and positional construction remain compatible.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input:, output:, cache_read:, cache_write:, total:, known: true) ⇒ Cost

Returns a new instance of Cost.



18
19
20
21
# File 'lib/mistri/usage.rb', line 18

def initialize(input:, output:, cache_read:, cache_write:, total:, known: true)
  @known = known == true
  super(input:, output:, cache_read:, cache_write:, total:)
end

Instance Attribute Details

#cache_readObject (readonly)

Returns the value of attribute cache_read

Returns:

  • (Object)

    the current value of cache_read



17
18
19
# File 'lib/mistri/usage.rb', line 17

def cache_read
  @cache_read
end

#cache_writeObject (readonly)

Returns the value of attribute cache_write

Returns:

  • (Object)

    the current value of cache_write



17
18
19
# File 'lib/mistri/usage.rb', line 17

def cache_write
  @cache_write
end

#inputObject (readonly)

Returns the value of attribute input

Returns:

  • (Object)

    the current value of input



17
18
19
# File 'lib/mistri/usage.rb', line 17

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output

Returns:

  • (Object)

    the current value of output



17
18
19
# File 'lib/mistri/usage.rb', line 17

def output
  @output
end

#totalObject (readonly)

Returns the value of attribute total

Returns:

  • (Object)

    the current value of total



17
18
19
# File 'lib/mistri/usage.rb', line 17

def total
  @total
end

Class Method Details

._load(payload) ⇒ Object



56
# File 'lib/mistri/usage.rb', line 56

def self._load(payload) = new(**JSON.parse(payload, symbolize_names: true))

.unknownObject



28
29
30
31
# File 'lib/mistri/usage.rb', line 28

def self.unknown
  new(input: 0.0, output: 0.0, cache_read: 0.0, cache_write: 0.0,
      total: 0.0, known: false)
end

.zeroObject



23
24
25
26
# File 'lib/mistri/usage.rb', line 23

def self.zero
  new(input: 0.0, output: 0.0, cache_read: 0.0, cache_write: 0.0,
      total: 0.0, known: true)
end

Instance Method Details

#+(other) ⇒ Object



39
40
41
42
43
44
# File 'lib/mistri/usage.rb', line 39

def +(other)
  self.class.new(input: input + other.input, output: output + other.output,
                 cache_read: cache_read + other.cache_read,
                 cache_write: cache_write + other.cache_write,
                 total: total + other.total, known: known? && other.known?)
end

#==(other) ⇒ Object Also known as: eql?



46
# File 'lib/mistri/usage.rb', line 46

def ==(other) = super && known? == other.known?

#_dump(_level) ⇒ Object



54
# File 'lib/mistri/usage.rb', line 54

def _dump(_level) = JSON.generate(to_h)

#hashObject



50
# File 'lib/mistri/usage.rb', line 50

def hash = [super, known?].hash

#known?Boolean

Returns:

  • (Boolean)


33
# File 'lib/mistri/usage.rb', line 33

def known? = defined?(@known) ? @known : true

#to_hObject



52
# File 'lib/mistri/usage.rb', line 52

def to_h = super.merge(known: known?)

#with(**changes) ⇒ Object



35
36
37
# File 'lib/mistri/usage.rb', line 35

def with(**changes)
  self.class.new(**to_h, **changes)
end