Class: LlmCostTracker::Pricing::Mode

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_cost_tracker/pricing/mode.rb

Constant Summary collapse

COMPOUND_MODIFIERS =
%w[data_residency].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(modifiers) ⇒ Mode

Returns a new instance of Mode.



38
39
40
41
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 38

def initialize(modifiers)
  @modifiers = Array(modifiers).map(&:to_sym).uniq.sort
  freeze
end

Instance Attribute Details

#modifiersObject (readonly)

Returns the value of attribute modifiers.



8
9
10
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 8

def modifiers
  @modifiers
end

Class Method Details

.parse(value) ⇒ Object



10
11
12
13
14
15
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 10

def self.parse(value)
  return value if value.is_a?(self)
  return new([]) if value.nil?

  new(tokenize(value.to_s))
end

.tokenize(value) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 17

def self.tokenize(value)
  remaining = value.to_s.downcase.tr("-", "_")
  tokens = []
  loop do
    break if remaining.empty?

    compound = COMPOUND_MODIFIERS.find do |token|
      remaining == token || remaining.start_with?("#{token}_")
    end
    if compound
      tokens << compound.to_sym
      remaining = remaining.delete_prefix(compound).delete_prefix("_")
    else
      first, _, rest = remaining.partition("_")
      tokens << first.to_sym unless first.empty?
      remaining = rest
    end
  end
  tokens
end

Instance Method Details

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



66
67
68
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 66

def ==(other)
  other.is_a?(self.class) && modifiers == other.modifiers
end

#canonicalObject Also known as: to_s



51
52
53
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 51

def canonical
  modifiers.join("_")
end

#empty?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 43

def empty?
  modifiers.empty?
end

#hashObject



71
72
73
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 71

def hash
  modifiers.hash
end

#include?(modifier) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 47

def include?(modifier)
  modifiers.include?(modifier.to_sym)
end

#permutationsObject



60
61
62
63
64
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 60

def permutations
  return [canonical] if modifiers.size <= 1

  modifiers.permutation.map { |permutation| permutation.join("_") }.uniq
end

#to_symObject



56
57
58
# File 'lib/llm_cost_tracker/pricing/mode.rb', line 56

def to_sym
  empty? ? nil : canonical.to_sym
end