Latinum SourceLatinumCollection

class Collection

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

Definitions

def initialize(names = Set.new)

Initialize the collection with a given set of resource names.

Implementation

def initialize(names = Set.new)
	@names = names
	@resources = Hash.new {|hash, key| @names << key; BigDecimal(0)}
end

attr :names

All resource names which have been added to the collection. e.g. ['NZD', 'USD'].

Signature

attribute Set

attr :resources

Keeps track of all added resources.

Signature

attribute Hash(String, BigDecimal)

def add(resource)

Add a resource into the totals.

Signature

parameter resource Resource

The resource to add.

Implementation

def add(resource)
	@resources[resource.name] += resource.amount
end

def <<(object)

Add a resource, an array of resources, or another collection into this one.

Signature

parameter object Resource | Array(Resource) | Collection

The resource(s) to add.

Implementation

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

def - other

Subtract something from this collection.

Implementation

def - other
	self << -other
end

def -@

Allow negation of all values within the collection.

Signature

returns Collection

A new collection with the inverted values.

Implementation

def -@
	collection = self.class.new
	
	@resources.each do |key, value|
		collection.resources[key] = -value
	end
	
	return collection
end

def [] key

Signature

returns Resource | Nil

A resource for the specified name.

Implementation

def [] key
	if amount = @resources[key]
		Resource.new(@resources[key], key)
	end
end

def []= key, amount

Set the amount for the specified resource name.

Signature

parameter key String

The resource name.

parameter value BigDecimal

The resource amount.

Implementation

def []= key, amount
	@resources[key] = amount
end

def each

Iterates over all the resources.

Signature

yields {|resource| ...}

The resources if a block is given.

parameter resource Resource

Implementation

def each
	return to_enum(:each) unless block_given?
	
	@resources.each do |key, value|
		yield Resource.new(value, key)
	end
end

def empty?

Whether the collection is empty.

Signature

returns Boolean

Implementation

def empty?
	@resources.empty?
end

def include?(key)

Whether the collection contains the specified resource (may be zero).

Signature

returns Boolean

Implementation

def include?(key)
	@resources.include?(key)
end

def compact

Generate a new collection but ignore zero values.

Signature

returns Collection

A new collection.

Implementation

def compact
	collection = self.class.new
	
	@resources.each do |key, value|
		unless value.zero?
			collection.resources[key] = value
		end
	end
	
	return collection
end

def to_s

A human readable representation of the collection. e.g. "5.0 NZD; 10.0 USD"

Signature

returns String

Implementation

def to_s
	@resources.map{|name, amount| "#{amount.to_s('F')} #{name}"}.join("; ")
end