satoshis 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/coin_decimal.rb +88 -0
  3. data/lib/satoshis.rb +4 -74
  4. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 20a41eb38b1f428e876fe02cc28348e98cf11a9aed14c7612ca532a6bbe7bb14
4
- data.tar.gz: 1fb070622ba09170b8d976e66091e297ae49d279f03734f520747ad9744a49c9
3
+ metadata.gz: 84e1fc81b5429f41a576b2a4de261716346f6f1ff159f6f183fe6ea2270321c9
4
+ data.tar.gz: ba36753dfef71535e029188b1063535c8236afc09848bbacb0254f872478d213
5
5
  SHA512:
6
- metadata.gz: 7f0a4d1fc66fc0dbc83e211dc34031305328468e59c73916f64a1c74dea5ed1acd03a322a5e9211bebd4c3a7b67ed218870d664b495e7da5ae3506a6fb584a06
7
- data.tar.gz: 1076d53d10a2f238f310ed807a3751440649c6ddfce73ef1a684ec92da04bcb9d55ba567c02efac6c46aba7ffb2a2cf000b45afc6ee8c7a0fbc036139b75b4a4
6
+ metadata.gz: ec15c4103fa0d1dbac59c7a3decba1d61e373d4f31e5d2cb3e16d6d8e2992173ba1d914af8efca1f2ae67930c2b8f5e139845800188c1e7b35c3578eb09f86a2
7
+ data.tar.gz: 5277dd18aecf988c3a29396a8f3f6e2395bffe6945a4cebc6be05c5167d0900311c969a3d9ba27219a35a2d566c7d7324ba47c277d0d179ad43525addd61dabd
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CoinDecimal
4
+ extend Forwardable
5
+
6
+ BITCOIN_PRECISION = 8
7
+ MAX_PRECISION = 32
8
+
9
+ attr_reader :value, :precision
10
+
11
+ def_delegators :value, :to_i
12
+
13
+ def initialize(value, precision = BITCOIN_PRECISION)
14
+ raise(ArgumentError, "Precision must be an Integer.") unless precision.is_a?(Integer)
15
+
16
+ raise(ArgumentError, "Precision must be less than or equal to #{MAX_PRECISION}.") if precision > MAX_PRECISION
17
+
18
+ raise(ArgumentError, "Precision can't be negative.") if precision < 0
19
+
20
+ @precision = precision
21
+
22
+ raise(ArgumentError, "Value can't be nil.") if value.nil?
23
+
24
+ @value = CoinDecimal.ensure_integer(value, precision)
25
+ end
26
+
27
+ def self.ensure_integer(value, precision = BITCOIN_PRECISION)
28
+ if value.is_a?(Integer)
29
+ value
30
+ elsif value.is_a?(BigDecimal)
31
+ (value * (10 ** precision)).to_i
32
+ elsif value.is_a?(String)
33
+ (value.to_d * (10 ** precision)).to_i
34
+ else
35
+ raise(ArgumentError, "Value must be Integer, BigDecimal or String.")
36
+ end
37
+ end
38
+
39
+ def +(addend)
40
+ sum = value + addend.value
41
+ CoinDecimal.new(sum, precision)
42
+ end
43
+
44
+ def -(subtrahend)
45
+ difference = value - subtrahend.value
46
+ CoinDecimal.new(difference, precision)
47
+ end
48
+
49
+ def formatted(short: true)
50
+ result = string
51
+
52
+ if short
53
+ result = result.gsub(/0+$/, "")
54
+ result = "#{result}0" if result.end_with?(".")
55
+ end
56
+
57
+ result
58
+ end
59
+
60
+ alias to_s formatted
61
+
62
+ def to_d
63
+ formatted.to_d
64
+ end
65
+
66
+ def positive?
67
+ value > 0
68
+ end
69
+
70
+ def negative?
71
+ value < 0
72
+ end
73
+
74
+ private
75
+
76
+ def string
77
+ result = value.abs.to_s
78
+
79
+ result = \
80
+ if result.length > precision
81
+ result.insert(-(precision + 1), ".")
82
+ else
83
+ "0.#{result.rjust(precision, "0")}"
84
+ end
85
+
86
+ negative? ? "-#{result}" : result
87
+ end
88
+ end
@@ -3,84 +3,14 @@
3
3
  require "bigdecimal"
4
4
  require "bigdecimal/util"
5
5
  require "forwardable"
6
+ require "coin_decimal"
6
7
 
7
- class Satoshis
8
- extend Forwardable
9
-
10
- VERSION = "0.2.0"
11
-
8
+ class Satoshis < CoinDecimal
9
+ VERSION = "0.3.0"
12
10
  PRECISION = 8
13
11
  ONE = 100_000_000
14
12
 
15
- attr_reader :value
16
-
17
- def_delegators :value, :to_i
18
-
19
13
  def initialize(value)
20
- raise(ArgumentError, "Value can't be nil.") if value.nil?
21
-
22
- @value = Satoshis.ensure_integer(value)
23
- end
24
-
25
- def self.ensure_integer(value)
26
- if value.is_a?(Integer)
27
- value
28
- elsif value.is_a?(BigDecimal)
29
- (value * ONE).to_i
30
- elsif value.is_a?(String)
31
- (value.to_d * ONE).to_i
32
- else
33
- raise(ArgumentError, "Value must be Integer, BigDecimal or String.")
34
- end
35
- end
36
-
37
- def +(addend)
38
- sum = value + addend.value
39
- Satoshis.new(sum)
40
- end
41
-
42
- def -(subtrahend)
43
- difference = value - subtrahend.value
44
- Satoshis.new(difference)
45
- end
46
-
47
- def formatted(short: true)
48
- result = string
49
-
50
- if short
51
- result = result.gsub(/0+$/, "")
52
- result = "#{result}0" if result.end_with?(".")
53
- end
54
-
55
- result
56
- end
57
-
58
- alias to_s formatted
59
-
60
- def to_d
61
- formatted.to_d
62
- end
63
-
64
- def positive?
65
- value > 0
66
- end
67
-
68
- def negative?
69
- value < 0
70
- end
71
-
72
- private
73
-
74
- def string
75
- result = value.abs.to_s
76
-
77
- result = \
78
- if result.length > PRECISION
79
- result.insert(-(PRECISION + 1), ".")
80
- else
81
- "0.#{result.rjust(PRECISION, "0")}"
82
- end
83
-
84
- negative? ? "-#{result}" : result
14
+ super(value, PRECISION)
85
15
  end
86
16
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: satoshis
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Soares
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-22 00:00:00.000000000 Z
11
+ date: 2018-10-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -115,6 +115,7 @@ executables: []
115
115
  extensions: []
116
116
  extra_rdoc_files: []
117
117
  files:
118
+ - lib/coin_decimal.rb
118
119
  - lib/satoshis.rb
119
120
  homepage: https://github.com/bsoares/satoshis
120
121
  licenses: