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
- #<=>(other) ⇒ Object
- #exchange(rate, name, precision = nil) ⇒ Object
-
#initialize(amount, name) ⇒ Resource
constructor
A new instance of Resource.
- #inspect ⇒ Object
- #to_s ⇒ Object
- #zero? ⇒ Boolean
Constructor Details
#initialize(amount, name) ⇒ Resource
31 32 33 34 |
# File 'lib/latinum/resource.rb', line 31 def initialize(amount, name) @amount = amount.to_d @name = name end |
Instance Attribute Details
#amount ⇒ Object (readonly)
Returns the value of attribute amount.
28 29 30 |
# File 'lib/latinum/resource.rb', line 28 def amount @amount end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
29 30 31 |
# File 'lib/latinum/resource.rb', line 29 def name @name end |
Class Method Details
.dump(resource) ⇒ Object
92 93 94 |
# File 'lib/latinum/resource.rb', line 92 def dump(resource) resource.to_s if resource end |
.load(string) ⇒ Object
88 89 90 |
# File 'lib/latinum/resource.rb', line 88 def load(string) parse(string) if string end |
.parse(string, default_name: nil) ⇒ Object
82 83 84 85 86 |
# File 'lib/latinum/resource.rb', line 82 def parse(string, default_name: nil) amount, name = string.split(/\s+/, 2) self.new(amount, name || default_name) end |
Instance Method Details
#*(factor) ⇒ Object
53 54 55 |
# File 'lib/latinum/resource.rb', line 53 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
37 38 39 40 41 |
# File 'lib/latinum/resource.rb', line 37 def + other raise ArgumentError.new("Cannot operate on different currencies!") if @name != other.name self.class.new(@amount + other.amount, @name) end |
#-(other) ⇒ Object
43 44 45 46 47 |
# File 'lib/latinum/resource.rb', line 43 def - other raise ArgumentError.new("Cannot operate on different currencies!") if @name != other.name self.class.new(@amount - other.amount, @name) end |
#-@ ⇒ Object
49 50 51 |
# File 'lib/latinum/resource.rb', line 49 def -@ self.class.new(-@amount, @name) end |
#<=>(other) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/latinum/resource.rb', line 73 def <=> other if @name == other.name @amount <=> other.amount else @name <=> other.name end end |
#exchange(rate, name, precision = nil) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/latinum/resource.rb', line 57 def exchange(rate, name, precision = nil) exchanged_amount = @amount * rate exchanged_amount = exchanged_amount.round(precision) if precision self.class.new(exchanged_amount, name) end |
#inspect ⇒ Object
69 70 71 |
# File 'lib/latinum/resource.rb', line 69 def inspect "<#{self.class.name} #{self.to_s.dump}>" end |
#to_s ⇒ Object
65 66 67 |
# File 'lib/latinum/resource.rb', line 65 def to_s @amount.to_s('F') + ' ' + @name.to_s end |
#zero? ⇒ Boolean
97 98 99 |
# File 'lib/latinum/resource.rb', line 97 def zero? @amount.zero? end |