bitcoin-money 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 08edeaf50fc52e5e28e8527f576ad423068c0984
4
- data.tar.gz: ce6c85461f45a2d973f55c889f413009c327d346
3
+ metadata.gz: c0d20b83dd14d7d669af9743fd274b681d78926b
4
+ data.tar.gz: aa2ef366382416fd818386bc799f26c51cc15629
5
5
  SHA512:
6
- metadata.gz: 1d934297abb3aeb256c0edf9b2ca328576166c0822022382ba9d934097bce6169b05723a5c7079e16f2ca7461030f79cc72a22c96fbe95ed67aab2b87236ffcd
7
- data.tar.gz: fcdcdacc578c5732d99ffa1101911a4642ce9fc9a38688e35235c7c8b64643ecd62cc5f13bc1126d627f066e2a218e910a6e8b5127cc5768550f8ae2ea811d8a
6
+ metadata.gz: 6289ad3afa3f266bc4e5e971acedae165b752dbc23d2132a01f60b9ff1f9888e40af23a6b4fa550cc975d7deec9056a926c67b2d9795dce3ff7b5e61c9e2fdae
7
+ data.tar.gz: f03cf0cc62259bcac9c14ea09a9377510585069b6505c34a2b93f4ea9570f30acbda0e83bc71391e99b71aff4afa81bdec2ea0bee6cadd9f546397c11d30e420
@@ -4,11 +4,11 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "bitcoin-money"
7
- spec.version = "0.5.1"
7
+ spec.version = "0.6.0"
8
8
  spec.authors = ["James Larisch"]
9
9
  spec.email = ["root@jameslarisch.com"]
10
- spec.summary = "Represent BTC like Money, with nice precision."
11
- spec.description = "Represent BTC like Money, with nice precision."
10
+ spec.summary = "Intelligently manipulate Bitcoin with a Ruby object."
11
+ spec.description = "Supports conversions and arithmetic."
12
12
  spec.homepage = "https://github.com/zenchilabs/bitcoin-money"
13
13
  spec.license = "MIT"
14
14
 
data/lib/btc.rb CHANGED
@@ -1,65 +1,65 @@
1
1
  require 'bigdecimal'
2
2
 
3
- class Btc
3
+ class BTC
4
4
  BTC = BigDecimal.new('1.0')
5
5
  MBTC = BigDecimal.new('0.001')
6
- UBTC = BigDecimal.new('0.000001')
6
+ BIT = BigDecimal.new('0.000001')
7
7
  SATOSHI = BigDecimal.new('0.00000001')
8
8
 
9
9
  MIN_BTC = SATOSHI.to_f
10
10
  MIN_MBTC = 0.00001
11
- MIN_UBTC = 0.01
11
+ MIN_BIT = 0.01
12
12
  MIN_SATOSHI = 1
13
13
 
14
14
  def initialize(amt)
15
- raise ArgumentError, "BTC amount must not be less than #{MIN_BTC}" unless Btc.valid_btc(amt)
15
+ raise ArgumentError, "BTC amount must not be less than #{MIN_BTC}" unless self.class.valid_btc?(amt)
16
16
  @amt = BigDecimal.new(amt.to_f.to_s)
17
17
  end
18
18
 
19
19
  def self.from_mbtc(amt)
20
- raise ArgumentError, "mBTC amount must not be less than #{MIN_MBTC}" unless Btc.valid_mbtc(amt)
21
- Btc.new(BigDecimal.new(amt.to_f.to_s) * MBTC)
20
+ raise ArgumentError, "mBTC amount must not be less than #{MIN_MBTC}" unless self.valid_mbtc?(amt)
21
+ self.new(BigDecimal.new(amt.to_f.to_s) * MBTC)
22
22
  end
23
23
 
