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.



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

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

Instance Attribute Details

#amountObject

The amount of the resource.



51
52
53
# File 'lib/latinum/resource.rb', line 51

def amount
  @amount
end

#nameObject (readonly)

The name of the resource.



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

def name
  @name
end

Class Method Details

.dump(resource) ⇒ Object

Dump a string representation of a resource.



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

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

.load(input) ⇒ Object

Load a string representation of a resource.



28
29
30
31
32
33
# File 'lib/latinum/resource.rb', line 28

def self.load(input)
	if input.is_a?(String)
		input = input.strip
		return parse(input) unless input.empty?
	end
end

.parse(string, default_name: nil) ⇒ Object

Parse a string representation of a resource.



19
20
21
22
23
# File 'lib/latinum/resource.rb', line 19

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.



81
82
83
# File 'lib/latinum/resource.rb', line 81

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

#+(other) ⇒ Object

Add two resources. Must have the same name.



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

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.



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

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

#-@Object

Invert the amount of the resource.



75
76
77
# File 'lib/latinum/resource.rb', line 75

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

#/(factor) ⇒ Object

Divides the resource by a given factor.



87
88
89
90
91
92
93
94
95
# File 'lib/latinum/resource.rb', line 87

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.



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/latinum/resource.rb', line 128

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)


144
145
146
# File 'lib/latinum/resource.rb', line 144

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.



101
102
103
104
105
106
107
108
109
# File 'lib/latinum/resource.rb', line 101

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



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

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

#inspectObject



123
124
125
# File 'lib/latinum/resource.rb', line 123

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

#to_digitsObject

A human readable string representation of the resource amount.



119
120
121
# File 'lib/latinum/resource.rb', line 119

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

#to_sObject

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



113
114
115
# File 'lib/latinum/resource.rb', line 113

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

#zero?Boolean

Whether the amount of the resource is zero.

Returns:

  • (Boolean)


150
151
152
# File 'lib/latinum/resource.rb', line 150

def zero?
	@amount.zero?
end