wowecon 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,16 +5,19 @@ module Wowecon
5
5
  include Wowecon::CurrencyHelpers
6
6
 
7
7
  def initialize(value)
8
- if value.class == Fixnum
9
- @value = value
10
- elsif value.class == Float
11
- @value = sprintf("%.4f", value).split(".").join("").to_i
12
- elsif value.class == Hash
8
+ @value = case
9
+ when value.kind_of?(Fixnum)
10
+ value
11
+ when value.kind_of?(Float)
12
+ sprintf("%.4f", value).split(".").join("").to_i
13
+ when value.kind_of?(Hash)
13
14
  if value.key?(:gold) and value.key?(:silver) and value.key?(:copper)
14
- @value = "#{value[:gold]}#{sprintf("%02d", value[:silver])}#{sprintf("%02d", value[:copper])}".to_i
15
+ "#{value[:gold]}#{sprintf("%02d", value[:silver])}#{sprintf("%02d", value[:copper])}".to_i
15
16
  else
16
- @value = 0
17
+ nil
17
18
  end
19
+ else
20
+ nil
18
21
  end
19
22
  end
20
23
 
@@ -47,5 +47,9 @@ module Wowecon
47
47
  @value == other.to_i
48
48
  end
49
49
 
50
+ def nil?
51
+ @value.nil?
52
+ end
53
+
50
54
  end
51
55
  end
@@ -1,48 +1,54 @@
1
1
  require_relative "../lib/wowecon/currency.rb"
2
2
 
3
3
  describe Wowecon::Currency do
4
-
5
- before :each do
6
- @currency = Wowecon::Currency.new({:gold => 1420, :silver => 9, :copper => 10})
7
- end
8
-
4
+
9
5
  it "can be initialised using a floating point value" do
10
- currency = Wowecon::Currency.new(1420.091)
11
- currency.class.should == Wowecon::Currency
6
+ @currency = Wowecon::Currency.new(1420.091)
7
+ @currency.class.should == Wowecon::Currency
12
8
  end
13
9
 
14
10
  it "can be initialised using a hash" do
15
- currency = Wowecon::Currency.new({:gold => 1420, :silver => 9, :copper => 10})
16
- currency.class.should == Wowecon::Currency
11
+ @currency = Wowecon::Currency.new({:gold => 1420, :silver => 9, :copper => 10})
12
+ @currency.class.should == Wowecon::Currency
17
13
  end
18
14
 
19
15
  it "can be initialised using an integer" do
20
- currency = Wowecon::Currency.new(1420091)
21
- currency.class.should == Wowecon::Currency
16
+ @currency = Wowecon::Currency.new(1420091)
17
+ @currency.class.should == Wowecon::Currency
22
18
  end
23
19
 
24
20
  it "responds to integer" do
21
+ @currency = Wowecon::Currency.new({:gold => 1420, :silver => 9, :copper => 10})
25
22
  (@currency.should respond_to :to_i) && @currency.to_i.should == 14200910
26
23
  end
27
24
 
28
25
  it "responds to hash" do
26
+ @currency = Wowecon::Currency.new({:gold => 1420, :silver => 9, :copper => 10})
29
27
  (@currency.should respond_to :to_hash) && @currency.to_hash.should == {:gold => 1420, :silver => 9, :copper => 10}
30
28
  end
31
29
 
32
30
  it "responds to float" do
31
+ @currency = Wowecon::Currency.new({:gold => 1420, :silver => 9, :copper => 10})
33
32
  (@currency.should respond_to :to_f) && @currency.to_f.should == 1420.091
34
33
  end
35
34
 
36
- it "returns zero if initialised with an invalid hash" do
37
- zero = Wowecon::Currency.new({:error => "price not found"})
38
- zero.should == 0
35
+ it "returns nil if initialised with an invalid hash" do
36
+ @zero = Wowecon::Currency.new({:error => "price not found"})
37
+ @zero.should be_nil
38
+ end
39
+
40
+ it "returns nil if initialised with nil" do
41
+ @nilcurrency = Wowecon::Currency.new(nil)
42
+ @nilcurrency.should be_nil
39
43
  end
40
44
 
41
45
  it "can create a string representation of the currency value" do
46
+ @currency = Wowecon::Currency.new({:gold => 1420, :silver => 9, :copper => 10})
42
47
  @currency.to_s.should == "1,420g 9s 10c"
43
48
  end
44
49
 
45
50
  it "can create a HTML tag representation of the currency value" do
51
+ @currency = Wowecon::Currency.new({:gold => 1420, :silver => 9, :copper => 10})
46
52
  @currency.to_html.should == '<var class="gold">1,420</var><var class="silver">9</var><var class="copper">10</var>'
47
53
  end
48
54
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: wowecon
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.2.1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ben Darlow
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-05 00:00:00 +01:00
13
+ date: 2011-07-06 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency