satoshis 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/satoshis.rb +51 -37
- metadata +2 -44
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 20a41eb38b1f428e876fe02cc28348e98cf11a9aed14c7612ca532a6bbe7bb14
|
4
|
+
data.tar.gz: 1fb070622ba09170b8d976e66091e297ae49d279f03734f520747ad9744a49c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f0a4d1fc66fc0dbc83e211dc34031305328468e59c73916f64a1c74dea5ed1acd03a322a5e9211bebd4c3a7b67ed218870d664b495e7da5ae3506a6fb584a06
|
7
|
+
data.tar.gz: 1076d53d10a2f238f310ed807a3751440649c6ddfce73ef1a684ec92da04bcb9d55ba567c02efac6c46aba7ffb2a2cf000b45afc6ee8c7a0fbc036139b75b4a4
|
data/lib/satoshis.rb
CHANGED
@@ -2,71 +2,85 @@
|
|
2
2
|
|
3
3
|
require "bigdecimal"
|
4
4
|
require "bigdecimal/util"
|
5
|
+
require "forwardable"
|
5
6
|
|
6
7
|
class Satoshis
|
7
|
-
|
8
|
+
extend Forwardable
|
9
|
+
|
10
|
+
VERSION = "0.2.0"
|
8
11
|
|
9
12
|
PRECISION = 8
|
10
13
|
ONE = 100_000_000
|
11
14
|
|
12
15
|
attr_reader :value
|
13
16
|
|
14
|
-
|
15
|
-
@value = value
|
16
|
-
end
|
17
|
-
|
18
|
-
class << self
|
19
|
-
def from_integer(value)
|
20
|
-
raise(ArgumentError, "value must be an Integer") unless value.is_a? Integer
|
21
|
-
|
22
|
-
new(value)
|
23
|
-
end
|
24
|
-
|
25
|
-
def from_big_decimal(bitcoins)
|
26
|
-
raise(ArgumentError, "bitcoins must be a BigDecimal") unless bitcoins.is_a? BigDecimal
|
17
|
+
def_delegators :value, :to_i
|
27
18
|
|
28
|
-
|
29
|
-
|
19
|
+
def initialize(value)
|
20
|
+
raise(ArgumentError, "Value can't be nil.") if value.nil?
|
30
21
|
|
31
|
-
|
32
|
-
|
22
|
+
@value = Satoshis.ensure_integer(value)
|
23
|
+
end
|
33
24
|
|
34
|
-
|
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.")
|
35
34
|
end
|
36
35
|
end
|
37
36
|
|
38
|
-
def
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
if result.length > PRECISION
|
43
|
-
result.insert(-(PRECISION + 1), ".")
|
44
|
-
else
|
45
|
-
"0.#{result.rjust(PRECISION, "0")}"
|
46
|
-
end
|
47
|
-
|
48
|
-
result = "-#{result}" if negative?
|
37
|
+
def +(addend)
|
38
|
+
sum = value + addend.value
|
39
|
+
Satoshis.new(sum)
|
40
|
+
end
|
49
41
|
|
50
|
-
|
42
|
+
def -(subtrahend)
|
43
|
+
difference = value - subtrahend.value
|
44
|
+
Satoshis.new(difference)
|
51
45
|
end
|
52
46
|
|
53
|
-
def
|
54
|
-
result = string
|
47
|
+
def formatted(short: true)
|
48
|
+
result = string
|
55
49
|
|
56
|
-
|
50
|
+
if short
|
51
|
+
result = result.gsub(/0+$/, "")
|
52
|
+
result = "#{result}0" if result.end_with?(".")
|
53
|
+
end
|
57
54
|
|
58
55
|
result
|
59
56
|
end
|
60
57
|
|
61
|
-
alias to_s
|
58
|
+
alias to_s formatted
|
62
59
|
|
63
60
|
def to_d
|
64
|
-
|
61
|
+
formatted.to_d
|
65
62
|
end
|
66
63
|
|
67
|
-
|
64
|
+
def positive?
|
65
|
+
value > 0
|
66
|
+
end
|
68
67
|
|
69
68
|
def negative?
|
70
69
|
value < 0
|
71
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
|
85
|
+
end
|
72
86
|
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.
|
4
|
+
version: 0.2.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-
|
11
|
+
date: 2018-04-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -24,20 +24,6 @@ dependencies:
|
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.16'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: codeclimate-test-reporter
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '1.0'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '1.0'
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: pry-byebug
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -122,34 +108,6 @@ dependencies:
|
|
122
108
|
- - "~>"
|
123
109
|
- !ruby/object:Gem::Version
|
124
110
|
version: '1.24'
|
125
|
-
- !ruby/object:Gem::Dependency
|
126
|
-
name: simplecov
|
127
|
-
requirement: !ruby/object:Gem::Requirement
|
128
|
-
requirements:
|
129
|
-
- - "~>"
|
130
|
-
- !ruby/object:Gem::Version
|
131
|
-
version: '0.16'
|
132
|
-
type: :development
|
133
|
-
prerelease: false
|
134
|
-
version_requirements: !ruby/object:Gem::Requirement
|
135
|
-
requirements:
|
136
|
-
- - "~>"
|
137
|
-
- !ruby/object:Gem::Version
|
138
|
-
version: '0.16'
|
139
|
-
- !ruby/object:Gem::Dependency
|
140
|
-
name: simplecov-console
|
141
|
-
requirement: !ruby/object:Gem::Requirement
|
142
|
-
requirements:
|
143
|
-
- - "~>"
|
144
|
-
- !ruby/object:Gem::Version
|
145
|
-
version: '0.4'
|
146
|
-
type: :development
|
147
|
-
prerelease: false
|
148
|
-
version_requirements: !ruby/object:Gem::Requirement
|
149
|
-
requirements:
|
150
|
-
- - "~>"
|
151
|
-
- !ruby/object:Gem::Version
|
152
|
-
version: '0.4'
|
153
111
|
description: A Ruby gem to manipulate Bitcoin money without loss of precision.
|
154
112
|
email:
|
155
113
|
- bruno@bsoares.com
|