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.



15
16
17
18
# File 'lib/latinum/collection.rb', line 15

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’]‘.



23
24
25
# File 'lib/latinum/collection.rb', line 23

def names
  @names
end

#resourcesObject (readonly)

Keeps track of all added resources.



27
28
29
# File 'lib/latinum/collection.rb', line 27

def resources
  @resources
end

Instance Method Details

#-(other) ⇒ Object

Subtract something from this collection.



54
55
56
# File 'lib/latinum/collection.rb', line 54

def - other
	self << -other
end

#-@Object

Allow negation of all values within the collection.



60
61
62
63
64
65
66
67
68
# 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) ⇒ Object Also known as: +

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



37
38
39
40
41
42
43
44
45
46
47
48
# 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

#[](key) ⇒ Object



75
76
77
78
79
# File 'lib/latinum/collection.rb', line 75

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

#[]=(key, amount) ⇒ Object

Set the amount for the specified resource name.



84
85
86
# File 'lib/latinum/collection.rb', line 84

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

#add(resource) ⇒ Object

Add a resource into the totals.



31
32
33
# File 'lib/latinum/collection.rb', line 31

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

#compactObject

Generate a new collection but ignore zero values.



113
114
115
116
117
118
119
120
121
122
123
# 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

#eachObject

Iterates over all the resources.



91
92
93
94
95
96
97
# 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?Boolean

Whether the collection is empty.

Returns:

  • (Boolean)


101
102
103
# File 'lib/latinum/collection.rb', line 101

def empty?
	@resources.empty?
end

#include?(key) ⇒ Boolean

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

Returns:

  • (Boolean)


107
108
109
# File 'lib/latinum/collection.rb', line 107

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

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/latinum/collection.rb', line 70

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

#to_sObject

A human readable representation of the collection. e.g. ‘“5.0 NZD; 10.0 USD”`



128
129
130
# File 'lib/latinum/collection.rb', line 128

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