Module: VatCalculator

Defined in:
lib/vat-calculator.rb,
lib/vat-calculator/version.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



43
44
45
# File 'lib/vat-calculator.rb', line 43

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#calculate_sum(args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/vat-calculator.rb', line 22

def calculate_sum args
  options = args.extract_options!
  with_amount = options[:with_amount].nil? ? self.class.fields_for_calculations[:with_amount] : options[:with_amount]
  price_type = args.first

  price, vat = fields_values price_type, :vat
  sum = yield price, vat

  amount = field_value :amount
  with_amount ? sum * amount : sum
end

#field_value(field) ⇒ Object



38
39
40
41
# File 'lib/vat-calculator.rb', line 38

def field_value field
  field_name = self.class.fields_for_calculations[field.to_sym]
  send field_name
end

#fields_values(*fields) ⇒ Object



34
35
36
# File 'lib/vat-calculator.rb', line 34

def fields_values *fields
  fields.map{ |field_name| field_value field_name}
end

#sum_of_vat_for(*args) ⇒ Object



4
5
6
7
8
# File 'lib/vat-calculator.rb', line 4

def sum_of_vat_for *args
  calculate_sum(args) do |price, vat|
    (vat * price) / (100.0 + vat)
  end
end

#sum_with_vat_for(*args) ⇒ Object



16
17
18
19
20
# File 'lib/vat-calculator.rb', line 16

def sum_with_vat_for *args
  calculate_sum(args) do |price, _|
    price
  end
end

#sum_without_vat_for(*args) ⇒ Object



10
11
12
13
14
# File 'lib/vat-calculator.rb', line 10

def sum_without_vat_for *args
  calculate_sum(args) do |price, vat|
    price / (1 + vat / 100.0)
  end
end