24
- def self.from_ubtc(amt)
25
- raise ArgumentError, "uBTC amount must not be less than #{MIN_UBTC}" unless Btc.valid_ubtc(amt)
26
- Btc.new(BigDecimal.new(amt.to_f.to_s) * UBTC)
24
+ def self.from_bits(amt)
25
+ raise ArgumentError, "bits amount must not be less than #{MIN_BIT}" unless self.valid_bits?(amt)
26
+ self.new(BigDecimal.new(amt.to_f.to_s) * BIT)
27
27
  end
28
28
 
29
29
  def self.from_satoshis(amt)
30
- raise ArgumentError, 'Satoshi amount must be > 1 or 0' unless Btc.valid_satoshis(amt)
31
- Btc.new(BigDecimal.new(amt.to_i.to_f.to_s) * SATOSHI)
30
+ raise ArgumentError, 'Satoshi amount must be > 1 or 0' unless self.valid_satoshis?(amt)
31
+ self.new(BigDecimal.new(amt.to_i.to_f.to_s) * SATOSHI)
32
32
  end
33
33
 
34
- def self.valid_btc(amt)
34
+ def self.valid_btc?(amt)
35
35
  amt.to_f >= SATOSHI.to_f || amt.to_f == 0.0
36
36
  end
37
37
 
38
- def self.valid_mbtc(amt)
38
+ def self.valid_mbtc?(amt)
39
39
  amt.to_f >= MIN_MBTC || amt.to_f == 0.0
40
40
  end
41
41
 
42
- def self.valid_ubtc(amt)
43
- amt.to_f >= MIN_UBTC || amt.to_f == 0.0
42
+ def self.valid_bits?(amt)
43
+ amt.to_f >= MIN_BIT || amt.to_f == 0.0
44
44
  end
45
45
 
46
- def self.valid_satoshis(amt)
46
+ def self.valid_satoshis?(amt)
47
47
  amt.to_i >= 0
48
48
  end
49
49
 
50
- def btc
50
+ def to_btc
51
51
  @amt
52
52
  end
53
53
 
54
- def mbtc
54
+ def to_mbtc
55
55
  @amt / MBTC
56
56
  end
57
57
 
58
- def ubtc
59
- @amt / UBTC
58
+ def to_bits
59
+ @amt / BIT
60
60
  end
61
61
 
62
- def satoshis
62
+ def to_satoshis
63
63
  @amt / SATOSHI
64
64
  end
65
65
 
@@ -72,30 +72,30 @@ class Btc
72
72
  end
73
73
 
74
74
  def +(other)
75
- Btc.new(@amt + other.btc)
75
+ self.class.new(@amt + other.to_btc)
76
76
  end
77
77
 
78
78
  def /(other)
79
- Btc.new(@amt / other.btc)
79
+ self.class.new(@amt / other.to_btc)
80
80
  end
81
81
 
82
82
  def -(other)
83
- Btc.new(@amt - other.btc)
83
+ self.class.new(@amt - other.to_btc)
84
84
  end
85
85
 
86
86
  def *(other)
87
- Btc.new(@amt * other.btc)
87
+ self.class.new(@amt * other.to_btc)
88
88
  end
89
89
 
90
90
  def **(exp)
91
- Btc.new(@amt ** exp)
91
+ self.class.new(@amt ** exp)
92
92
  end
93
93
 
94
94
  def ==(other)
95
- @amt == other.btc
95
+ @amt == other.to_btc
96
96
  end
97
97
 
98
98
  def inspect
99
- "#<Btc BTC:#{btc.to_f} mBTC:#{mbtc.to_f} uBTC:#{ubtc.to_f} Satoshis:#{satoshis.to_f}>"
99
+ "#<BTC:#{to_btc.to_f} mBTC:#{to_mbtc.to_f} bits:#{to_bits.to_f} Satoshis:#{to_satoshis.to_f}>"
100
100
  end
101
101
  end
@@ -1,83 +1,83 @@
1
1
  require 'btc'
