d12n 0.0.4 → 0.0.5
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 +4 -4
- data/LICENSE +20 -0
- data/README.md +7 -6
- data/lib/d12n/model_support.rb +24 -16
- metadata +6 -5
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9b325c6c4d24775e4056d7102380928c90291fe2
|
|
4
|
+
data.tar.gz: eb007a871dd91240dfe25195349ee8060b84533d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bb13167ab9842d3ec4a0e22c3577bb693c537693eeebc5e2f6e1d0e55c381dddc9854d9ce38a71c6c039c46e0f685f104a7018b7705a5e27eb54d75833df7162
|
|
7
|
+
data.tar.gz: 498b429e2fadeb20583592d807dd4f969344e127a9098de27598c832ab345665894f965bfc4f18b576ae5d929f619321f8cd56483d27e0750c853034a1f3b657
|
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2017 Konstantin Munteanu
|
|
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
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
# D12n
|
|
2
2
|
|
|
3
|
-
D12n (=delocalization) can be used to cast model attributes from localized format
|
|
4
|
-
additional attribute to the model which keeps the original value the user
|
|
5
|
-
original user input can be validated and rendered back to the user during
|
|
3
|
+
D12n (=delocalization) can be used to cast model attributes from localized format to an internal BigDecimal or Integer
|
|
4
|
+
representation. This is done by adding an additional attribute to the model which keeps the original value the user has
|
|
5
|
+
entered. This has the advantage that the original user input can be validated and rendered back to the user during
|
|
6
|
+
validation errors.
|
|
6
7
|
|
|
7
8
|
## Usage
|
|
8
9
|
|
|
@@ -17,7 +18,7 @@ class Dummy
|
|
|
17
18
|
include D12n::ModelSupport
|
|
18
19
|
|
|
19
20
|
attr_accessor :amount
|
|
20
|
-
d12n_attribute :amount
|
|
21
|
+
d12n_attribute :amount
|
|
21
22
|
end
|
|
22
23
|
```
|
|
23
24
|
|
|
@@ -38,8 +39,8 @@ d.amount.to_f # 3456.78, did not change
|
|
|
38
39
|
## Configuration
|
|
39
40
|
|
|
40
41
|
```ruby
|
|
41
|
-
D12n.config.strategy = D12n::Strategy::DecimalPoint # the default
|
|
42
|
-
D12n.config.strategy = D12n::Strategy::DecimalComma
|
|
42
|
+
D12n.config.strategy = D12n::Strategy::DecimalPoint # the default, no need to set it
|
|
43
|
+
D12n.config.strategy = D12n::Strategy::DecimalComma # Use this for example in DE or NL locales
|
|
43
44
|
```
|
|
44
45
|
|
|
45
46
|
### Custom method prefix
|
data/lib/d12n/model_support.rb
CHANGED
|
@@ -14,27 +14,13 @@ module D12n
|
|
|
14
14
|
|
|
15
15
|
def define_reader(name, options)
|
|
16
16
|
define_method "#{options[:prefix]}_#{name}" do
|
|
17
|
-
|
|
18
|
-
return local if local
|
|
19
|
-
number = send(name)
|
|
20
|
-
# If a factor is defined, internal presentation is an integer multiple of the local value
|
|
21
|
-
number /= options[:factor].to_f if options[:factor]
|
|
22
|
-
D12n.strategy.bigdecimal_to_formatted(number)
|
|
17
|
+
read_d12n_attribute name, options
|
|
23
18
|
end
|
|
24
19
|
end
|
|
25
20
|
|
|
26
21
|
def define_writer(name, options)
|
|
27
22
|
define_method "#{options[:prefix]}_#{name}=" do |val|
|
|
28
|
-
|
|
29
|
-
instance_variable_set "@#{options[:prefix]}_#{name}", val
|
|
30
|
-
number = D12n.strategy.formatted_to_bigdecimal(val)
|
|
31
|
-
# If a factor is defined, internal presentation is an integer multiple of the local value
|
|
32
|
-
number = (number * options[:factor].to_f).to_i if options[:factor]
|
|
33
|
-
send "#{name}=", number
|
|
34
|
-
val
|
|
35
|
-
rescue ArgumentError
|
|
36
|
-
val
|
|
37
|
-
end
|
|
23
|
+
write_d12n_attribute name, val, options
|
|
38
24
|
end
|
|
39
25
|
end
|
|
40
26
|
end
|
|
@@ -42,5 +28,27 @@ module D12n
|
|
|
42
28
|
def self.included(base)
|
|
43
29
|
base.extend ClassMethods
|
|
44
30
|
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def read_d12n_attribute(name, options = {})
|
|
35
|
+
localized = instance_variable_get "@#{options[:prefix]}_#{name}"
|
|
36
|
+
return localized if localized
|
|
37
|
+
number = send(name)
|
|
38
|
+
# If a factor is defined, internal presentation is an integer multiple of the local value
|
|
39
|
+
number /= options[:factor].to_f if options[:factor]
|
|
40
|
+
D12n.strategy.bigdecimal_to_formatted(number)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def write_d12n_attribute(name, val, options = {})
|
|
44
|
+
instance_variable_set "@#{options[:prefix]}_#{name}", val
|
|
45
|
+
number = D12n.strategy.formatted_to_bigdecimal(val)
|
|
46
|
+
# If a factor is defined, internal presentation is an integer multiple of the local value
|
|
47
|
+
number = (number * options[:factor].to_f).to_i if options[:factor]
|
|
48
|
+
send "#{name}=", number
|
|
49
|
+
val
|
|
50
|
+
rescue ArgumentError
|
|
51
|
+
val
|
|
52
|
+
end
|
|
45
53
|
end
|
|
46
54
|
end
|
metadata
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: d12n
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
|
-
-
|
|
7
|
+
- Konstantin Munteanu
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
@@ -16,7 +16,7 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - ">="
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version:
|
|
19
|
+
version: '4.2'
|
|
20
20
|
- - "<"
|
|
21
21
|
- !ruby/object:Gem::Version
|
|
22
22
|
version: '6'
|
|
@@ -26,7 +26,7 @@ dependencies:
|
|
|
26
26
|
requirements:
|
|
27
27
|
- - ">="
|
|
28
28
|
- !ruby/object:Gem::Version
|
|
29
|
-
version:
|
|
29
|
+
version: '4.2'
|
|
30
30
|
- - "<"
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
32
|
version: '6'
|
|
@@ -65,12 +65,13 @@ executables: []
|
|
|
65
65
|
extensions: []
|
|
66
66
|
extra_rdoc_files: []
|
|
67
67
|
files:
|
|
68
|
+
- LICENSE
|
|
68
69
|
- README.md
|
|
69
70
|
- lib/d12n.rb
|
|
70
71
|
- lib/d12n/model_support.rb
|
|
71
72
|
- lib/d12n/strategy/decimal_comma.rb
|
|
72
73
|
- lib/d12n/strategy/decimal_point.rb
|
|
73
|
-
homepage: https://github.com/mkon
|
|
74
|
+
homepage: https://github.com/mkon/d12n
|
|
74
75
|
licenses:
|
|
75
76
|
- MIT
|
|
76
77
|
metadata: {}
|