Class: Latinum::Collection
- Inherits:
-
Object
- Object
- Latinum::Collection
- Includes:
- Enumerable
- Defined in:
- lib/latinum/collection.rb
Overview
Aggregates a set of resources, typically used for summing values.
Instance Attribute Summary collapse
-
#names ⇒ Object
readonly
All resource names which have been added to the collection, e.g.
-
#resources ⇒ Object
readonly
A map of ‘name` => `total` for all added resources.
Instance Method Summary collapse
-
#-(other) ⇒ Object
Subtract something from this collection.
-
#-@ ⇒ Object
Allow negation of all values within the collection:.
-
#<<(object) ⇒ Object
(also: #+)
Add a resource, an array of resources, or another collection into this one.
-
#[](key) ⇒ Object
Get a ‘Resource` for the given name:.
-
#[]=(key, amount) ⇒ Object
Set a ‘BigDecimal` value for the given name:.
-
#add(resource) ⇒ Object
Add a resource into the totals.
-
#compact ⇒ Object
Generate a new collection but ignore zero values.
- #each ⇒ Object
-
#initialize(names = Set.new) ⇒ Collection
constructor
Initialize the collection with a given set of resource names.
Constructor Details
#initialize(names = Set.new) ⇒ Collection
Initialize the collection with a given set of resource names.
30 31 32 33 |
# File 'lib/latinum/collection.rb', line 30 def initialize(names = Set.new) @names = names @resources = Hash.new {|hash, key| @names << key; BigDecimal.new("0")} end |
Instance Attribute Details
#names ⇒ Object (readonly)
All resource names which have been added to the collection, e.g. ‘[’NZD’, ‘USD’]‘.
36 37 38 |
# File 'lib/latinum/collection.rb', line 36 def names @names end |
#resources ⇒ Object (readonly)
A map of ‘name` => `total` for all added resources. Totals are stored as BigDecimal instances.
39 40 41 |
# File 'lib/latinum/collection.rb', line 39 def resources @resources end |
Instance Method Details
#-(other) ⇒ Object
Subtract something from this collection.
64 65 66 |
# File 'lib/latinum/collection.rb', line 64 def - other self << -other end |
#-@ ⇒ Object
Allow negation of all values within the collection:
69 70 71 72 73 74 75 76 77 |
# File 'lib/latinum/collection.rb', line 69 def -@ collection = self.class.new @resources.each do |key, value| collection.resources[key] = -value end return collection end |
#<<(object) ⇒ Object Also known as: +
Add a resource, an array of resources, or another collection into this one.
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/latinum/collection.rb', line 47 def << object case object when Resource add(object) when Array object.each { |resource| add(resource) } when Collection object.resources.each { |name, amount| @resources[name] += amount } end return self end |
#[](key) ⇒ Object
Get a ‘Resource` for the given name:
80 81 82 |
# File 'lib/latinum/collection.rb', line 80 def [] key Resource.new(@resources[key], key) end |
#[]=(key, amount) ⇒ Object
Set a ‘BigDecimal` value for the given name:
85 86 87 |
# File 'lib/latinum/collection.rb', line 85 def []= key, amount @resources[key] = amount end |
#add(resource) ⇒ Object
Add a resource into the totals.
42 43 44 |
# File 'lib/latinum/collection.rb', line 42 def add resource @resources[resource.name] += resource.amount end |
#compact ⇒ Object
Generate a new collection but ignore zero values.
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/latinum/collection.rb', line 98 def compact collection = self.class.new @resources.each do |key, value| unless value.zero? collection.resources[key] = value end end return collection end |