Class: Shushu::UnitGroup
- Inherits:
-
Object
- Object
- Shushu::UnitGroup
- Defined in:
- lib/models/unit_group.rb
Constant Summary collapse
- InvalidUnitGroup =
Class.new(ShushuException)
Instance Attribute Summary collapse
-
#units ⇒ Object
readonly
Returns the value of attribute units.
Instance Method Summary collapse
- #check_rates ⇒ Object
- #description ⇒ Object
- #hid ⇒ Object
-
#initialize(units) ⇒ UnitGroup
constructor
A new instance of UnitGroup.
- #name ⇒ Object
- #product_group ⇒ Object
- #product_name ⇒ Object
- #qty ⇒ Object
- #rate ⇒ Object
- #rate_period ⇒ Object
- #sample_unit ⇒ Object
- #total ⇒ Object
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
#units ⇒ Object (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_rates ⇒ Object
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 |
#description ⇒ Object
50 51 52 |
# File 'lib/models/unit_group.rb', line 50 def description sample_unit.product_name end |
#hid ⇒ Object
38 39 40 |
# File 'lib/models/unit_group.rb', line 38 def hid sample_unit.hid end |
#name ⇒ Object
34 35 36 |
# File 'lib/models/unit_group.rb', line 34 def name sample_unit.product_name end |
#product_group ⇒ Object
46 47 48 |
# File 'lib/models/unit_group.rb', line 46 def product_group sample_unit.product_group end |
#product_name ⇒ Object
42 43 44 |
# File 'lib/models/unit_group.rb', line 42 def product_name sample_unit.product_name end |
#qty ⇒ Object
22 23 24 |
# File 'lib/models/unit_group.rb', line 22 def qty @units.map(&:qty).reduce(:+) end |
#rate ⇒ Object
26 27 28 |
# File 'lib/models/unit_group.rb', line 26 def rate sample_unit.rate end |
#rate_period ⇒ Object
30 31 32 |
# File 'lib/models/unit_group.rb', line 30 def rate_period sample_unit.rate_period end |
#sample_unit ⇒ Object
54 55 56 |
# File 'lib/models/unit_group.rb', line 54 def sample_unit @sample_unit ||= @units.sample end |
#total ⇒ Object
18 19 20 |
# File 'lib/models/unit_group.rb', line 18 def total rate * qty end |