Class: Latinum::Collection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#namesObject

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

#resourcesObject (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

#compactObject

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

#eachObject

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.

Returns:

  • (Boolean)


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).

Returns:

  • (Boolean)


120
121
122
# File 'lib/latinum/collection.rb', line 120

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

#to_sObject

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