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.

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 (readonly)

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



38
39
40
# File 'lib/latinum/collection.rb', line 38

def names
  @names
end

#resourcesObject (readonly)

A map of ‘name` => `total` for all added resources. Totals are stored as BigDecimal instances.



41
42
43
# File 'lib/latinum/collection.rb', line 41

def resources
  @resources
end

Instance Method Details

#-(other) ⇒ Object

Subtract something from this collection.



66
67
68
# File 'lib/latinum/collection.rb', line 66

def - other
	self << -other
end

#-@Object

Allow negation of all values within the collection:



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

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.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/latinum/collection.rb', line 49

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

Get a ‘Resource` for the given name:



82
83
84
# File 'lib/latinum/collection.rb', line 82

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

#[]=(key, amount) ⇒ Object

Set a ‘BigDecimal` value for the given name:



87
88
89
# File 'lib/latinum/collection.rb', line 87

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

#add(resource) ⇒ Object

Add a resource into the totals.



44
45
46
# File 'lib/latinum/collection.rb', line 44

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

#compactObject

Generate a new collection but ignore zero values.



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/latinum/collection.rb', line 108

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

#eachObject



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

Returns:

  • (Boolean)


99
100
101
# File 'lib/latinum/collection.rb', line 99

def empty?
	@resources.empty?
end

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/latinum/collection.rb', line 103

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

#to_sObject



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

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