2
2
 
3
- describe Btc do
3
+ describe BTC do
4
4
 
5
5
  describe '#new' do
6
6
  it 'should initialize okay' do
7
- b = Btc.new(12345)
8
- expect(b.btc).to eq(BigDecimal.new('12345.0'))
7
+ b = BTC.new(12345)
8
+ expect(b.to_btc).to eq(BigDecimal.new('12345.0'))
9
9
  end
10
10
 
11
11
  it 'should throw error if invalid btc amount' do
12
- expect{ Btc.new(0.00000000000001) }.to raise_error(ArgumentError)
12
+ expect{ BTC.new(0.00000000000001) }.to raise_error(ArgumentError)
13
13
  end
14
14
  end
15
15
 
16
16
  describe '.from_mbtc' do
17
17
  it 'should throw error if invalid mbtc amount' do
18
- expect{ Btc.from_mbtc(Btc::SATOSHI) }.to raise_error(ArgumentError)
18
+ expect{ BTC.from_mbtc(BTC::SATOSHI) }.to raise_error(ArgumentError)
19
19
  end
20
20
  end
21
21
 
22
- describe '.from_ubtc' do
23
- it 'should throw error if invalid ubtc amount' do
24
- expect{ Btc.from_ubtc(Btc::MIN_UBTC - 10) }.to raise_error(ArgumentError)
22
+ describe '.from_bits' do
23
+ it 'should throw error if invalid bit amount' do
24
+ expect{ BTC.from_bits(BTC::MIN_BIT - 10) }.to raise_error(ArgumentError)
25
25
  end
26
26
  end
27
27
 
28
28
  describe '.from_satoshis' do
29
29
  it 'should throw error if invalid satoshi amount' do
30
- expect{ Btc.from_satoshis(-1) }.to raise_error(ArgumentError)
30
+ expect{ BTC.from_satoshis(-1) }.to raise_error(ArgumentError)
31
31
  end
32
32
  end
33
33
 
34
34
  it 'should convert btc to mbtc and back' do
35
- expect(Btc.new(Btc::MBTC.to_f).mbtc).to eq(BigDecimal.new('1'))
36
- expect(Btc.from_mbtc(1).btc).to eq(BigDecimal.new(Btc::MBTC))
35
+ expect(BTC.new(BTC::MBTC.to_f).to_mbtc).to eq(BigDecimal.new('1'))
36
+ expect(BTC.from_mbtc(1).to_btc).to eq(BigDecimal.new(BTC::MBTC))
37
37
  end
38
38
 
39
39
  it 'should convert btc to satoshis and back' do
40
- expect(Btc.new(Btc::SATOSHI).satoshis).to eq(BigDecimal.new('1'))
41
- expect(Btc.from_satoshis(1).btc).to eq(BigDecimal.new(Btc::SATOSHI))
40
+ expect(BTC.new(BTC::SATOSHI).to_satoshis).to eq(BigDecimal.new('1'))
41
+ expect(BTC.from_satoshis(1).to_btc).to eq(BigDecimal.new(BTC::SATOSHI))
42
42
  end
43
43
 
44
44
  it 'should convert mbtc to satoshis and back' do
45
- expect(Btc.from_mbtc(1).satoshis).to eq(BigDecimal.new('100000'))
46
- expect(Btc.from_satoshis(10000).mbtc).to eq(BigDecimal.new('0.1'))
45
+ expect(BTC.from_mbtc(1).to_satoshis).to eq(BigDecimal.new('100000'))
46
+ expect(BTC.from_satoshis(10000).to_mbtc).to eq(BigDecimal.new('0.1'))
47
47
  end
48
48
 
