Class: Differential::Calculator::Group

Inherits:
Object
  • Object
show all
Includes:
HasTotals
Defined in:
lib/differential/calculator/group.rb

Overview

A Report has 0 or more Group objects and a Group has 0 or more Item objects. Report -> Group -> Item A Group is, as the name implies, a grouping of items. It is up to the consumer application to define how to group (i.e. group based on this attribute’s value or group based on these two attributes’ values.)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasTotals

#totals

Constructor Details

#initialize(id) ⇒ Group

Returns a new instance of Group.

Raises:

  • (ArgumentError)


22
23
24
25
26
# File 'lib/differential/calculator/group.rb', line 22

def initialize(id)
  raise ArgumentError, 'id is required' unless id

  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



20
21
22
# File 'lib/differential/calculator/group.rb', line 20

def id
  @id
end

Instance Method Details

#add(record, side) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
# File 'lib/differential/calculator/group.rb', line 32

def add(record, side)
  raise ArgumentError, 'record is required' unless record
  raise ArgumentError, 'side is required'   unless side
  raise ArgumentError, "mismatch: #{record.group_id} != #{id}" if id != record.group_id

  totals.add(record.value, side)

  upsert_item(record, side)

  self
end

#itemsObject



28
29
30
# File 'lib/differential/calculator/group.rb', line 28

def items
  items_by_id.values
end