class Latinum::Resource

A Resource represents a fixed amount of a named currency or material.

Attributes

amount[R]

The amount of the resource. @attribute [BigDecimal]

name[R]

The name of the resource. @attribute [String]

Public Class Methods

dump(resource) click to toggle source

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(input) click to toggle source

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
new(amount, name) click to toggle source
# File lib/latinum/resource.rb, line 44
def initialize(amount, name)
        @amount = amount.to_d
        @name = name
end
parse(string, default_name: nil) click to toggle source

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

*(factor) click to toggle source

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
+(other) click to toggle source

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
-(other) click to toggle source

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
-@() click to toggle source

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
/(factor) click to toggle source

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
<=>(other) click to toggle source

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
eql?(other) click to toggle source
# 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) click to toggle source

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
hash() click to toggle source
# File lib/latinum/resource.rb, line 140
def hash
        [@amount, @name].hash
end
inspect() click to toggle source
# File lib/latinum/resource.rb, line 123
def inspect
        "#<#{self.class.name} #{self.to_s.dump}>"
end
to_digits() click to toggle source

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
to_s() click to toggle source

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
zero?() click to toggle source

Whether the amount of the resource is zero. @returns [Boolean]

# File lib/latinum/resource.rb, line 150
def zero?
        @amount.zero?
end