Class: Workarea::Tax::RateLookup
- Inherits:
-
Object
- Object
- Workarea::Tax::RateLookup
- Defined in:
- app/models/workarea/tax/rate_lookup.rb
Instance Attribute Summary collapse
-
#category ⇒ Object
readonly
Returns the value of attribute category.
-
#country ⇒ Object
readonly
Returns the value of attribute country.
-
#postal_code ⇒ Object
readonly
Returns the value of attribute postal_code.
-
#price ⇒ Object
readonly
Returns the value of attribute price.
-
#region ⇒ Object
readonly
Returns the value of attribute region.
Class Method Summary collapse
-
.find_best_rate(**options) ⇒ Object
Find the best available rate for the given parameters.
Instance Method Summary collapse
- #best_available_rate ⇒ Object
- #country_rates ⇒ Object
-
#initialize(category:, price:, postal_code: nil, country: nil, region: nil) ⇒ RateLookup
constructor
A new instance of RateLookup.
- #local_rate ⇒ Object
- #localized_rates ⇒ Object
- #no_local_postal_code_rates? ⇒ Boolean
- #postal_code_clause ⇒ Object
- #postal_code_rates ⇒ Object
- #rates_in_country ⇒ Object
- #region_clause ⇒ Object
- #region_rates ⇒ Object
- #regional_postal_code ⇒ Object
- #regional_postal_code_clause ⇒ Object
- #regional_postal_code_rates ⇒ Object
- #tiered_rate ⇒ Object
Constructor Details
#initialize(category:, price:, postal_code: nil, country: nil, region: nil) ⇒ RateLookup
Returns a new instance of RateLookup.
8 9 10 11 12 13 14 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 8 def initialize(category: , price: , postal_code: nil, country: nil, region: nil) @category = category @price = price @postal_code = postal_code @country = country @region = region end |
Instance Attribute Details
#category ⇒ Object (readonly)
Returns the value of attribute category.
4 5 6 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 4 def category @category end |
#country ⇒ Object (readonly)
Returns the value of attribute country.
4 5 6 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 4 def country @country end |
#postal_code ⇒ Object (readonly)
Returns the value of attribute postal_code.
4 5 6 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 4 def postal_code @postal_code end |
#price ⇒ Object (readonly)
Returns the value of attribute price.
4 5 6 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 4 def price @price end |
#region ⇒ Object (readonly)
Returns the value of attribute region.
4 5 6 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 4 def region @region end |
Class Method Details
.find_best_rate(**options) ⇒ Object
Find the best available rate for the given parameters.
17 18 19 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 17 def self.find_best_rate(**) new(**).best_available_rate end |
Instance Method Details
#best_available_rate ⇒ Object
21 22 23 24 25 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 21 def best_available_rate return if country.blank? && region.blank? && postal_code.blank? return tiered_rate if tiered? local_rate end |
#country_rates ⇒ Object
54 55 56 57 58 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 54 def country_rates rates_in_country .where(region_clause) .where(postal_code_clause) end |
#local_rate ⇒ Object
39 40 41 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 39 def local_rate localized_rates.first end |
#localized_rates ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 27 def localized_rates if country.present? && region.blank? && postal_code.blank? country_rates elsif country.present? && region.present? && postal_code.blank? region_rates elsif country == Country['US'] && no_local_postal_code_rates? regional_postal_code_rates else postal_code_rates end end |
#no_local_postal_code_rates? ⇒ Boolean
75 76 77 78 79 80 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 75 def no_local_postal_code_rates? rates_in_country .where(region_clause) .where(postal_code: postal_code) .none? end |
#postal_code_clause ⇒ Object
90 91 92 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 90 def postal_code_clause { :postal_code.in => [nil, '', postal_code].uniq } end |
#postal_code_rates ⇒ Object
67 68 69 70 71 72 73 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 67 def postal_code_rates rates_in_country .where(region_clause) .where(postal_code_clause) .sort(region: -1) .sort(postal_code: -1) end |
#rates_in_country ⇒ Object
50 51 52 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 50 def rates_in_country rates.where(country: country) end |
#region_clause ⇒ Object
102 103 104 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 102 def region_clause { :region.in => [nil, '', region].uniq } end |
#region_rates ⇒ Object
60 61 62 63 64 65 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 60 def region_rates rates_in_country .where(region_clause) .where(postal_code_clause) .sort(region: -1) end |
#regional_postal_code ⇒ Object
98 99 100 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 98 def regional_postal_code postal_code.first(5) end |
#regional_postal_code_clause ⇒ Object
94 95 96 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 94 def regional_postal_code_clause { :postal_code.in => [nil, '', regional_postal_code].uniq } end |
#regional_postal_code_rates ⇒ Object
82 83 84 85 86 87 88 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 82 def regional_postal_code_rates rates_in_country .where(region_clause) .where(regional_postal_code_clause) .sort(region: -1) .sort(postal_code: -1) end |
#tiered_rate ⇒ Object
43 44 45 46 47 48 |
# File 'app/models/workarea/tax/rate_lookup.rb', line 43 def tiered_rate localized_rates.detect do |rate| (rate.tier_min.nil? || rate.tier_min <= price) && (rate.tier_max.nil? || rate.tier_max >= price) end end |