Class: Latinum::Resource
- Inherits:
-
Object
- Object
- Latinum::Resource
- Includes:
- Comparable
- Defined in:
- lib/latinum/resource.rb
Overview
A fixed unit in a given named resource
Instance Attribute Summary collapse
-
#amount ⇒ Object
readonly
Returns the value of attribute amount.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #*(factor) ⇒ Object
-
#+(other) ⇒ Object
By default, we can only add and subtract if the name is the same.
- #-(other) ⇒ Object
- #-@ ⇒ Object
- #/(factor) ⇒ Object
- #<=>(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #exchange(rate, name, precision = nil) ⇒ Object
- #hash ⇒ Object
-
#initialize(amount, name) ⇒ Resource
constructor
A new instance of Resource.
- #inspect ⇒ Object
- #to_s ⇒ Object
- #zero? ⇒ Boolean
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
#amount ⇒ Object (readonly)
Returns the value of attribute amount.
35 36 37 |
# File 'lib/latinum/resource.rb', line 35 def amount @amount end |
#name ⇒ Object (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
121 122 123 |
# File 'lib/latinum/resource.rb', line 121 def dump(resource) resource.to_s if resource end |
.load(string) ⇒ Object
117 118 119 |
# File 'lib/latinum/resource.rb', line 117 def load(string) parse(string) if string end |
.parse(string, default_name: nil) ⇒ Object
111 112 113 114 115 |
# File 'lib/latinum/resource.rb', line 111 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
92 93 94 95 96 97 98 99 100 |
# File 'lib/latinum/resource.rb', line 92 def <=> other return nil unless other and other.is_a? self.class if @name == other.name @amount <=> other.amount else @name <=> other.name end end |
#eql?(other) ⇒ Boolean
106 107 108 |
# File 'lib/latinum/resource.rb', line 106 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 |
#hash ⇒ Object
102 103 104 |
# File 'lib/latinum/resource.rb', line 102 def hash [@amount, @name].hash end |
#inspect ⇒ Object
88 89 90 |
# File 'lib/latinum/resource.rb', line 88 def inspect "#<#{self.class.name} #{self.to_s.dump}>" end |
#to_s ⇒ Object
84 85 86 |
# File 'lib/latinum/resource.rb', line 84 def to_s @amount.to_s('F') + ' ' + @name.to_s end |
#zero? ⇒ Boolean
126 127 128 |
# File 'lib/latinum/resource.rb', line 126 def zero? @amount.zero? end |