class Latinum::Collection

Aggregates a set of resources, typically used for summing values to compute a total.

Attributes

names[R]

All resource names which have been added to the collection. e.g. ‘[’NZD’, ‘USD’]‘. @attribute [Set]

resources[R]

Keeps track of all added resources. @attribute [Hash(String, BigDecimal)]

Public Class Methods

new(names = Set.new) click to toggle source

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

+(object)

Add something to this collection.

Alias for: <<
-(other) click to toggle source

Subtract something from this collection.

# File lib/latinum/collection.rb, line 54
def - other
        self << -other
end
-@() click to toggle source

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
<<(object) click to toggle source

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
Also aliased as: +
[](key) click to toggle source

@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
[]=(key, amount) click to toggle source

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(resource) click to toggle source

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
compact() click to toggle source

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
each() { |resource| ... } click to toggle source

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
empty?() click to toggle source

Whether the collection is empty. @returns [Boolean]

# File lib/latinum/collection.rb, line 101
def empty?
        @resources.empty?
end
include?(key) click to toggle source

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
key?(name) click to toggle source
# File lib/latinum/collection.rb, line 70
def key?(name)
        @resources.key?(name)
end
to_s() click to toggle source

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