Class: Smith::Workflow::Composite::EncodedValueBudget

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Defined in:
lib/smith/workflow/composite/encoded_value_budget.rb

Instance Method Summary collapse

Constructor Details

#initializeEncodedValueBudget

Returns a new instance of EncodedValueBudget.



17
18
19
20
21
# File 'lib/smith/workflow/composite/encoded_value_budget.rb', line 17

def initialize(...)
  super
  @bytes = 2
  @entries = 0
end

Instance Method Details

#add(value) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/smith/workflow/composite/encoded_value_budget.rb', line 23

def add(value)
  @bytes += JSON.generate(value, max_nesting: false).bytesize
  @bytes += 1 if @entries.positive?
  @entries += 1
  raise WorkflowError, "#{label} exceeds maximum bytes #{max_bytes}" if @bytes > max_bytes

  self
rescue JSON::GeneratorError => e
  raise WorkflowError, "#{label} cannot be encoded: #{e.message}"
end