crypto-unit 0.3.0 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/crypto-unit.gemspec +4 -4
- data/lib/crypto_unit_base.rb +13 -13
- data/spec/crypto_unit_base_spec.rb +38 -38
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 44be33d0f9a4b7bb535419719c790b0780327cde
|
4
|
+
data.tar.gz: bcb256c4393e58355f19f383567c8fdfdff66df5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9b7b58b89f860f5ba0ed54935151eddb3ee086909ea4acec2c29d1681cb761dd8a08237d71a3ceb8eb27852767ae2d18f8e391bff2b1c173caf41ebd42ae3f2e
|
7
|
+
data.tar.gz: 43864b3a960cc88d8295f820b81da34df0bdd0ccb73992046f42509c4d79862d9d234f5c8a2d7169ad021ae2ffd6b069b372cf164fe4b98ba4a4295013d8f610
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.2
|
data/crypto-unit.gemspec
CHANGED
@@ -2,16 +2,16 @@
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
|
-
# stub: crypto-unit 0.3.
|
5
|
+
# stub: crypto-unit 0.3.2 ruby lib
|
6
6
|
|
7
7
|
Gem::Specification.new do |s|
|
8
8
|
s.name = "crypto-unit".freeze
|
9
|
-
s.version = "0.3.
|
9
|
+
s.version = "0.3.2"
|
10
10
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
12
12
|
s.require_paths = ["lib".freeze]
|
13
13
|
s.authors = ["Roman Snitko".freeze]
|
14
|
-
s.date = "2017-
|
14
|
+
s.date = "2017-08-31"
|
15
15
|
s.description = "Converts various BTC and LTC denominations".freeze
|
16
16
|
s.email = "roman.snitko@gmail.com".freeze
|
17
17
|
s.extra_rdoc_files = [
|
@@ -36,7 +36,7 @@ Gem::Specification.new do |s|
|
|
36
36
|
]
|
37
37
|
s.homepage = "http://github.com/mgpnd/crypto-unit".freeze
|
38
38
|
s.licenses = ["MIT".freeze]
|
39
|
-
s.rubygems_version = "2.6.
|
39
|
+
s.rubygems_version = "2.6.13".freeze
|
40
40
|
s.summary = "Converts various BTC and LTC denominations".freeze
|
41
41
|
|
42
42
|
if s.respond_to? :specification_version then
|
data/lib/crypto_unit_base.rb
CHANGED
@@ -26,23 +26,23 @@ class CryptoUnit
|
|
26
26
|
@value = convert_to_primary(n) if n
|
27
27
|
end
|
28
28
|
|
29
|
-
def to_standart(as: :number)
|
30
|
-
to_denomination(UNIT_DENOMINATIONS[:standart], as: as)
|
29
|
+
def to_standart(as: :number, format_zero: false)
|
30
|
+
to_denomination(UNIT_DENOMINATIONS[:standart], as: as, format_zero: format_zero)
|
31
31
|
end
|
32
32
|
|
33
|
-
def to_milli(as: :number)
|
34
|
-
to_denomination(UNIT_DENOMINATIONS[:milli], as: as)
|
33
|
+
def to_milli(as: :number, format_zero: false)
|
34
|
+
to_denomination(UNIT_DENOMINATIONS[:milli], as: as, format_zero: format_zero)
|
35
35
|
end
|
36
36
|
|
37
|
-
def to_unit(as: :number)
|
38
|
-
to_denomination(UNIT_DENOMINATIONS[@to_unit], as: as)
|
37
|
+
def to_unit(as: :number, format_zero: false)
|
38
|
+
to_denomination(UNIT_DENOMINATIONS[@to_unit], as: as, format_zero: format_zero)
|
39
39
|
end
|
40
40
|
|
41
41
|
def to_i
|
42
42
|
return 0 if @value.nil?
|
43
43
|
@value
|
44
44
|
end
|
45
|
-
alias :primary_value :to_i
|
45
|
+
alias :primary_value :to_i
|
46
46
|
|
47
47
|
def to_s
|
48
48
|
to_unit.to_s
|
@@ -112,8 +112,8 @@ class CryptoUnit
|
|
112
112
|
end
|
113
113
|
|
114
114
|
protected
|
115
|
-
|
116
|
-
def to_denomination(digits_after_delimiter, as: :number)
|
115
|
+
|
116
|
+
def to_denomination(digits_after_delimiter, as: :number, format_zero: false)
|
117
117
|
sign = @value < 0 ? -1 : 1
|
118
118
|
val = @value.abs
|
119
119
|
return val if digits_after_delimiter <= 0
|
@@ -124,10 +124,10 @@ class CryptoUnit
|
|
124
124
|
result.reverse!
|
125
125
|
result = result.sub(/\A0*/, '').sub(/0*\Z/, '') # remove zeros on both sides
|
126
126
|
if as == :number
|
127
|
-
result.to_f*sign
|
127
|
+
result.to_f*sign
|
128
128
|
else
|
129
129
|
if result == '.'
|
130
|
-
result = '0.0'
|
130
|
+
result = format_zero ? '0' : '0.0'
|
131
131
|
elsif result =~ /\A\./
|
132
132
|
result = "0#{result}"
|
133
133
|
end
|
@@ -147,7 +147,7 @@ class CryptoUnit
|
|
147
147
|
|
148
148
|
if decimal_part_length > UNIT_DENOMINATIONS[@from_unit]
|
149
149
|
raise TooManyDigitsAfterDecimalPoint,
|
150
|
-
"Too many digits (#{decimal_part_length}) after decimal point used for #{@from_unit} value, while #{UNIT_DENOMINATIONS[@from_unit]} allowed"
|
150
|
+
"Too many digits (#{decimal_part_length}) after decimal point used for #{@from_unit} value, while #{UNIT_DENOMINATIONS[@from_unit]} allowed"
|
151
151
|
end
|
152
152
|
|
153
153
|
n = ("%.#{UNIT_DENOMINATIONS[@from_unit]}f" % n) # otherwise we might see a scientific notation
|
@@ -161,4 +161,4 @@ class CryptoUnit
|
|
161
161
|
|
162
162
|
n.join.to_i
|
163
163
|
end
|
164
|
-
end
|
164
|
+
end
|
@@ -5,92 +5,92 @@ require_relative '../lib/litoshi'
|
|
5
5
|
describe CryptoUnit do
|
6
6
|
|
7
7
|
it "creates a Bignum representing value in primary units" do
|
8
|
-
expect(
|
8
|
+
expect(SatoshiUnit.new(1.00).to_i).to eq(100000000)
|
9
9
|
end
|
10
10
|
|
11
11
|
it "takes care of the sign before the value" do
|
12
|
-
expect(
|
12
|
+
expect(SatoshiUnit.new(-1.00).to_i).to eq(-100000000)
|
13
13
|
end
|
14
14
|
|
15
15
|
it "converts primary unit back to some more common denomination" do
|
16
|
-
expect(
|
17
|
-
expect(
|
18
|
-
expect(
|
19
|
-
expect(
|
20
|
-
expect(
|
21
|
-
expect(
|
16
|
+
expect(SatoshiUnit.new(1.00).to_standart).to eq(1)
|
17
|
+
expect(SatoshiUnit.new(1.08763).to_standart).to eq(1.08763)
|
18
|
+
expect(SatoshiUnit.new(1.08763).to_milli).to eq(1087.63)
|
19
|
+
expect(SatoshiUnit.new(-1.08763).to_milli).to eq(-1087.63)
|
20
|
+
expect(SatoshiUnit.new(0.00000001).to_i).to eq(1)
|
21
|
+
expect(SatoshiUnit.new(0.00000001).to_milli).to eq(0.00001)
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
it "converts from various source denominations" do
|
25
|
-
expect(
|
26
|
-
expect(
|
27
|
-
expect(
|
28
|
-
satoshi =
|
25
|
+
expect(SatoshiUnit.new(1, unit: 'milli').to_standart).to eq(0.001)
|
26
|
+
expect(SatoshiUnit.new(1, unit: 'milli').to_unit).to eq(1)
|
27
|
+
expect(SatoshiUnit.new(10000000, unit: 'milli').to_unit).to eq(10000000)
|
28
|
+
satoshi = SatoshiUnit.new(10000000, unit: 'milli')
|
29
29
|
satoshi.primary_value = 1
|
30
30
|
expect(satoshi.to_unit).to eq(0.00001)
|
31
|
-
expect(
|
31
|
+
expect(SatoshiUnit.new(100, unit: 'milli').to_i).to eq(10000000)
|
32
32
|
end
|
33
33
|
|
34
34
|
it "treats nil in value as 0" do
|
35
|
-
expect(
|
36
|
-
expect(
|
37
|
-
expect(
|
35
|
+
expect(SatoshiUnit.new < 1).to be_truthy
|
36
|
+
expect(SatoshiUnit.new > 1).to be_falsy
|
37
|
+
expect(SatoshiUnit.new == 0).to be_truthy
|
38
38
|
end
|
39
39
|
|
40
40
|
it "converts negative values correctly" do
|
41
|
-
expect(
|
41
|
+
expect(SatoshiUnit.new(-1.00, unit: :milli).to_standart).to eq(-0.001)
|
42
42
|
end
|
43
43
|
|
44
44
|
it "converts zero values correctly" do
|
45
|
-
expect(
|
45
|
+
expect(SatoshiUnit.new(0, unit: :milli).to_unit).to eq(0)
|
46
46
|
end
|
47
47
|
|
48
48
|
it "converts nil values correctly" do
|
49
|
-
s =
|
49
|
+
s = SatoshiUnit.new(nil, unit: :milli)
|
50
50
|
expect(s.value).to eq(0)
|
51
51
|
s.value = nil
|
52
52
|
expect(s.to_unit).to eq(0)
|
53
53
|
end
|
54
54
|
|
55
55
|
it "displays one primary unit in human form, not math form" do
|
56
|
-
one_satoshi =
|
56
|
+
one_satoshi = SatoshiUnit.new(1, from_unit: :primary, to_unit: :standart)
|
57
57
|
expect(one_satoshi.to_unit(as: :string)).not_to eq('1.0e-08')
|
58
58
|
expect(one_satoshi.to_unit(as: :string)).to eq('0.00000001')
|
59
59
|
end
|
60
60
|
|
61
61
|
it "displays zero units in human form, not math form" do
|
62
|
-
zero_satoshi =
|
62
|
+
zero_satoshi = SatoshiUnit.new(0, from_unit: :primary, to_unit: :standart)
|
63
63
|
expect(zero_satoshi.to_unit(as: :string)).to eq('0.0')
|
64
64
|
end
|
65
65
|
|
66
66
|
it "raises exception if decimal part contains more digits than allowed by from_value" do
|
67
|
-
expect( -> {
|
68
|
-
expect( -> {
|
69
|
-
expect( -> {
|
70
|
-
expect( -> {
|
71
|
-
expect( -> {
|
72
|
-
expect( -> {
|
73
|
-
expect( -> {
|
67
|
+
expect( -> { SatoshiUnit.new(0.001000888888888888888, from_unit: :standart).to_unit }).to raise_exception(CryptoUnit::TooManyDigitsAfterDecimalPoint)
|
68
|
+
expect( -> { SatoshiUnit.new("0.001000999999999999999", from_unit: :standart).to_unit }).to raise_exception(CryptoUnit::TooManyDigitsAfterDecimalPoint)
|
69
|
+
expect( -> { SatoshiUnit.new(0.001000999, from_unit: :standart).to_unit }).to raise_exception(CryptoUnit::TooManyDigitsAfterDecimalPoint)
|
70
|
+
expect( -> { SatoshiUnit.new(0.00100099, from_unit: :standart).to_unit }).not_to raise_exception
|
71
|
+
expect( -> { SatoshiUnit.new(0.123456789, from_unit: :standart) }).to raise_exception(CryptoUnit::TooManyDigitsAfterDecimalPoint)
|
72
|
+
expect( -> { SatoshiUnit.new(0.12345678, from_unit: :standart).to_unit }).not_to raise_exception
|
73
|
+
expect( -> { SatoshiUnit.new(nil, from_unit: :standart).to_unit }).not_to raise_exception
|
74
74
|
end
|
75
75
|
|
76
76
|
it "Satoshi dissallows to create values more than 21mil BTC" do
|
77
|
-
expect( -> {
|
78
|
-
expect( -> {
|
77
|
+
expect( -> { SatoshiUnit.new(21_000_001) }).to raise_exception(CryptoUnit::TooLarge)
|
78
|
+
expect( -> { SatoshiUnit.new(21_000_000) }).not_to raise_exception
|
79
79
|
end
|
80
80
|
|
81
81
|
it "Litoshi disallows to create values more than 84mil LTC" do
|
82
|
-
expect( -> {
|
83
|
-
expect( -> {
|
82
|
+
expect( -> { LitoshiUnit.new(84_000_001) }).to raise_exception(CryptoUnit::TooLarge)
|
83
|
+
expect( -> { LitoshiUnit.new(84_000_000) }).not_to raise_exception
|
84
84
|
end
|
85
85
|
|
86
86
|
it "returns CryptoUnit for +,- and * methods if both operands are CryptoUnit" do
|
87
|
-
s1 =
|
88
|
-
s2 =
|
89
|
-
expect(s1+s2).to be_kind_of(
|
87
|
+
s1 = SatoshiUnit.new(0.001, from_unit: :standart)
|
88
|
+
s2 = SatoshiUnit.new(0.002, from_unit: :standart)
|
89
|
+
expect(s1+s2).to be_kind_of(SatoshiUnit)
|
90
90
|
expect((s1+s2).to_unit).to eq(0.003)
|
91
|
-
expect(s2-s1).to be_kind_of(
|
91
|
+
expect(s2-s1).to be_kind_of(SatoshiUnit)
|
92
92
|
expect((s2-s1).to_unit).to eq(0.001)
|
93
|
-
expect(s2*s1).to be_kind_of(
|
93
|
+
expect(s2*s1).to be_kind_of(SatoshiUnit)
|
94
94
|
expect((s2*s1).to_unit).to eq(200)
|
95
95
|
end
|
96
96
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crypto-unit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Roman Snitko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -94,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
94
|
version: '0'
|
95
95
|
requirements: []
|
96
96
|
rubyforge_project:
|
97
|
-
rubygems_version: 2.6.
|
97
|
+
rubygems_version: 2.6.13
|
98
98
|
signing_key:
|
99
99
|
specification_version: 4
|
100
100
|
summary: Converts various BTC and LTC denominations
|