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 to compute a total.
Instance Attribute Summary collapse
-
#names ⇒ Object
All resource names which have been added to the collection.
-
#resources ⇒ Object
readonly
Keeps track of 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
-
#[]=(key, amount) ⇒ Object
Set the amount for the specified resource name.
-
#add(resource) ⇒ Object
Add a resource into the totals.
-
#compact ⇒ Object
Generate a new collection but ignore zero values.
-
#each ⇒ Object
Iterates over all the resources.
-
#empty? ⇒ Boolean
Whether the collection is empty.
-
#include?(key) ⇒ Boolean
Whether the collection contains the specified resource (may be zero).
-
#initialize(names = Set.new) ⇒ Collection
constructor
Initialize the collection with a given set of resource names.
-
#to_s ⇒ Object
A human readable representation of the collection.
Constructor Details
#initialize(names = Set.new) ⇒ Collection
Initialize the collection with a given set of resource names.
32 33 34 35 |
# File 'lib/latinum/collection.rb', line 32 def initialize(names = Set.new) @names = names @resources = Hash.new {|hash, key| @names << key; BigDecimal(0)} end |
Instance Attribute Details
#names ⇒ Object
All resource names which have been added to the collection. e.g. ‘[’NZD’, ‘USD’]‘.
40 41 42 |
# File 'lib/latinum/collection.rb', line 40 def names @names end |
#resources ⇒ Object (readonly)
Keeps track of all added resources.
44 45 46 |
# File 'lib/latinum/collection.rb', line 44 def resources @resources end |
Instance Method Details
#-(other) ⇒ Object
Subtract something from this collection.
71 72 73 |
# File 'lib/latinum/collection.rb', line 71 def - other self << -other end |
#-@ ⇒ Object
Allow negation of all values within the collection.
77 78 79 80 81 82 83 84 85 |
# File 'lib/latinum/collection.rb', line 77 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.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/latinum/collection.rb', line 54 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
88 89 90 91 92 |
# File 'lib/latinum/collection.rb', line 88 def [] key if amount = @resources[key] Resource.new(@resources[key], key) end end |
#[]=(key, amount) ⇒ Object
Set the amount for the specified resource name.
97 98 99 |
# File 'lib/latinum/collection.rb', line 97 def []= key, amount @resources[key] = amount end |
#add(resource) ⇒ Object
Add a resource into the totals.
48 49 50 |
# File 'lib/latinum/collection.rb', line 48 def add(resource) @resources[resource.name] += resource.amount end |
#compact ⇒ Object
Generate a new collection but ignore zero values.
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/latinum/collection.rb', line 126 def compact collection = self.class.new @resources.each do |key, value| unless value.zero? collection.resources[key] = value end end return collection end |
#each ⇒ Object
Iterates over all the resources.
104 105 106 107 108 109 110 |
# File 'lib/latinum/collection.rb', line 104 def each return to_enum(:each) unless block_given? @resources.each do |key, value| yield Resource.new(value, key) end end |
#empty? ⇒ Boolean
Whether the collection is empty.
114 115 116 |
# File 'lib/latinum/collection.rb', line 114 def empty? @resources.empty? end |
#include?(key) ⇒ Boolean
Whether the collection contains the specified resource (may be zero).
120 121 122 |
# File 'lib/latinum/collection.rb', line 120 def include?(key) @resources.include?(key) end |
#to_s ⇒ Object
A human readable representation of the collection. e.g. ‘“5.0 NZD; 10.0 USD”`
141 142 143 |
# File 'lib/latinum/collection.rb', line 141 def to_s @resources.map{|name, amount| "#{amount.to_s('F')} #{name}"}.join("; ") end |