d12n 0.0.3 → 0.0.4
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/README.md +21 -1
- data/lib/d12n.rb +1 -0
- data/lib/d12n/model_support.rb +17 -11
- data/lib/d12n/strategy/decimal_comma.rb +5 -1
- data/lib/d12n/strategy/decimal_point.rb +5 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fab51c4115aeaa16486a5319ea6cdb9f63eef5f
|
4
|
+
data.tar.gz: b98690bb15e012314d65b65594e9ca1f8e8e006c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 359787933d7c1c15f9a6c41e905a1a28808d3bc8ae29364f7cba35080c7f0614ea351ea5c04470cc4f49faedd0f45a50e0f02ceb9f542e88d6703fb525baa9eb
|
7
|
+
data.tar.gz: ee41ec45117aeb4e02d698aa08f0b901b4c025e1c53051f3bacc62c8c7fc2399d061557e97b2780f6358b1eab35d20e5200bc00940b74708fefaf2cde1ad323a
|
data/README.md
CHANGED
@@ -17,7 +17,7 @@ class Dummy
|
|
17
17
|
include D12n::ModelSupport
|
18
18
|
|
19
19
|
attr_accessor :amount
|
20
|
-
d12n_attribute :amount
|
20
|
+
d12n_attribute :amount, factor: 100
|
21
21
|
end
|
22
22
|
```
|
23
23
|
|
@@ -49,3 +49,23 @@ d12n_attribute :amount, prefix: 'localized'
|
|
49
49
|
```
|
50
50
|
|
51
51
|
Would give you `localized_amount` instead of `local_amount`.
|
52
|
+
|
53
|
+
### Internal integer representation with a factor
|
54
|
+
|
55
|
+
If your internal representation is for example in cents, but the local format should be EUR with decimal point
|
56
|
+
you can use this option:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
d12n_attribute :amount, factor: 100
|
60
|
+
```
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
d = Dummy.new
|
64
|
+
|
65
|
+
d.amount = 1_234
|
66
|
+
d.local_amount # "12,34"
|
67
|
+
|
68
|
+
d.local_amount = '3,456.78'
|
69
|
+
d.amount.class # Integer
|
70
|
+
d.amount # 345_678
|
71
|
+
```
|
data/lib/d12n.rb
CHANGED
data/lib/d12n/model_support.rb
CHANGED
@@ -3,28 +3,34 @@ module D12n
|
|
3
3
|
module ClassMethods
|
4
4
|
def d12n_attribute(*args)
|
5
5
|
options = args.extract_options!
|
6
|
-
prefix
|
6
|
+
options.reverse_merge! prefix: 'local'
|
7
7
|
args.each do |name|
|
8
|
-
define_reader name,
|
9
|
-
define_writer name,
|
8
|
+
define_reader name, options
|
9
|
+
define_writer name, options
|
10
10
|
end
|
11
11
|
end
|
12
12
|
|
13
13
|
private
|
14
14
|
|
15
|
-
def define_reader(name,
|
16
|
-
define_method "#{prefix}_#{name}" do
|
17
|
-
local = instance_variable_get "@#{prefix}_#{name}"
|
15
|
+
def define_reader(name, options)
|
16
|
+
define_method "#{options[:prefix]}_#{name}" do
|
17
|
+
local = instance_variable_get "@#{options[:prefix]}_#{name}"
|
18
18
|
return local if local
|
19
|
-
|
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)
|
20
23
|
end
|
21
24
|
end
|
22
25
|
|
23
|
-
def define_writer(name,
|
24
|
-
define_method "#{prefix}_#{name}=" do |val|
|
26
|
+
def define_writer(name, options)
|
27
|
+
define_method "#{options[:prefix]}_#{name}=" do |val|
|
25
28
|
begin
|
26
|
-
instance_variable_set "@#{prefix}_#{name}", val
|
27
|
-
|
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
|
28
34
|
val
|
29
35
|
rescue ArgumentError
|
30
36
|
val
|
@@ -6,9 +6,13 @@ module D12n
|
|
6
6
|
extend self
|
7
7
|
|
8
8
|
# must raise ArgumentError when format is invalid
|
9
|
-
def
|
9
|
+
def formatted_to_bigdecimal(formatted)
|
10
10
|
BigDecimal.new formatted.tr('.', '').tr(',', '.')
|
11
11
|
end
|
12
|
+
|
13
|
+
def bigdecimal_to_formatted(internal)
|
14
|
+
ActiveSupport::NumberHelper::NumberToDelimitedConverter.convert(internal, {})
|
15
|
+
end
|
12
16
|
end
|
13
17
|
end
|
14
18
|
end
|
@@ -6,9 +6,13 @@ module D12n
|
|
6
6
|
extend self
|
7
7
|
|
8
8
|
# must raise ArgumentError when format is invalid
|
9
|
-
def
|
9
|
+
def formatted_to_bigdecimal(formatted)
|
10
10
|
BigDecimal.new formatted.tr(',', '')
|
11
11
|
end
|
12
|
+
|
13
|
+
def bigdecimal_to_formatted(internal)
|
14
|
+
ActiveSupport::NumberHelper::NumberToDelimitedConverter.convert(internal, {})
|
15
|
+
end
|
12
16
|
end
|
13
17
|
end
|
14
18
|
end
|