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.



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

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

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



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

def amount
  @amount
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.dump(resource) ⇒ Object



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

def dump(resource)
	resource.to_s if resource
end

.load(string) ⇒ Object



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

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

.parse(string, default_name: nil) ⇒ Object



114
115
116
117
118
# File 'lib/latinum/resource.rb', line 114

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

Instance Method Details

#*(factor) ⇒ Object



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

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



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

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

#-(other) ⇒ Object



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

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

#-@Object



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

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

#/(factor) ⇒ Object



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

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.



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

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)


109
110
111
# File 'lib/latinum/resource.rb', line 109

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



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

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



105
106
107
# File 'lib/latinum/resource.rb', line 105

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

#inspectObject



88
89
90
# File 'lib/latinum/resource.rb', line 88

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

#to_sObject



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

def to_s
	@amount.to_s('F') + ' ' + @name.to_s
end

#zero?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/latinum/resource.rb', line 134

def zero?
	@amount.zero?
end