class Resource
A Resource represents a fixed amount of a named currency or material.
Definitions
def self.parse(string, default_name: nil)
Parse a string representation of a resource.
Signature
-
parameter
string
String
e.g "5 NZD".
-
returns
Resource
The Resource that represents the parsed string.
Implementation
def self.parse(string, default_name: nil)
amount, name = string.split(/\s+/, 2)
self.new(amount, name || default_name)
end
def self.load(input)
Load a string representation of a resource.
Signature
-
parameter
string
String | Nil
e.g. "5 NZD" or nil.
-
returns
Resource | Nil
The Resource that represents the parsed string.
Implementation
def self.load(input)
if input.is_a?(String)
input = input.strip
return parse(input) unless input.empty?
end
end
def self.dump(resource)
Dump a string representation of a resource.
Signature
-
parameter
resource
Resource
The resource to dump.
-
returns
String | Nil
A string that represents the
class Latinum::Resource
.
Implementation
def self.dump(resource)
resource.to_s if resource
end
attr :amount
The amount of the resource.
Signature
-
attribute
BigDecimal
attr :name
The name of the resource.
Signature
-
attribute
String
def + other
Add two resources. Must have the same name.
Signature
-
returns
Resource
A resource with the added amount.
Implementation
def + other
raise DifferentResourceNameError if @name != other.name
self.class.new(@amount + other.amount, @name)
end
def - other
Subtract two resources. Must have the same name.
Signature
-
returns
Resource
A resource with the subtracted amount.
Implementation
def - other
raise DifferentResourceNameError if @name != other.name
self.class.new(@amount - other.amount, @name)
end
def -@
Invert the amount of the resource.
Signature
-
returns
Resource
A resource with the negated amount.
Implementation
def -@
self.class.new(-@amount, @name)
end
def * factor
Multiplies the resource by a given factor.
Signature
-
returns
Resource
A resource with the updated amount.
Implementation
def * factor
self.class.new(@amount * factor, @name)
end
def / factor
Divides the resource by a given factor.
Signature
-
returns
Resource
A resource with the updated amount.
Implementation
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
def exchange(rate, name, precision = nil)
Compute a new resource using the given exchange rate for the specified name.
Signature
-
parameter
rate
Numeric
The exchange rate to use.
-
parameter
name
String
The name of the new resource.
-
parameter
precision
Integer | Nil
The number of decimal places to round to.
Implementation
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
def to_s
A human readable string representation of the resource amount and name.
Signature
-
returns
String
e.g. "5 NZD".
Implementation
def to_s
"#{@amount.to_s('F')} #{@name}"
end
def to_digits
A human readable string representation of the resource amount.
Signature
-
returns
String
e.g. "5.00".
Implementation
def to_digits
@amount.to_s('F')
end
def <=> other
Compare with another class Latinum::Resource
or a Numeric value.
Implementation
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
def zero?
Whether the amount of the resource is zero.
Signature
-
returns
Boolean
Implementation
def zero?
@amount.zero?
end