Class: Smith::Tool::ArgumentSnapshotAccounting

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Defined in:
lib/smith/tool/argument_snapshot_accounting.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeArgumentSnapshotAccounting

Returns a new instance of ArgumentSnapshotAccounting.



16
17
18
19
20
21
# File 'lib/smith/tool/argument_snapshot_accounting.rb', line 16

def initialize(...)
  super
  @node_count = 0
  @allocated_node_count = 1
  @byte_count = 0
end

Instance Attribute Details

#byte_countObject (readonly)

Returns the value of attribute byte_count.



14
15
16
# File 'lib/smith/tool/argument_snapshot_accounting.rb', line 14

def byte_count
  @byte_count
end

#node_countObject (readonly)

Returns the value of attribute node_count.



14
15
16
# File 'lib/smith/tool/argument_snapshot_accounting.rb', line 14

def node_count
  @node_count
end

Instance Method Details

#account_reused!(metrics, depth) ⇒ Object

Raises:



43
44
45
46
47
48
# File 'lib/smith/tool/argument_snapshot_accounting.rb', line 43

def (metrics, depth)
  raise Error, "tool arguments exceed #{max_depth} levels" if depth + metrics.fetch(:max_depth) > max_depth

  add_nodes!(metrics.fetch(:node_count) - 1)
  add_bytes!(metrics.fetch(:byte_count))
end

#add_bytes!(bytes) ⇒ Object

Raises:



34
35
36
37
# File 'lib/smith/tool/argument_snapshot_accounting.rb', line 34

def add_bytes!(bytes)
  @byte_count += bytes
  raise Error, "tool arguments exceed #{max_bytes} bytes" if @byte_count > max_bytes
end

#propagate!(parent_metrics, child_metrics) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/smith/tool/argument_snapshot_accounting.rb', line 50

def propagate!(parent_metrics, child_metrics)
  return unless parent_metrics

  parent_metrics[:node_count] += child_metrics.fetch(:node_count)
  parent_metrics[:byte_count] += child_metrics.fetch(:byte_count)
  parent_metrics[:max_depth] = [
    parent_metrics[:max_depth],
    child_metrics.fetch(:max_depth) + 1
  ].max
end

#reserve_children!(count) ⇒ Object

Raises:



29
30
31
32
# File 'lib/smith/tool/argument_snapshot_accounting.rb', line 29

def reserve_children!(count)
  @allocated_node_count += count
  raise Error, "tool arguments exceed #{max_nodes} values" if @allocated_node_count > max_nodes
end

#validate_bytes!(bytes) ⇒ Object

Raises:



39
40
41
# File 'lib/smith/tool/argument_snapshot_accounting.rb', line 39

def validate_bytes!(bytes)
  raise Error, "tool arguments exceed #{max_bytes} bytes" if @byte_count + bytes > max_bytes
end

#visit!(depth) ⇒ Object

Raises:



23
24
25
26
27
# File 'lib/smith/tool/argument_snapshot_accounting.rb', line 23

def visit!(depth)
  raise Error, "tool arguments exceed #{max_depth} levels" if depth > max_depth

  add_nodes!(1)
end