Class: Smith::Budget::AmountContract

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

Instance Method Summary collapse

Instance Method Details

#dimension!(key) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/smith/budget/amount_contract.rb', line 35

def dimension!(key)
  case key
  when Symbol then key
  when String then String.new(key).freeze
  else raise ArgumentError, "budget dimension keys must be symbols or strings"
  end
end

#known_dimension!(key) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
# File 'lib/smith/budget/amount_contract.rb', line 23

def known_dimension!(key)
  return if limits&.key?(key)

  raise ArgumentError, "unknown budget dimension #{key.inspect}"
end

#known_hash!(amounts) ⇒ Object



19
20
21
# File 'lib/smith/budget/amount_contract.rb', line 19

def known_hash!(amounts)
  amounts.each_key { known_dimension!(_1) }
end

#matching_dimensions!(reserved, actual) ⇒ Object

Raises:

  • (ArgumentError)


29
30
31
32
33
# File 'lib/smith/budget/amount_contract.rb', line 29

def matching_dimensions!(reserved, actual)
  return if reserved.length == actual.length && reserved.each_key.all? { actual.key?(_1) }

  raise ArgumentError, "reserved and actual budget dimensions must match"
end

#own_hash!(amounts, label) ⇒ Object



12
13
14
15
16
17
# File 'lib/smith/budget/amount_contract.rb', line 12

def own_hash!(amounts, label)
  case amounts
  when Hash then own_amount_hash(amounts, label)
  else raise ArgumentError, "#{label} values must be a Hash"
  end
end