49
- it 'should convert btc to ubtc and back' do
50
- expect(Btc.new(1).ubtc).to eq(BigDecimal.new('1000000'))
51
- expect(Btc.from_ubtc(12).btc).to eq(BigDecimal('.000012'))
49
+ it 'should convert btc to bit and back' do
50
+ expect(BTC.new(1).to_bits).to eq(BigDecimal.new('1000000'))
51
+ expect(BTC.from_bits(12).to_btc).to eq(BigDecimal('.000012'))
52
52
  end
53
53
 
54
54
  it 'should use equality correctly' do
55
- expect(Btc.new(0.123)).to eq(Btc.from_mbtc(123))
56
- expect(Btc.new(0.123)).to eq(Btc.from_satoshis(12300000))
57
- expect(Btc.from_mbtc(12)).to eq(Btc.from_satoshis(1200000))
55
+ expect(BTC.new(0.123)).to eq(BTC.from_mbtc(123))
56
+ expect(BTC.new(0.123)).to eq(BTC.from_satoshis(12300000))
57
+ expect(BTC.from_mbtc(12)).to eq(BTC.from_satoshis(1200000))
58
58
  end
59
59
 
60
60
  it 'should use multiplication correctly' do
61
- expect(Btc.new(0.1234) * Btc.new(0.1234)).to eq(Btc.new(BigDecimal.new('0.1234') * BigDecimal.new('0.1234')))
61
+ expect(BTC.new(0.1234) * BTC.new(0.1234)).to eq(BTC.new(BigDecimal.new('0.1234') * BigDecimal.new('0.1234')))
62
62
  end
63
63
 
64
64
  it 'should use division correctly' do
65
- expect(Btc.new(0.1234) / Btc.new(0.1234)).to eq(Btc.new(BigDecimal.new('0.1234') / BigDecimal.new('0.1234')))
65
+ expect(BTC.new(0.1234) / BTC.new(0.1234)).to eq(BTC.new(BigDecimal.new('0.1234') / BigDecimal.new('0.1234')))
66
66
  end
67
67
 
68
68
  it 'should use addition correctly' do
69
- expect(Btc.new(0.1234) + Btc.new(0.1234)).to eq(Btc.new(BigDecimal.new('0.1234') + BigDecimal.new('0.1234')))
69
+ expect(BTC.new(0.1234) + BTC.new(0.1234)).to eq(BTC.new(BigDecimal.new('0.1234') + BigDecimal.new('0.1234')))
70
70
  end
71
71
 
72
72
  it 'should use subtraction correctly' do
73
- expect(Btc.new(0.1234) - Btc.new(0.1234)).to eq(Btc.new(BigDecimal.new('0.1234') - BigDecimal.new('0.1234')))
73
+ expect(BTC.new(0.1234) - BTC.new(0.1234)).to eq(BTC.new(BigDecimal.new('0.1234') - BigDecimal.new('0.1234')))
74
74
  end
75
75
 
76
76
  it 'should use to_f correctly' do
77
- expect(Btc.new(1.5).to_f).to eq(1.5)
77
+ expect(BTC.new(1.5).to_f).to eq(1.5)
78
78
  end
79
79
 
80
80
  it 'should use to_i correctly' do
81
- expect(Btc.new(1.4).to_i).to eq(1)
81
+ expect(BTC.new(1.4).to_i).to eq(1)
82
82
  end
83
83
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitcoin-money
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Larisch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-20 00:00:00.000000000 Z
11
+ date: 2014-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 3.0.0
41
- description: Represent BTC like Money, with nice precision.
41
+ description: Supports conversions and arithmetic.
42
42
  email:
43
43
  - root@jameslarisch.com
44
44
  executables: []
@@ -76,6 +76,7 @@ rubyforge_project:
76
76
  rubygems_version: 2.2.2
77
77
  signing_key:
78
78
  specification_version: 4
79
- summary: Represent BTC like Money, with nice precision.
79
+ summary: Intelligently manipulate Bitcoin with a Ruby object.
80
80
  test_files:
81
81
  - spec/btc_spec.rb
82
+ has_rdoc: