class Latinum::Resource
A Resource
represents a fixed amount of a named currency or material.
Attributes
The amount of the resource. @attribute [BigDecimal]
The name of the resource. @attribute [String]
Public Class Methods
Dump a string representation of a resource. @parameter resource [Resource] The resource to dump. @returns [String | Nil] A string that represents the {Resource}.
# File lib/latinum/resource.rb, line 38 def self.dump(resource) resource.to_s if resource end
Load a string representation of a resource. @parameter string [String | Nil] e.g. “5 NZD” or nil. @returns [Resource | Nil] The Resource
that represents the parsed string.
# 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
# File lib/latinum/resource.rb, line 44 def initialize(amount, name) @amount = amount.to_d @name = name end
Parse a string representation of a resource. @parameter string [String] e.g “5 NZD”. @returns [Resource] The Resource
that represents the parsed string.
# 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
Public Instance Methods
Multiplies the resource by a given factor. @returns [Resource] A resource with the updated amount.
# File lib/latinum/resource.rb, line 81 def * factor self.class.new(@amount * factor, @name) end
Add two resources. Must have the same name. @returns [Resource] A resource with the added amount.
# File lib/latinum/resource.rb, line 59 def + other raise DifferentResourceNameError if @name != other.name self.class.new(@amount + other.amount, @name) end
Subtract two resources. Must have the same name. @returns [Resource] A resource with the subtracted amount.
# File lib/latinum/resource.rb, line 67 def - other raise DifferentResourceNameError if @name != other.name self.class.new(@amount - other.amount, @name) end
Invert the amount of the resource. @returns [Resource] A resource with the negated amount.
# File lib/latinum/resource.rb, line 75 def -@ self.class.new(-@amount, @name) end
Divides the resource by a given factor. @returns [Resource] A resource with the updated amount.
# 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
Compare with another {Resource} or a Numeric value.
# 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
# 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
Compute a new resource using the given exchange rate for the specified name. @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.
# 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
# File lib/latinum/resource.rb, line 140 def hash [@amount, @name].hash end
# File lib/latinum/resource.rb, line 123 def inspect "#<#{self.class.name} #{self.to_s.dump}>" end
A human readable string representation of the resource amount. @returns [String] e.g. “5.00”.
# File lib/latinum/resource.rb, line 119 def to_digits @amount.to_s('F') end
A human readable string representation of the resource amount and name. @returns [String] e.g. “5 NZD”.
# File lib/latinum/resource.rb, line 113 def to_s "#{@amount.to_s('F')} #{@name}" end
Whether the amount of the resource is zero. @returns [Boolean]
# File lib/latinum/resource.rb, line 150 def zero? @amount.zero? end