Class: IncomeTax::Models::Progressive

Inherits:
Generic
  • Object
show all
Defined in:
lib/income_tax/models/progressive.rb

Direct Known Subclasses

Countries::Albania, Countries::Algeria, Countries::Angola, Countries::AntiguaAndBarbuda, Countries::Argentina, Countries::Armenia, Countries::Aruba, Countries::Australia, Countries::Austria, Countries::Azerbaijan, Countries::Bangladesh, Countries::Barbados, Countries::Belgium, Countries::Belize, Countries::Benin, Countries::Bhutan, Countries::Botswana, Countries::Brazil, Countries::BurkinaFaso, Countries::Burundi, Countries::Cambodia, Countries::Cameroon, Countries::Canada::Federal, Countries::CapeVerde, Countries::CaribbeanNetherlands, Countries::CentralAfricanRepublic, Countries::Chad, Countries::Chile, Countries::China, Countries::Colombia, Countries::Comoros, Countries::CongoRepublic, Countries::CookIslands, Countries::CostaRica, Countries::Croatia, Countries::Cuba, Countries::Curacao, Countries::Cyprus, Countries::Denmark, Countries::Djibouti, Countries::Dominica, Countries::DominicanRepublic, Countries::Ecuador, Countries::Egypt, Countries::EquatorialGuinea, Countries::Ethiopia, Countries::FalklandIslands, Countries::Fiji, Countries::Finland, Countries::France, Countries::Gabon, Countries::Gambia, Countries::Ghana, Countries::Gibraltar, Countries::Greece, Countries::Grenada, Countries::Guadeloupe, Countries::Guatemala, Countries::Guinea, Countries::Guyana, Countries::HongKong, Countries::Iceland, Countries::India, Countries::Indonesia, Countries::Iran, Countries::Ireland, Countries::IsleOfMan, Countries::Israel, Countries::Italy, Countries::IvoryCoast, Countries::Jamaica, Countries::Japan, Countries::Jordan, Countries::Kenya, Countries::Kosovo, Countries::Laos, Countries::Lebanon, Countries::Lesotho, Countries::Liberia, Countries::Libya, Countries::Luxembourg, Countries::Macao, Countries::Madagascar, Countries::Malawi, Countries::Malaysia, Countries::Malta, Countries::MarshallIslands, Countries::Martinique, Countries::Mauritania, Countries::Mexico, Countries::Moldova, Countries::Montenegro, Countries::Montserrat, Countries::Morocco, Countries::Mozambique, Countries::Myanmar, Countries::Namibia, Countries::Nepal, Countries::Netherlands, Countries::NetherlandsAntilles, Countries::NewCaledonia, Countries::NewZealand, Countries::Nicaragua, Countries::Niger, Countries::Nigeria, Countries::Niue, Countries::Norway, Countries::Pakistan, Countries::Palau, Countries::PalestinianTerritory, Countries::Panama, Countries::PapuaNewGuinea, Countries::Peru, Countries::Philippines, Countries::Poland, Countries::Portugal, Countries::Rwanda, Countries::SaintLucia, Countries::SaintMartin, Countries::SaintPierreAndMiquelon, Countries::SaintVincentAndTheGrenedines, Countries::Samoa, Countries::SanMarino, Countries::Senegal, Countries::Serbia, Countries::SierraLeone, Countries::Singapore, Countries::SintMaarten, Countries::Slovakia, Countries::Slovenia, Countries::SolomonIslands, Countries::Somalia, Countries::SouthAfrica, Countries::SouthKorea, Countries::SouthSudan, Countries::Spain, Countries::Suriname, Countries::Sweden, Countries::Switzerland::Canton, Countries::Switzerland::Federal, Countries::Syria, Countries::Taiwan, Countries::Tajikistan, Countries::Tanzania, Countries::Thailand, Countries::TimorLeste, Countries::Tokelau, Countries::Tonga, Countries::TrinidadAndTobago, Countries::Tunisia, Countries::Turkey, Countries::Uganda, Countries::UnitedKingdom, Countries::UnitedStates::Federal, Countries::UnitedStates::State, Countries::Uruguay, Countries::Uzbekistan, Countries::Venezuela, Countries::Vietnam, Countries::Yemen, Countries::Zambia, Countries::Zimbabwe

Defined Under Namespace

Classes: Levels

Instance Attribute Summary

Attributes inherited from Generic

#gross_income, #net_income, #options, #rate, #taxes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generic

#based_on?, #cast_value, #cast_values, currency, #initialize, #inspect, lazy, #location_name, method_added, name, names, new, other_names, register, register_on, #set_default_options, #setup, #validate, wants_options

Constructor Details

This class inherits a constructor from IncomeTax::Models::Generic

Class Method Details

.level(start, rate) ⇒ Object



65
66
67
# File 'lib/income_tax/models/progressive.rb', line 65

def self.level(start, rate)
  levels(:default).level(start, rate)
end

.levels(category = nil, *options, year: nil, &block) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/income_tax/models/progressive.rb', line 89

def self.levels(category = nil, *options, year: nil, &block)
  category    ||= :default if block
  @year_cache ||= {}
  result        = tax_year(year)

  if category
    result = result[category] ||= Levels.new(*options)
    result.instance_eval(&block) if block
    @year_cache.clear
  elsif year
    @year_cache[year] ||= tax_years.select { |k,_| k.to_i <= year }.map(&:last).inject(:merge)
    result = @year_cache.fetch(year)
  end

  result
end

.offset(delta) ⇒ Object



69
70
71
# File 'lib/income_tax/models/progressive.rb', line 69

def self.offset(delta)
  levels(:default).offset(delta)
end

.remainder(rate) ⇒ Object



73
74
75
# File 'lib/income_tax/models/progressive.rb', line 73

def self.remainder(rate)
  levels(:default).remainder(rate)
end

.tax_year(year) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/income_tax/models/progressive.rb', line 81

def self.tax_year(year)
  tax_years.fetch(year) do
    data       = {}
    @tax_years = @tax_years.to_a.push([year, data]).sort_by { |e| e.first.to_i }.to_h
    data
  end
end

.tax_yearsObject



77
78
79
# File 'lib/income_tax/models/progressive.rb', line 77

def self.tax_years
  @tax_years ||= {}
end

Instance Method Details

#calculate_grossObject



116
117
118
# File 'lib/income_tax/models/progressive.rb', line 116

def calculate_gross
  @taxes = levels.calculate_gross(gross_income)
end

#calculate_netObject



120
121
122
# File 'lib/income_tax/models/progressive.rb', line 120

def calculate_net
  @taxes = levels.calculate_net(net_income)
end

#level_categoryObject



106
107
108
# File 'lib/income_tax/models/progressive.rb', line 106

def level_category
  :default
end

#levels(category = level_category) ⇒ Object



110
111
112
113
114
# File 'lib/income_tax/models/progressive.rb', line 110

def levels(category = level_category)
  self.class.levels(year: tax_year).fetch(category) do
    raise ArgumentError, "unkown level category #{category} (available: #{self.class.levels.keys.join(", ")})"
  end
end