Class: Latinum::Resource

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

Overview

A fixed unit in a given named resource

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, name) ⇒ Resource

Returns a new instance of Resource.



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

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

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



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

def amount
  @amount
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.dump(resource) ⇒ Object



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

def dump(resource)
	resource.to_s if resource
end

.load(string) ⇒ Object



122
123
124
125
126
127
128
129
# File 'lib/latinum/resource.rb', line 122

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

.parse(string, default_name: nil) ⇒ Object



116
117
118
119
120
# File 'lib/latinum/resource.rb', line 116

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

Instance Method Details

#*(factor) ⇒ Object



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

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

#+(other) ⇒ Object

By default, we can only add and subtract if the name is the same



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

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

#-(other) ⇒ Object



52
53
54
55
56
# File 'lib/latinum/resource.rb', line 52

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

#-@Object



58
59
60
# File 'lib/latinum/resource.rb', line 58

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

#/(factor) ⇒ Object



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

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 resource or a Numeric value.



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/latinum/resource.rb', line 95

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)


111
112
113
# File 'lib/latinum/resource.rb', line 111

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



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

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



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

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

#inspectObject



90
91
92
# File 'lib/latinum/resource.rb', line 90

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

#to_sObject



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

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

#zero?Boolean

Returns:

  • (Boolean)


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

def zero?
	@amount.zero?
end