class Aggregator

Public Class Methods

aggregate(&block) click to toggle source
# File lib/guerrilla_patch/aggregate_by_type/aggregator.rb, line 5
def self.aggregate(&block)
  agregator = Aggregator.new
  block.call(agregator)
  return Amount.new(agregator.total,agregator.total_by_type)
end
aggregate_full_precistion(&block) click to toggle source
# File lib/guerrilla_patch/aggregate_by_type/aggregator.rb, line 11
def self.aggregate_full_precistion(&block)
  agregator = Aggregator.new
  block.call(agregator)
  return Amount.new(agregator.total,agregator.total_by_type)
end

Private Class Methods

agregate_by_type(lista) click to toggle source

hey don’t judge me, yes I know I am a bad man

# File lib/guerrilla_patch/aggregate_by_type/aggregator.rb, line 55
def self.agregate_by_type(lista)
 return {} if lista[0].class != Hash

 result = lista.inject { |memo, el| self.merge_by_key(memo, el) }
 (result.nil? ? {} : result).reject { |key, value| value == 0 }
end
merge_by_key(memo, el) click to toggle source
# File lib/guerrilla_patch/aggregate_by_type/aggregator.rb, line 62
def self.merge_by_key(memo, el)
  memo.merge( el ) { |key, old_v, new_v| old_v + new_v }
end
prepare(amount) click to toggle source
# File lib/guerrilla_patch/aggregate_by_type/aggregator.rb, line 66
def self.prepare(amount)
  amount.class == Amount ? amount.divide : amount
end

Public Instance Methods

add(amount) click to toggle source
# File lib/guerrilla_patch/aggregate_by_type/aggregator.rb, line 17
def add(amount)
  total_list << Aggregator.prepare(amount)
end
add_full_precision(amount) click to toggle source
# File lib/guerrilla_patch/aggregate_by_type/aggregator.rb, line 21
def add_full_precision(amount)
  raise "Full precision can be done only on amount class. Class sended was: #{amount.class} with value: #{amount.inspect}" if amount.class != Amount
  total_list << amount.by_type
end
subtract(amount) click to toggle source
# File lib/guerrilla_patch/aggregate_by_type/aggregator.rb, line 26
def subtract(amount)
  add(amount.negative)
end
subtract_full_precision(amount) click to toggle source
# File lib/guerrilla_patch/aggregate_by_type/aggregator.rb, line 30
def subtract_full_precision(amount)
  add_full_precision(amount.negative)
end
total() click to toggle source
# File lib/guerrilla_patch/aggregate_by_type/aggregator.rb, line 34
def total
  (by_type? ? total_by_type : total_list).sum_me
end
total_by_type() click to toggle source
# File lib/guerrilla_patch/aggregate_by_type/aggregator.rb, line 38
def total_by_type
  Aggregator.agregate_by_type(total_list)
end

Private Instance Methods

by_type?() click to toggle source
# File lib/guerrilla_patch/aggregate_by_type/aggregator.rb, line 44
def by_type?
  return false if @total_list == []

  total_list[0].class == Hash
end
total_list() click to toggle source
# File lib/guerrilla_patch/aggregate_by_type/aggregator.rb, line 50
def total_list
  @total_list ||= []
end