satoshiproc-unit 0.2.2
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.
- checksums.yaml +7 -0
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +75 -0
- data/LICENSE.txt +20 -0
- data/README.md +69 -0
- data/Rakefile +26 -0
- data/VERSION +1 -0
- data/lib/satoshiproc-unit.rb +5 -0
- data/lib/satoshiproc.rb +164 -0
- data/satoshiproc-unit.gemspec +58 -0
- data/spec/satoshiproc_spec.rb +90 -0
- metadata +100 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 0e501a119e4ea258b8dd96ba9ac00c6243ccdc3a
|
|
4
|
+
data.tar.gz: 45e0ae68d41b2fc38944e5c7189f75438ea44040
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a055166890e3c0b3a2750aaa9cd4b51d8a1c8f1f505fe4e59da185b6194bd15da16e2e90e3a5a6f3f5416c2fbf0f9d78489d19aead8beb40d1d75b47b7aa6728
|
|
7
|
+
data.tar.gz: dfbd11b5565e4c3783cbd90a51c1733bd9638d39f8ad2d98b0f5c1c3294a9f8201a4778437ae390fcccc7d4c6983135c45026c38e70bf39cc4742c37281f63d5
|
data/.document
ADDED
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
GEM
|
|
2
|
+
remote: http://rubygems.org/
|
|
3
|
+
specs:
|
|
4
|
+
addressable (2.5.0)
|
|
5
|
+
public_suffix (~> 2.0, >= 2.0.2)
|
|
6
|
+
builder (3.2.2)
|
|
7
|
+
descendants_tracker (0.0.4)
|
|
8
|
+
thread_safe (~> 0.3, >= 0.3.1)
|
|
9
|
+
diff-lcs (1.2.5)
|
|
10
|
+
faraday (0.9.2)
|
|
11
|
+
multipart-post (>= 1.2, < 3)
|
|
12
|
+
git (1.3.0)
|
|
13
|
+
github_api (0.11.3)
|
|
14
|
+
addressable (~> 2.3)
|
|
15
|
+
descendants_tracker (~> 0.0.1)
|
|
16
|
+
faraday (~> 0.8, < 0.10)
|
|
17
|
+
hashie (>= 1.2)
|
|
18
|
+
multi_json (>= 1.7.5, < 2.0)
|
|
19
|
+
nokogiri (~> 1.6.0)
|
|
20
|
+
oauth2
|
|
21
|
+
hashie (3.4.6)
|
|
22
|
+
highline (1.7.8)
|
|
23
|
+
jeweler (2.1.2)
|
|
24
|
+
builder
|
|
25
|
+
bundler (>= 1.0)
|
|
26
|
+
git (>= 1.2.5)
|
|
27
|
+
github_api (~> 0.11.0)
|
|
28
|
+
highline (>= 1.6.15)
|
|
29
|
+
nokogiri (>= 1.5.10)
|
|
30
|
+
rake
|
|
31
|
+
rdoc
|
|
32
|
+
semver
|
|
33
|
+
jwt (1.5.6)
|
|
34
|
+
mini_portile2 (2.1.0)
|
|
35
|
+
multi_json (1.12.1)
|
|
36
|
+
multi_xml (0.5.5)
|
|
37
|
+
multipart-post (2.0.0)
|
|
38
|
+
nokogiri (1.6.8.1)
|
|
39
|
+
mini_portile2 (~> 2.1.0)
|
|
40
|
+
oauth2 (1.2.0)
|
|
41
|
+
faraday (>= 0.8, < 0.10)
|
|
42
|
+
jwt (~> 1.0)
|
|
43
|
+
multi_json (~> 1.3)
|
|
44
|
+
multi_xml (~> 0.5)
|
|
45
|
+
rack (>= 1.2, < 3)
|
|
46
|
+
public_suffix (2.0.3)
|
|
47
|
+
rack (2.0.1)
|
|
48
|
+
rake (11.3.0)
|
|
49
|
+
rdoc (5.0.0)
|
|
50
|
+
rspec (3.5.0)
|
|
51
|
+
rspec-core (~> 3.5.0)
|
|
52
|
+
rspec-expectations (~> 3.5.0)
|
|
53
|
+
rspec-mocks (~> 3.5.0)
|
|
54
|
+
rspec-core (3.5.4)
|
|
55
|
+
rspec-support (~> 3.5.0)
|
|
56
|
+
rspec-expectations (3.5.0)
|
|
57
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
58
|
+
rspec-support (~> 3.5.0)
|
|
59
|
+
rspec-mocks (3.5.0)
|
|
60
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
61
|
+
rspec-support (~> 3.5.0)
|
|
62
|
+
rspec-support (3.5.0)
|
|
63
|
+
semver (1.0.1)
|
|
64
|
+
thread_safe (0.3.5)
|
|
65
|
+
|
|
66
|
+
PLATFORMS
|
|
67
|
+
ruby
|
|
68
|
+
|
|
69
|
+
DEPENDENCIES
|
|
70
|
+
bundler (~> 1.0)
|
|
71
|
+
jeweler (~> 2.1.2)
|
|
72
|
+
rspec
|
|
73
|
+
|
|
74
|
+
BUNDLED WITH
|
|
75
|
+
1.12.4
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2014 Roman Snitko
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
4
|
+
a copy of this software and associated documentation files (the
|
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
9
|
+
the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be
|
|
12
|
+
included in all copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
SatoshiProc Unit
|
|
2
|
+
================
|
|
3
|
+
This tiny gem allows you to make easy conversions from one Proc denomination into another.
|
|
4
|
+
It provides a class, which objects would essentially represent an amount of procss measured
|
|
5
|
+
in the smallest possible denomination, which is Satoshiproc. You can then call methods on these objects
|
|
6
|
+
to convert it various other denominations.
|
|
7
|
+
|
|
8
|
+
### Installation
|
|
9
|
+
|
|
10
|
+
gem install satoshiproc-unit
|
|
11
|
+
|
|
12
|
+
### Usage
|
|
13
|
+
|
|
14
|
+
Here's how it might look:
|
|
15
|
+
|
|
16
|
+
s = SatoshiProc.new(1) # By default, it accepts amounts in PROC denomination
|
|
17
|
+
s.to_i # => 100000000 (in Satoshiprocs)
|
|
18
|
+
s.to_mbtc # => 1000.0
|
|
19
|
+
s.to_btc # => 1.0
|
|
20
|
+
|
|
21
|
+
When creating a SatoshiProc object, you can also specify which unit is being used to pass the amount,
|
|
22
|
+
for example:
|
|
23
|
+
|
|
24
|
+
s = Satoshi.new(1, from_unit: :mproc)
|
|
25
|
+
|
|
26
|
+
s.to_i # => 100000
|
|
27
|
+
s.to_mproc # => 1.0
|
|
28
|
+
s.to_proc # => 0.001
|
|
29
|
+
|
|
30
|
+
There's also a special method which is called `#to_unit`, it always converts to the denomination
|
|
31
|
+
specified in the `:to_unit` option in the constructor. It becomes really handy when you want to
|
|
32
|
+
specify your preferred denomination used througout the program in just one place
|
|
33
|
+
(to be able to change it later):
|
|
34
|
+
|
|
35
|
+
DENOMINATION = :mproc
|
|
36
|
+
s = SatoshiProc.new(1, from_unit: DENOMINATION, to_unit: DENOMINATION)
|
|
37
|
+
s.to_unit # => 1.0 (in mBTC)
|
|
38
|
+
|
|
39
|
+
This can be shortened to just the `:unit` option:
|
|
40
|
+
|
|
41
|
+
s = SatoshiProc.new(1, unit: DENOMINATION)
|
|
42
|
+
|
|
43
|
+
But, of course, if you want your "from" denomination and "to" denomination to be different, then
|
|
44
|
+
you'd have to pass them both manually:
|
|
45
|
+
|
|
46
|
+
s = SatoshiProc.new(1, from_unit: :mproc, to_unit: :proc)
|
|
47
|
+
s.to_unit # => 0.001
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
###Comparing satoshiprocs
|
|
52
|
+
|
|
53
|
+
SatoshiProc objects come with methods and coercions for comparing itself with both other Satoshi objects and also numeric values:
|
|
54
|
+
|
|
55
|
+
s1 = SatoshiProc.new(1)
|
|
56
|
+
s2 = SatoshiProc.new(2)
|
|
57
|
+
|
|
58
|
+
s1 > s2 # => false
|
|
59
|
+
s1 < s2 # => true
|
|
60
|
+
s1 == s2 # => false
|
|
61
|
+
|
|
62
|
+
s1 > 1 # => true
|
|
63
|
+
1 > s2 # => false
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
### Unit tests
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
Run `rspec spec`. Pull requests with more unit tests are welcome.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'rubygems'
|
|
4
|
+
require 'bundler'
|
|
5
|
+
begin
|
|
6
|
+
Bundler.setup(:default, :development)
|
|
7
|
+
rescue Bundler::BundlerError => e
|
|
8
|
+
$stderr.puts e.message
|
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
|
10
|
+
exit e.status_code
|
|
11
|
+
end
|
|
12
|
+
require 'rake'
|
|
13
|
+
|
|
14
|
+
require 'jeweler'
|
|
15
|
+
Jeweler::Tasks.new do |gem|
|
|
16
|
+
# gem is a Gem::Specification... see http://guides.rubygems.org/specification-reference/ for more options
|
|
17
|
+
gem.name = "satoshiproc-unit"
|
|
18
|
+
gem.homepage = "http://github.com/oscarguindzberg/satoshiproc-unit"
|
|
19
|
+
gem.license = "MIT"
|
|
20
|
+
gem.summary = %Q{Converts various proc denominations in Satoshis and back}
|
|
21
|
+
gem.description = %Q{Converts various proc denominations in Satoshis and back}
|
|
22
|
+
gem.email = "oscar.guindzberg@gmail.com"
|
|
23
|
+
gem.authors = ["Roman Snitko", "Oscar Guindzberg"]
|
|
24
|
+
# dependencies defined in Gemfile
|
|
25
|
+
end
|
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
data/VERSION
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
0.2.2
|
data/lib/satoshiproc.rb
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
class SatoshiProc
|
|
2
|
+
|
|
3
|
+
# Says how many digits after the decimal point
|
|
4
|
+
# a denomination has.
|
|
5
|
+
UNIT_DENOMINATIONS = {
|
|
6
|
+
proc: 8,
|
|
7
|
+
mproc: 5,
|
|
8
|
+
satoshiproc: 0
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
class TooManyDigitsAfterDecimalPoint < Exception;end
|
|
12
|
+
class TooLarge < Exception;end
|
|
13
|
+
|
|
14
|
+
attr_reader :value, :from_unit, :to_unit
|
|
15
|
+
|
|
16
|
+
def initialize(n=nil, from_unit: 'proc', to_unit: 'proc', unit: nil)
|
|
17
|
+
n = 0 if n.nil?
|
|
18
|
+
if unit
|
|
19
|
+
@from_unit = @to_unit = unit.downcase.to_sym
|
|
20
|
+
else
|
|
21
|
+
@from_unit = from_unit.downcase.to_sym
|
|
22
|
+
@to_unit = to_unit.downcase.to_sym
|
|
23
|
+
end
|
|
24
|
+
@value = convert_to_satoshiproc(n) if n
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def to_proc(as: :number)
|
|
28
|
+
to_denomination(UNIT_DENOMINATIONS[:proc], as: as)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def to_mproc(as: :number)
|
|
32
|
+
to_denomination(UNIT_DENOMINATIONS[:mproc], as: as)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def to_unit(as: :number)
|
|
36
|
+
to_denomination(UNIT_DENOMINATIONS[@to_unit], as: as)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def to_i
|
|
40
|
+
return 0 if @value.nil?
|
|
41
|
+
@value
|
|
42
|
+
end
|
|
43
|
+
alias :satoshiproc_value :to_i
|
|
44
|
+
|
|
45
|
+
def to_s
|
|
46
|
+
to_unit.to_s
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def value=(n)
|
|
50
|
+
n = 0 if n.nil?
|
|
51
|
+
@value = convert_to_satoshiproc(n)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def satoshiproc_value=(v)
|
|
55
|
+
@value = v
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def >(i)
|
|
59
|
+
self.to_i > i
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def <(i)
|
|
63
|
+
self.to_i < i
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def >=(i)
|
|
67
|
+
self.to_i >= i
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def <=(i)
|
|
71
|
+
self.to_i <= i
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def ==(i)
|
|
75
|
+
self.to_i == i
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def +(i)
|
|
79
|
+
if i.kind_of?(SatoshiProc)
|
|
80
|
+
SatoshiProc.new(self.to_i + i, from_unit: :satoshiproc)
|
|
81
|
+
else
|
|
82
|
+
self.to_i + i
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def -(i)
|
|
87
|
+
if i.kind_of?(SatoshiProc)
|
|
88
|
+
SatoshiProc.new(self.to_i - i, from_unit: :satoshiproc)
|
|
89
|
+
else
|
|
90
|
+
self.to_i - i
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# IMPORTANT: multiplication is done on satoshiprocs, not proc.
|
|
95
|
+
# 0.01*0.02 PROC will be a larger value.
|
|
96
|
+
def *(i)
|
|
97
|
+
if i.kind_of?(SatoshiProc)
|
|
98
|
+
SatoshiProc.new(self.to_i * i, from_unit: :satoshiproc)
|
|
99
|
+
else
|
|
100
|
+
self.to_i * i
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def coerce(other)
|
|
105
|
+
if other.kind_of?(Integer)
|
|
106
|
+
[other, self.to_i]
|
|
107
|
+
else
|
|
108
|
+
raise TypeError, message: "SatoshiProc cannot be coerced into anything but Integer"
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
private
|
|
113
|
+
|
|
114
|
+
def to_denomination(digits_after_delimiter, as: :number)
|
|
115
|
+
sign = @value < 0 ? -1 : 1
|
|
116
|
+
val = @value.abs
|
|
117
|
+
return val if digits_after_delimiter <= 0
|
|
118
|
+
leading_zeros = "0"*(18-val.to_s.length)
|
|
119
|
+
result = leading_zeros + val.to_s
|
|
120
|
+
result.reverse!
|
|
121
|
+
result = result.slice(0..digits_after_delimiter-1) + '.' + result.slice(digits_after_delimiter..17)
|
|
122
|
+
result.reverse!
|
|
123
|
+
result = result.sub(/\A0*/, '').sub(/0*\Z/, '') # remove zeros on both sides
|
|
124
|
+
if as == :number
|
|
125
|
+
result.to_f*sign
|
|
126
|
+
else
|
|
127
|
+
if result == '.'
|
|
128
|
+
result = '0.0'
|
|
129
|
+
elsif result =~ /\A\./
|
|
130
|
+
result = "0#{result}"
|
|
131
|
+
end
|
|
132
|
+
sign == -1 ? "-#{result}" : result
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def convert_to_satoshiproc(n)
|
|
137
|
+
n = BigDecimal.new(n.to_s)
|
|
138
|
+
|
|
139
|
+
decimal_part_length = n.to_s("F").split(".")[1]
|
|
140
|
+
decimal_part_length = if decimal_part_length
|
|
141
|
+
decimal_part_length.sub(/0*\Z/, "").length
|
|
142
|
+
else
|
|
143
|
+
0
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
if decimal_part_length > UNIT_DENOMINATIONS[@from_unit]
|
|
147
|
+
raise TooManyDigitsAfterDecimalPoint,
|
|
148
|
+
"Too many digits (#{decimal_part_length}) after decimal point used for #{@from_unit} value, while #{UNIT_DENOMINATIONS[@from_unit]} allowed"
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
n = ("%.#{UNIT_DENOMINATIONS[@from_unit]}f" % n) # otherwise we might see a scientific notation
|
|
152
|
+
n = n.split('.')
|
|
153
|
+
n[1] ||= '' # in the case where there's no decimal part
|
|
154
|
+
n[1] += "0"*(UNIT_DENOMINATIONS[@from_unit]-n[1].length) if n[1]
|
|
155
|
+
|
|
156
|
+
if(n.join.to_i > 21_000_000_00000000)
|
|
157
|
+
raise TooLarge, "Max value for Proc is 21 million PROC"
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
n.join.to_i
|
|
161
|
+
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# Generated by jeweler
|
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
|
4
|
+
# -*- encoding: utf-8 -*-
|
|
5
|
+
# stub: satoshiproc-unit 0.2.2 ruby lib
|
|
6
|
+
|
|
7
|
+
Gem::Specification.new do |s|
|
|
8
|
+
s.name = "satoshiproc-unit".freeze
|
|
9
|
+
s.version = "0.2.2"
|
|
10
|
+
|
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
|
|
12
|
+
s.require_paths = ["lib".freeze]
|
|
13
|
+
s.authors = ["Roman Snitko".freeze, "Oscar Guindzberg".freeze]
|
|
14
|
+
s.date = "2017-06-20"
|
|
15
|
+
s.description = "Converts various proc denominations in Satoshis and back".freeze
|
|
16
|
+
s.email = "oscar.guindzberg@gmail.com".freeze
|
|
17
|
+
s.extra_rdoc_files = [
|
|
18
|
+
"LICENSE.txt",
|
|
19
|
+
"README.md"
|
|
20
|
+
]
|
|
21
|
+
s.files = [
|
|
22
|
+
".document",
|
|
23
|
+
".rspec",
|
|
24
|
+
"Gemfile",
|
|
25
|
+
"Gemfile.lock",
|
|
26
|
+
"LICENSE.txt",
|
|
27
|
+
"README.md",
|
|
28
|
+
"Rakefile",
|
|
29
|
+
"VERSION",
|
|
30
|
+
"lib/satoshiproc-unit.rb",
|
|
31
|
+
"lib/satoshiproc.rb",
|
|
32
|
+
"satoshiproc-unit.gemspec",
|
|
33
|
+
"spec/satoshiproc_spec.rb"
|
|
34
|
+
]
|
|
35
|
+
s.homepage = "http://github.com/oscarguindzberg/satoshiproc-unit".freeze
|
|
36
|
+
s.licenses = ["MIT".freeze]
|
|
37
|
+
s.rubygems_version = "2.6.12".freeze
|
|
38
|
+
s.summary = "Converts various proc denominations in Satoshis and back".freeze
|
|
39
|
+
|
|
40
|
+
if s.respond_to? :specification_version then
|
|
41
|
+
s.specification_version = 4
|
|
42
|
+
|
|
43
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
|
44
|
+
s.add_development_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
|
45
|
+
s.add_development_dependency(%q<jeweler>.freeze, ["~> 2.1.2"])
|
|
46
|
+
s.add_development_dependency(%q<rspec>.freeze, [">= 0"])
|
|
47
|
+
else
|
|
48
|
+
s.add_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
|
49
|
+
s.add_dependency(%q<jeweler>.freeze, ["~> 2.1.2"])
|
|
50
|
+
s.add_dependency(%q<rspec>.freeze, [">= 0"])
|
|
51
|
+
end
|
|
52
|
+
else
|
|
53
|
+
s.add_dependency(%q<bundler>.freeze, ["~> 1.0"])
|
|
54
|
+
s.add_dependency(%q<jeweler>.freeze, ["~> 2.1.2"])
|
|
55
|
+
s.add_dependency(%q<rspec>.freeze, [">= 0"])
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
require_relative '../lib/satoshiproc-unit'
|
|
2
|
+
|
|
3
|
+
describe SatoshiProc do
|
|
4
|
+
|
|
5
|
+
it "creates a Bignum representing value in satoshiproc units" do
|
|
6
|
+
expect(SatoshiProc.new(1.00).to_i).to eq(100000000)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "takes care of the sign before the value" do
|
|
10
|
+
expect(SatoshiProc.new(-1.00).to_i).to eq(-100000000)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "converts satoshiproc unit back to some more common denomination" do
|
|
14
|
+
expect(SatoshiProc.new(1.00).to_proc).to eq(1)
|
|
15
|
+
expect(SatoshiProc.new(1.08763).to_proc).to eq(1.08763)
|
|
16
|
+
expect(SatoshiProc.new(1.08763).to_mproc).to eq(1087.63)
|
|
17
|
+
expect(SatoshiProc.new(-1.08763).to_mproc).to eq(-1087.63)
|
|
18
|
+
expect(SatoshiProc.new(0.00000001).to_i).to eq(1)
|
|
19
|
+
expect(SatoshiProc.new(0.00000001).to_mproc).to eq(0.00001)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it "converts from various source denominations" do
|
|
23
|
+
expect(SatoshiProc.new(1, unit: 'mproc').to_proc).to eq(0.001)
|
|
24
|
+
expect(SatoshiProc.new(1, unit: 'mproc').to_unit).to eq(1)
|
|
25
|
+
expect(SatoshiProc.new(10000000, unit: 'mproc').to_unit).to eq(10000000)
|
|
26
|
+
satoshiproc = SatoshiProc.new(10000000, unit: 'mproc')
|
|
27
|
+
satoshiproc.satoshiproc_value = 1
|
|
28
|
+
expect(satoshiproc.to_unit).to eq(0.00001)
|
|
29
|
+
expect(SatoshiProc.new(100, unit: 'mproc').to_i).to eq(10000000)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "treats nil in value as 0" do
|
|
33
|
+
expect(SatoshiProc.new < 1).to be_truthy
|
|
34
|
+
expect(SatoshiProc.new > 1).to be_falsy
|
|
35
|
+
expect(SatoshiProc.new == 0).to be_truthy
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "converts negative values correctly" do
|
|
39
|
+
expect(SatoshiProc.new(-1.00, unit: :mproc).to_proc).to eq(-0.001)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it "converts zero values correctly" do
|
|
43
|
+
expect(SatoshiProc.new(0, unit: :mproc).to_unit).to eq(0)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "converts nil values correctly" do
|
|
47
|
+
s = SatoshiProc.new(nil, unit: :mproc)
|
|
48
|
+
expect(s.value).to eq(0)
|
|
49
|
+
s.value = nil
|
|
50
|
+
expect(s.to_unit).to eq(0)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it "displays one SatoshiProc in human form, not math form" do
|
|
54
|
+
one_satoshiproc = SatoshiProc.new(1, from_unit: :satoshiproc, to_unit: :proc)
|
|
55
|
+
expect(one_satoshiproc.to_unit(as: :string)).not_to eq('1.0e-08')
|
|
56
|
+
expect(one_satoshiproc.to_unit(as: :string)).to eq('0.00000001')
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it "displays zero SatoshiProc in human form, not math form" do
|
|
60
|
+
zero_satoshiproc = SatoshiProc.new(0, from_unit: :satoshiproc, to_unit: :proc)
|
|
61
|
+
expect(zero_satoshiproc.to_unit(as: :string)).to eq('0.0')
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it "raises exception if decimal part contains more digits than allowed by from_value" do
|
|
65
|
+
expect( -> { SatoshiProc.new(0.001000888888888888888, from_unit: :proc).to_unit }).to raise_exception(SatoshiProc::TooManyDigitsAfterDecimalPoint)
|
|
66
|
+
expect( -> { SatoshiProc.new("0.001000999999999999999", from_unit: :proc).to_unit }).to raise_exception(SatoshiProc::TooManyDigitsAfterDecimalPoint)
|
|
67
|
+
expect( -> { SatoshiProc.new(0.001000999, from_unit: :proc).to_unit }).to raise_exception(SatoshiProc::TooManyDigitsAfterDecimalPoint)
|
|
68
|
+
expect( -> { SatoshiProc.new(0.00100099, from_unit: :proc).to_unit }).not_to raise_exception
|
|
69
|
+
expect( -> { SatoshiProc.new(0.123456789, from_unit: :proc) }).to raise_exception(SatoshiProc::TooManyDigitsAfterDecimalPoint)
|
|
70
|
+
expect( -> { SatoshiProc.new(0.12345678, from_unit: :proc).to_unit }).not_to raise_exception
|
|
71
|
+
expect( -> { SatoshiProc.new(nil, from_unit: :proc).to_unit }).not_to raise_exception
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it "disallows to create values more than 21mil PROC" do
|
|
75
|
+
expect( -> { SatoshiProc.new(21_000_001) }).to raise_exception(SatoshiProc::TooLarge)
|
|
76
|
+
expect( -> { SatoshiProc.new(21_000_000) }).not_to raise_exception
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it "returns satoshiproc for +,- and * methods if both operands are SatoshiProc" do
|
|
80
|
+
s1 = SatoshiProc.new(0.001, from_unit: :proc)
|
|
81
|
+
s2 = SatoshiProc.new(0.002, from_unit: :proc)
|
|
82
|
+
expect(s1+s2).to be_kind_of(SatoshiProc)
|
|
83
|
+
expect((s1+s2).to_unit).to eq(0.003)
|
|
84
|
+
expect(s2-s1).to be_kind_of(SatoshiProc)
|
|
85
|
+
expect((s2-s1).to_unit).to eq(0.001)
|
|
86
|
+
expect(s2*s1).to be_kind_of(SatoshiProc)
|
|
87
|
+
expect((s2*s1).to_unit).to eq(200)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: satoshiproc-unit
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Roman Snitko
|
|
8
|
+
- Oscar Guindzberg
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2017-06-20 00:00:00.000000000 Z
|
|
13
|
+
dependencies:
|
|
14
|
+
- !ruby/object:Gem::Dependency
|
|
15
|
+
name: bundler
|
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
|
17
|
+
requirements:
|
|
18
|
+
- - "~>"
|
|
19
|
+
- !ruby/object:Gem::Version
|
|
20
|
+
version: '1.0'
|
|
21
|
+
type: :development
|
|
22
|
+
prerelease: false
|
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
24
|
+
requirements:
|
|
25
|
+
- - "~>"
|
|
26
|
+
- !ruby/object:Gem::Version
|
|
27
|
+
version: '1.0'
|
|
28
|
+
- !ruby/object:Gem::Dependency
|
|
29
|
+
name: jeweler
|
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - "~>"
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: 2.1.2
|
|
35
|
+
type: :development
|
|
36
|
+
prerelease: false
|
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - "~>"
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: 2.1.2
|
|
42
|
+
- !ruby/object:Gem::Dependency
|
|
43
|
+
name: rspec
|
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
|
45
|
+
requirements:
|
|
46
|
+
- - ">="
|
|
47
|
+
- !ruby/object:Gem::Version
|
|
48
|
+
version: '0'
|
|
49
|
+
type: :development
|
|
50
|
+
prerelease: false
|
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: '0'
|
|
56
|
+
description: Converts various proc denominations in Satoshis and back
|
|
57
|
+
email: oscar.guindzberg@gmail.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files:
|
|
61
|
+
- LICENSE.txt
|
|
62
|
+
- README.md
|
|
63
|
+
files:
|
|
64
|
+
- ".document"
|
|
65
|
+
- ".rspec"
|
|
66
|
+
- Gemfile
|
|
67
|
+
- Gemfile.lock
|
|
68
|
+
- LICENSE.txt
|
|
69
|
+
- README.md
|
|
70
|
+
- Rakefile
|
|
71
|
+
- VERSION
|
|
72
|
+
- lib/satoshiproc-unit.rb
|
|
73
|
+
- lib/satoshiproc.rb
|
|
74
|
+
- satoshiproc-unit.gemspec
|
|
75
|
+
- spec/satoshiproc_spec.rb
|
|
76
|
+
homepage: http://github.com/oscarguindzberg/satoshiproc-unit
|
|
77
|
+
licenses:
|
|
78
|
+
- MIT
|
|
79
|
+
metadata: {}
|
|
80
|
+
post_install_message:
|
|
81
|
+
rdoc_options: []
|
|
82
|
+
require_paths:
|
|
83
|
+
- lib
|
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
85
|
+
requirements:
|
|
86
|
+
- - ">="
|
|
87
|
+
- !ruby/object:Gem::Version
|
|
88
|
+
version: '0'
|
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
|
+
requirements:
|
|
91
|
+
- - ">="
|
|
92
|
+
- !ruby/object:Gem::Version
|
|
93
|
+
version: '0'
|
|
94
|
+
requirements: []
|
|
95
|
+
rubyforge_project:
|
|
96
|
+
rubygems_version: 2.6.12
|
|
97
|
+
signing_key:
|
|
98
|
+
specification_version: 4
|
|
99
|
+
summary: Converts various proc denominations in Satoshis and back
|
|
100
|
+
test_files: []
|