Class: CoopAl::Value
- Inherits:
-
Object
- Object
- CoopAl::Value
- Defined in:
- lib/coop_al/value.rb
Overview
Value
Instance Attribute Summary collapse
-
#copper ⇒ Object
readonly
Returns the value of attribute copper.
-
#electrum ⇒ Object
readonly
Returns the value of attribute electrum.
-
#gold ⇒ Object
readonly
Returns the value of attribute gold.
-
#platinum ⇒ Object
readonly
Returns the value of attribute platinum.
-
#silver ⇒ Object
readonly
Returns the value of attribute silver.
Class Method Summary collapse
- .copper(amount) ⇒ Object
- .electrum(amount) ⇒ Object
- .gold(amount) ⇒ Object
- .platinum(amount) ⇒ Object
- .silver(amount) ⇒ Object
Instance Method Summary collapse
- #*(other) ⇒ Object
- #+(other) ⇒ Object
- #/(other) ⇒ Object
-
#cp ⇒ Object
Returns the value of attribute copper.
-
#ep ⇒ Object
Returns the value of attribute electrum.
-
#gp ⇒ Object
Returns the value of attribute gold.
-
#initialize(amounts = {}) ⇒ Value
constructor
A new instance of Value.
- #nonzero? ⇒ Boolean
- #normalize ⇒ Object
- #normalize_from(value) ⇒ Object
-
#pp ⇒ Object
Returns the value of attribute platinum.
- #raw_value ⇒ Object
-
#sp ⇒ Object
Returns the value of attribute silver.
- #to_a ⇒ Object
- #to_s ⇒ Object
- #zero? ⇒ Boolean
Constructor Details
#initialize(amounts = {}) ⇒ Value
Returns a new instance of Value.
8 9 10 11 12 13 14 15 |
# File 'lib/coop_al/value.rb', line 8 def initialize(amounts = {}) @platinum = amounts[:platinum] || 0 @electrum = amounts[:electrum] || 0 @gold = amounts[:gold] || 0 @silver = amounts[:silver] || 0 @copper = amounts[:copper] || 0 normalize end |
Instance Attribute Details
#copper ⇒ Object (readonly)
Returns the value of attribute copper.
6 7 8 |
# File 'lib/coop_al/value.rb', line 6 def copper @copper end |
#electrum ⇒ Object (readonly)
Returns the value of attribute electrum.
6 7 8 |
# File 'lib/coop_al/value.rb', line 6 def electrum @electrum end |
#gold ⇒ Object (readonly)
Returns the value of attribute gold.
6 7 8 |
# File 'lib/coop_al/value.rb', line 6 def gold @gold end |
#platinum ⇒ Object (readonly)
Returns the value of attribute platinum.
6 7 8 |
# File 'lib/coop_al/value.rb', line 6 def platinum @platinum end |
#silver ⇒ Object (readonly)
Returns the value of attribute silver.
6 7 8 |
# File 'lib/coop_al/value.rb', line 6 def silver @silver end |
Class Method Details
.copper(amount) ⇒ Object
83 84 85 |
# File 'lib/coop_al/value.rb', line 83 def self.copper(amount) Value.new(copper: amount) end |
.electrum(amount) ⇒ Object
87 88 89 |
# File 'lib/coop_al/value.rb', line 87 def self.electrum(amount) Value.new(electrum: amount) end |
.gold(amount) ⇒ Object
75 76 77 |
# File 'lib/coop_al/value.rb', line 75 def self.gold(amount) Value.new(gold: amount) end |
Instance Method Details
#*(other) ⇒ Object
61 62 63 64 |
# File 'lib/coop_al/value.rb', line 61 def *(other) normalize_from(raw_value * other) self end |
#+(other) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/coop_al/value.rb', line 51 def +(other) @platinum += other.pp @electrum += other.ep @gold += other.gp @silver += other.sp @copper += other.cp normalize self end |
#/(other) ⇒ Object
66 67 68 69 |
# File 'lib/coop_al/value.rb', line 66 def /(other) normalize_from(raw_value / other) self end |
#cp ⇒ Object
Returns the value of attribute copper.
21 22 23 |
# File 'lib/coop_al/value.rb', line 21 def copper @copper end |
#ep ⇒ Object
Returns the value of attribute electrum.
18 19 20 |
# File 'lib/coop_al/value.rb', line 18 def electrum @electrum end |
#gp ⇒ Object
Returns the value of attribute gold.
19 20 21 |
# File 'lib/coop_al/value.rb', line 19 def gold @gold end |
#nonzero? ⇒ Boolean
27 28 29 |
# File 'lib/coop_al/value.rb', line 27 def nonzero? !zero? end |
#normalize ⇒ Object
47 48 49 |
# File 'lib/coop_al/value.rb', line 47 def normalize normalize_from(raw_value) end |
#normalize_from(value) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/coop_al/value.rb', line 35 def normalize_from(value) @platinum = 0 @electrum = 0 @gold = value.floor value -= @gold value *= 10 @silver = value.floor value -= @silver value *= 10 @copper = value.floor end |
#pp ⇒ Object
Returns the value of attribute platinum.
17 18 19 |
# File 'lib/coop_al/value.rb', line 17 def platinum @platinum end |
#raw_value ⇒ Object
31 32 33 |
# File 'lib/coop_al/value.rb', line 31 def raw_value @platinum * 10 + @gold + @silver / 10.0 + @copper / 100.0 + @electrum / 2.0 end |
#sp ⇒ Object
Returns the value of attribute silver.
20 21 22 |
# File 'lib/coop_al/value.rb', line 20 def silver @silver end |
#to_a ⇒ Object
91 92 93 |
# File 'lib/coop_al/value.rb', line 91 def to_a [@platinum, @electrum, @gold, @silver, @copper] end |
#to_s ⇒ Object
95 96 97 98 99 100 101 102 103 |
# File 'lib/coop_al/value.rb', line 95 def to_s [ [@platinum, 'pp'], [@gold, 'gp'], [@silver, 'sp'], [@copper, 'cp'], [@electrum, 'ep'] ].select { |v| v[0].nonzero? }.map { |v| "#{v[0]}#{v[1]}" }.join(', ') end |
#zero? ⇒ Boolean
23 24 25 |
# File 'lib/coop_al/value.rb', line 23 def zero? raw_value.zero? end |