Class: DeathAndTaxes::State

Inherits:
Object
  • Object
show all
Defined in:
lib/death_and_taxes/state.rb

Instance Method Summary collapse

Constructor Details

#initialize(code, yml = nil) ⇒ State

Returns a new instance of State.



4
5
6
7
8
# File 'lib/death_and_taxes/state.rb', line 4

def initialize code, yml = nil
  @taxes = {}
  @code = code
  parse yml if yml
end

Instance Method Details

#applicable_taxes(to_state, date) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/death_and_taxes/state.rb', line 20

def applicable_taxes to_state, date
  rule = @rules.select{|d| (d['starts']..d['ends']).cover? date}.first
  taxes = if to_state == @code && rule['same_state']
    rule['same_state']
  elsif rule['same_country']
    rule['same_country']
  else
    []
  end
  [taxes].flatten
end

#build_tax(tax, date) ⇒ Object



32
33
34
35
36
# File 'lib/death_and_taxes/state.rb', line 32

def build_tax tax, date
  if (i = @taxes[tax]) && i.cover?(date)
    i.build(date)
  end
end

#parse(yml) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/death_and_taxes/state.rb', line 10

def parse yml
  ##
  # Builds a tax list for each taxes defined in this state
  yml['taxes'].each do |tax_name, infos|
    @taxes[tax_name] = TaxInfo.new(tax_name, infos)
  end

  @rules = yml['rules']
end