Class: Shushu::UnitGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/models/unit_group.rb

Constant Summary collapse

InvalidUnitGroup =
Class.new(ShushuException)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(units) ⇒ UnitGroup

Returns a new instance of UnitGroup.



7
8
9
10
# File 'lib/models/unit_group.rb', line 7

def initialize(units)
  @units = units
  check_rates
end

Instance Attribute Details

#unitsObject (readonly)

Returns the value of attribute units.



5
6
7
# File 'lib/models/unit_group.rb', line 5

def units
  @units
end

Instance Method Details

#check_ratesObject



12
13
14
15
16
# File 'lib/models/unit_group.rb', line 12

def check_rates
  if @units.map(&:rate).uniq.length > 1
    raise(InvalidUnitGroup, "Rates must be homogenius.")
  end
end

#descriptionObject



50
51
52
# File 'lib/models/unit_group.rb', line 50

def description
  sample_unit.product_name
end

#hidObject



38
39
40
# File 'lib/models/unit_group.rb', line 38

def hid
  sample_unit.hid
end

#nameObject



34
35
36
# File 'lib/models/unit_group.rb', line 34

def name
  sample_unit.product_name
end

#product_groupObject



46
47
48
# File 'lib/models/unit_group.rb', line 46

def product_group
  sample_unit.product_group
end

#product_nameObject



42
43
44
# File 'lib/models/unit_group.rb', line 42

def product_name
  sample_unit.product_name
end

#qtyObject



22
23
24
# File 'lib/models/unit_group.rb', line 22

def qty
  @units.map(&:qty).reduce(:+)
end

#rateObject



26
27
28
# File 'lib/models/unit_group.rb', line 26

def rate
  sample_unit.rate
end

#rate_periodObject



30
31
32
# File 'lib/models/unit_group.rb', line 30

def rate_period
  sample_unit.rate_period
end

#sample_unitObject



54
55
56
# File 'lib/models/unit_group.rb', line 54

def sample_unit
  @sample_unit ||= @units.sample
end

#totalObject



18
19
20
# File 'lib/models/unit_group.rb', line 18

def total
  rate * qty
end