store_as_int 0.0.9 → 0.0.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/store_as_int/base.rb +127 -65
- data/lib/store_as_int/exchange_rate.rb +37 -2
- data/lib/store_as_int/money.rb +36 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 237e8f13fbdf567a8e6a3a87adaf4633185ef1df45ee94deec35109ad3cc2a93
|
4
|
+
data.tar.gz: 85df459643cd74d291996079526a7f2716dd476ffe16fb2fac3569f3f73e34e3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: be707c1a0705d8ad41d391c7d25014c5bd661b48fafe9762ef51697bd8412625c90d58fc2e436b30f042d4a11e4a2ae96b4cd870563dc1001e9a097cad269b7b
|
7
|
+
data.tar.gz: 2061b6c4e2c1eed125753fe8c672d43d31a37d69dca3eb6b27b0e617a828b7ad6e806f76f907fc92d649d25009bf1780b97cd0c7a0057d6ea008b0dee603ece0
|
data/lib/store_as_int/base.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
module StoreAsInt
|
2
|
-
class Base
|
2
|
+
class Base < Numeric
|
3
3
|
require 'bigdecimal'
|
4
4
|
require 'bigdecimal/util'
|
5
5
|
|
6
6
|
# == Constants ============================================================
|
7
|
-
|
8
|
-
DECIMALS
|
9
|
-
SYM
|
10
|
-
STR_FORMAT
|
7
|
+
ACCURACY ||= 5
|
8
|
+
DECIMALS ||= 2
|
9
|
+
SYM ||= nil
|
10
|
+
STR_FORMAT ||= nil
|
11
|
+
OPERATORS ||= [:+, :-, :*, :/, :%, :**].map {|sym| [sym]*2}.to_h
|
11
12
|
|
12
13
|
# == Attributes ============================================================
|
13
14
|
attr_accessor :num
|
@@ -17,15 +18,27 @@ module StoreAsInt
|
|
17
18
|
|
18
19
|
# == Class Methods ========================================================
|
19
20
|
def self.===(other)
|
20
|
-
|
21
|
+
!!(
|
22
|
+
Numeric === other ||
|
23
|
+
(
|
24
|
+
other.is_a?(Class) ?
|
25
|
+
other :
|
26
|
+
other.class
|
27
|
+
) <= self
|
28
|
+
) || !!(
|
29
|
+
other.instance_of?(String) &&
|
30
|
+
(
|
31
|
+
other.gsub(/(,|\s)/, '') =~ /^(\-|\+)?#{Regexp.quote("\\#{sym}" || '[^0-9]')}?([0-9]+)(\.[0-9]+)?$/
|
32
|
+
)
|
33
|
+
)
|
21
34
|
end
|
22
35
|
|
23
|
-
def self
|
24
|
-
self
|
36
|
+
def self.accuracy
|
37
|
+
self::ACCURACY || 0
|
25
38
|
end
|
26
39
|
|
27
40
|
def self.base
|
28
|
-
|
41
|
+
10 ** accuracy.to_i
|
29
42
|
end
|
30
43
|
|
31
44
|
def self.decimals
|
@@ -33,96 +46,142 @@ module StoreAsInt
|
|
33
46
|
end
|
34
47
|
|
35
48
|
def self.sym
|
36
|
-
self::SYM
|
49
|
+
self::SYM
|
37
50
|
end
|
38
51
|
|
39
52
|
def self.str_format
|
40
53
|
self::STR_FORMAT
|
41
54
|
end
|
42
55
|
|
56
|
+
def self.operators
|
57
|
+
self::OPERATORS
|
58
|
+
end
|
59
|
+
|
60
|
+
# == Boolean Methods ======================================================
|
61
|
+
def is_a?(klass)
|
62
|
+
kind_of?(klass)
|
63
|
+
end
|
64
|
+
|
65
|
+
def is_an?(klass)
|
66
|
+
kind_of?(klass)
|
67
|
+
end
|
68
|
+
|
69
|
+
def kind_of?(klass)
|
70
|
+
value.kind_of?(klass) || self.class == klass || super(klass)
|
71
|
+
end
|
72
|
+
|
73
|
+
def instance_of?(klass)
|
74
|
+
self.class == klass
|
75
|
+
end
|
76
|
+
|
77
|
+
def present?
|
78
|
+
begin
|
79
|
+
self.num.present?
|
80
|
+
rescue NoMethodError
|
81
|
+
!self.num.nil? && !(self.num.to_s == "")
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# == Comparison Methods ===================================================
|
86
|
+
def <=>(compare)
|
87
|
+
value <=> convert(compare).value
|
88
|
+
end
|
89
|
+
|
90
|
+
def ==(compare)
|
91
|
+
value == convert(compare).value
|
92
|
+
end
|
93
|
+
|
94
|
+
def ===(compare)
|
95
|
+
self.== compare
|
96
|
+
end
|
97
|
+
|
43
98
|
# == Instance Methods =====================================================
|
44
99
|
def initialize(new_val = nil)
|
45
|
-
return self.num =
|
100
|
+
return self.num = nil unless new_val
|
46
101
|
|
47
102
|
if new_val.is_a?(self.class)
|
48
103
|
self.num = new_val.value
|
49
104
|
elsif new_val.is_a?(Integer)
|
50
105
|
self.num = new_val
|
51
106
|
else
|
107
|
+
if new_val.is_a?(String)
|
108
|
+
begin
|
109
|
+
new_val =
|
110
|
+
new_val.
|
111
|
+
gsub(/(,|\s)/, '').
|
112
|
+
match(/(\-|\+)?#{Regexp.quote("\\#{sym}" || '[^0-9]')}?([0-9]+)(\.[0-9]+)?$/)[1..-1].join("")
|
113
|
+
rescue NoMethodError
|
114
|
+
return self.num = 0
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
52
118
|
self.num = (new_val.to_d * self.class.base).to_i
|
53
119
|
end
|
54
120
|
end
|
55
121
|
|
56
|
-
def
|
57
|
-
self.class.
|
122
|
+
def accuracy
|
123
|
+
@accuracy ||= self.class.accuracy || 0
|
58
124
|
end
|
59
125
|
|
60
|
-
def
|
61
|
-
|
126
|
+
def as_json(*args)
|
127
|
+
self.num.as_json(*args)
|
62
128
|
end
|
63
129
|
|
64
|
-
def
|
65
|
-
base
|
130
|
+
def base
|
131
|
+
@base ||= 10 ** accuracy
|
66
132
|
end
|
67
133
|
|
68
|
-
def
|
69
|
-
|
134
|
+
def base_float
|
135
|
+
base.to_f
|
70
136
|
end
|
71
137
|
|
72
|
-
def
|
73
|
-
|
138
|
+
def coerce(other_val)
|
139
|
+
[convert(other_val), self]
|
74
140
|
end
|
75
141
|
|
76
142
|
def convert(other_val)
|
77
143
|
self.class.new(other_val)
|
78
144
|
end
|
79
145
|
|
80
|
-
def
|
81
|
-
|
82
|
-
end
|
83
|
-
|
84
|
-
def == (compare)
|
85
|
-
value == convert(compare).value
|
86
|
-
end
|
87
|
-
|
88
|
-
def kind_of?(klass)
|
89
|
-
self.num.kind_of?(klass)
|
90
|
-
end
|
91
|
-
|
92
|
-
def to_i
|
93
|
-
self.num.to_i
|
146
|
+
def decimals
|
147
|
+
@decimals ||= self.class.decimals
|
94
148
|
end
|
95
149
|
|
96
|
-
def
|
97
|
-
|
150
|
+
def inspect
|
151
|
+
to_s(true)
|
98
152
|
end
|
99
153
|
|
100
|
-
def
|
101
|
-
|
154
|
+
def method_missing(name, *args, &blk)
|
155
|
+
if self.class.operators[name.to_sym]
|
156
|
+
self.class.new(value.__send__(name, self.class.new(*args).value))
|
157
|
+
else
|
158
|
+
ret = value.send(name, *args, &blk)
|
159
|
+
ret.is_a?(Numeric) ? self.class.new(ret) : ret
|
160
|
+
end
|
102
161
|
end
|
103
162
|
|
104
|
-
def
|
105
|
-
|
163
|
+
def negative_sign
|
164
|
+
value < 0 ? '-' : ''
|
106
165
|
end
|
107
166
|
|
108
|
-
def
|
109
|
-
self.
|
167
|
+
def sym
|
168
|
+
@sym ||= self.class.sym || ''
|
110
169
|
end
|
111
170
|
|
112
|
-
def
|
113
|
-
|
171
|
+
def sym=(new_sym)
|
172
|
+
@sym = new_sym
|
114
173
|
end
|
115
174
|
|
116
|
-
def
|
117
|
-
|
175
|
+
def to_d
|
176
|
+
to_i.to_d/base
|
118
177
|
end
|
119
178
|
|
120
|
-
def
|
121
|
-
|
179
|
+
def to_f
|
180
|
+
to_i/base_float
|
122
181
|
end
|
123
182
|
|
124
|
-
def
|
125
|
-
|
183
|
+
def to_i
|
184
|
+
self.num.to_i
|
126
185
|
end
|
127
186
|
|
128
187
|
def to_s(w_sym = false)
|
@@ -135,25 +194,28 @@ module StoreAsInt
|
|
135
194
|
end
|
136
195
|
end
|
137
196
|
|
138
|
-
def
|
139
|
-
|
140
|
-
value.present?
|
141
|
-
rescue NoMethodError
|
142
|
-
!value.nil? && !(value.to_s == "")
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
def method_missing(name, *args, &blk)
|
147
|
-
ret = value.send(name, *args, &blk)
|
148
|
-
ret.is_a?(Numeric) ? self.class.new(ret) : ret
|
197
|
+
def value
|
198
|
+
self.num || 0
|
149
199
|
end
|
150
200
|
|
151
201
|
private
|
152
202
|
def str_format
|
153
203
|
@str_format ||= self.class.str_format || ->(passed, w_sym) do
|
154
|
-
return
|
155
|
-
str = "#{
|
156
|
-
|
204
|
+
return '' unless w_sym || passed.present?
|
205
|
+
str = "#{
|
206
|
+
passed.negative_sign
|
207
|
+
}#{
|
208
|
+
w_sym ? passed.sym : ''
|
209
|
+
}#{
|
210
|
+
passed.decimals ?
|
211
|
+
sprintf("%0.0#{passed.decimals.to_i}f", passed.to_d.abs) :
|
212
|
+
passed.to_i.to_s
|
213
|
+
}".reverse.split('.')
|
214
|
+
|
215
|
+
str[-1] =
|
216
|
+
str[-1].
|
217
|
+
gsub(/(\d{3})(?=\d)/, w_sym ? '\\1,' : '\\1')
|
218
|
+
|
157
219
|
str.join('.').reverse
|
158
220
|
end
|
159
221
|
end
|
@@ -2,10 +2,45 @@ module StoreAsInt
|
|
2
2
|
unless defined?(StoreAsInt::Base)
|
3
3
|
require_relative './base'
|
4
4
|
end
|
5
|
-
|
5
|
+
|
6
6
|
class ExchangeRate < StoreAsInt::Base
|
7
|
-
|
7
|
+
require 'bigdecimal'
|
8
|
+
require 'bigdecimal/util'
|
9
|
+
|
10
|
+
# == Constants ============================================================
|
11
|
+
ACCURACY = 10
|
8
12
|
DECIMALS = 4
|
9
13
|
SYM = '%'
|
14
|
+
|
15
|
+
# == Attributes ============================================================
|
16
|
+
|
17
|
+
# == Extensions ===========================================================
|
18
|
+
|
19
|
+
# == Class Methods ========================================================
|
20
|
+
def self.extend_numerics
|
21
|
+
Numeric.include StoreAsInt::ActsAsExchangeRateInt
|
22
|
+
end
|
23
|
+
# == Boolean Methods ======================================================
|
24
|
+
|
25
|
+
# == Comparison Methods ===================================================
|
26
|
+
|
27
|
+
# == Instance Methods =====================================================
|
28
|
+
def exchange_rate_str
|
29
|
+
to_s(true)
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_exchange_rate
|
33
|
+
self
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module ActsAsExchangeRateInt
|
38
|
+
def exchange_rate_str
|
39
|
+
to_cents.to_s(true)
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_exchange_rate
|
43
|
+
StoreAsInt::ExchangeRate.new self
|
44
|
+
end
|
10
45
|
end
|
11
46
|
end
|
data/lib/store_as_int/money.rb
CHANGED
@@ -4,8 +4,43 @@ module StoreAsInt
|
|
4
4
|
end
|
5
5
|
|
6
6
|
class Money < StoreAsInt::Base
|
7
|
-
|
7
|
+
require 'bigdecimal'
|
8
|
+
require 'bigdecimal/util'
|
9
|
+
|
10
|
+
# == Constants ============================================================
|
11
|
+
ACCURACY = 2
|
8
12
|
DECIMALS = 2
|
9
13
|
SYM = '$'
|
14
|
+
|
15
|
+
# == Attributes ============================================================
|
16
|
+
|
17
|
+
# == Extensions ===========================================================
|
18
|
+
|
19
|
+
# == Class Methods ========================================================
|
20
|
+
def self.extend_numerics
|
21
|
+
Numeric.include StoreAsInt::ActsAsMoneyInt
|
22
|
+
end
|
23
|
+
# == Boolean Methods ======================================================
|
24
|
+
|
25
|
+
# == Comparison Methods ===================================================
|
26
|
+
|
27
|
+
# == Instance Methods =====================================================
|
28
|
+
def dollar_str
|
29
|
+
to_s(true)
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_cents
|
33
|
+
self
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module ActsAsMoneyInt
|
38
|
+
def dollar_str
|
39
|
+
to_cents.to_s(true)
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_cents
|
43
|
+
StoreAsInt::Money.new self
|
44
|
+
end
|
10
45
|
end
|
11
46
|
end
|