class Latinum::Collection
Aggregates a set of resources, typically used for summing values to compute a total.
Attributes
All resource names which have been added to the collection. e.g. ‘[’NZD’, ‘USD’]‘. @attribute [Set]
Keeps track of all added resources. @attribute [Hash(String, BigDecimal)]
Public Class Methods
Initialize the collection with a given set of resource names.
# File lib/latinum/collection.rb, line 15 def initialize(names = Set.new) @names = names @resources = Hash.new {|hash, key| @names << key; BigDecimal(0)} end
Public Instance Methods
Subtract something from this collection.
# File lib/latinum/collection.rb, line 54 def - other self << -other end
Allow negation of all values within the collection. @returns [Collection] A new collection with the inverted values.
# File lib/latinum/collection.rb, line 60 def -@ collection = self.class.new @resources.each do |key, value| collection.resources[key] = -value end return collection end
Add a resource, an array of resources, or another collection into this one. @parameter object [Resource | Array(Resource
) | Collection] The resource(s) to add.
# File lib/latinum/collection.rb, line 37 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
@returns [Resource | Nil] A resource for the specified name.
# File lib/latinum/collection.rb, line 75 def [] key if amount = @resources[key] Resource.new(@resources[key], key) end end
Set the amount for the specified resource name. @parameter key [String] The resource name. @parameter value [BigDecimal] The resource amount.
# File lib/latinum/collection.rb, line 84 def []= key, amount @resources[key] = amount end
Add a resource into the totals. @parameter resource [Resource] The resource to add.
# File lib/latinum/collection.rb, line 31 def add(resource) @resources[resource.name] += resource.amount end
Generate a new collection but ignore zero values. @returns [Collection] A new collection.
# File lib/latinum/collection.rb, line 113 def compact collection = self.class.new @resources.each do |key, value| unless value.zero? collection.resources[key] = value end end return collection end
Iterates over all the resources. @yields {|resource| …} The resources if a block is given.
@parameter resource [Resource]
# File lib/latinum/collection.rb, line 91 def each return to_enum(:each) unless block_given? @resources.each do |key, value| yield Resource.new(value, key) end end
Whether the collection is empty. @returns [Boolean]
# File lib/latinum/collection.rb, line 101 def empty? @resources.empty? end
Whether the collection contains the specified resource (may be zero). @returns [Boolean]
# File lib/latinum/collection.rb, line 107 def include?(key) @resources.include?(key) end
# File lib/latinum/collection.rb, line 70 def key?(name) @resources.key?(name) end
A human readable representation of the collection. e.g. ‘“5.0 NZD; 10.0 USD”` @returns [String]
# File lib/latinum/collection.rb, line 128 def to_s @resources.map{|name, amount| "#{amount.to_s('F')} #{name}"}.join("; ") end