Class: Latinum::Resource

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/latinum/resource.rb

Overview

A Resource represents a fixed amount of a named currency or material.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, name) ⇒ Resource

Returns a new instance of Resource.



61
62
63
64
# File 'lib/latinum/resource.rb', line 61

def initialize(amount, name)
	@amount = amount.to_d
	@name = name
end

Instance Attribute Details

#amountObject

The amount of the resource.



68
69
70
# File 'lib/latinum/resource.rb', line 68

def amount
  @amount
end

#nameObject (readonly)

The name of the resource.



72
73
74
# File 'lib/latinum/resource.rb', line 72

def name
  @name
end

Class Method Details

.dump(resource) ⇒ Object

Dump a string representatino of a resource.



55
56
57
# File 'lib/latinum/resource.rb', line 55

def self.dump(resource)
	resource.to_s if resource
end

.load(string) ⇒ Object

Load a string representation of a resource.



43
44
45
46
47
48
49
50
# File 'lib/latinum/resource.rb', line 43

def self.load(string)
	if string
		# Remove any whitespaces
		string = string.strip
		
		parse(string) unless string.empty?
	end
end

.parse(string, default_name: nil) ⇒ Object

Parse a string representation of a resource.



34
35
36
37
38
# File 'lib/latinum/resource.rb', line 34

def self.parse(string, default_name: nil)
	amount, name = string.split(/\s+/, 2)
	
	self.new(amount, name || default_name)
end

Instance Method Details

#*(factor) ⇒ Object

Multiplies the resource by a given factor.



98
99
100
# File 'lib/latinum/resource.rb', line 98

def * factor
	self.class.new(@amount * factor, @name)
end

#+(other) ⇒ Object

Add two resources. Must have the same name.



76
77
78
79
80
# File 'lib/latinum/resource.rb', line 76

def + other
	raise DifferentResourceNameError if @name != other.name
	
	self.class.new(@amount + other.amount, @name)
end

#-(other) ⇒ Object

Subtract two resources. Must have the same name.



84
85
86
87
88
# File 'lib/latinum/resource.rb', line 84

def - other
	raise DifferentResourceNameError if @name != other.name
	
	self.class.new(@amount - other.amount, @name)
end

#-@Object

Invert the amount of the resource.



92
93
94
# File 'lib/latinum/resource.rb', line 92

def -@
	self.class.new(-@amount, @name)
end

#/(factor) ⇒ Object

Divides the resource by a given factor.



104
105
106
107
108
109
110
111
112
# File 'lib/latinum/resource.rb', line 104

def / factor
	if factor.is_a? self.class
		raise DifferentResourceNameError if @name != factor.name
		
		@amount / factor.amount
	else
		self.class.new(@amount / factor, @name)
	end
end

#<=>(other) ⇒ Object

Compare with another Latinum::Resource or a Numeric value.



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/latinum/resource.rb', line 145

def <=> other
	if other.is_a? self.class
		result = @amount <=> other.amount
		return result unless result == 0
		
		result = @name <=> other.name
		return result
	elsif other.is_a? Numeric
		@amount <=> other
	end
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/latinum/resource.rb', line 161

def eql? other
	self.class.eql?(other.class) and @name.eql?(other.name) and @amount.eql?(other.amount)
end

#exchange(rate, name, precision = nil) ⇒ Object

Compute a new resource using the given exchange rate for the specified name.



118
119
120
121
122
123
124
125
126
# File 'lib/latinum/resource.rb', line 118

def exchange(rate, name, precision = nil)
	return self if @name == name
	
	exchanged_amount = @amount * rate
	
	exchanged_amount = exchanged_amount.round(precision) if precision
	
	self.class.new(exchanged_amount, name)
end

#hashObject



157
158
159
# File 'lib/latinum/resource.rb', line 157

def hash
	[@amount, @name].hash
end

#inspectObject



140
141
142
# File 'lib/latinum/resource.rb', line 140

def inspect
	"#<#{self.class.name} #{self.to_s.dump}>"
end

#to_digitsObject

A human readable string representation of the resource amount.



136
137
138
# File 'lib/latinum/resource.rb', line 136

def to_digits
	@amount.to_s('F')
end

#to_sObject

A human readable string representation of the resource amount and name.



130
131
132
# File 'lib/latinum/resource.rb', line 130

def to_s
	"#{@amount.to_s('F')} #{@name}"
end

#zero?Boolean

Whether the amount of the resource is zero.

Returns:

  • (Boolean)


167
168
169
# File 'lib/latinum/resource.rb', line 167

def zero?
	@amount.zero?
end