modalh 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +2 -0
- data/Gemfile.lock +7 -0
- data/VERSION +1 -1
- data/lib/h/h.rb +18 -13
- data/modalh.gemspec +8 -2
- data/test/helper.rb +4 -0
- data/test/test_modalh.rb +78 -2
- metadata +35 -3
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,11 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
+
activesupport (3.2.3)
|
5
|
+
i18n (~> 0.6)
|
6
|
+
multi_json (~> 1.0)
|
4
7
|
git (1.2.5)
|
8
|
+
i18n (0.6.0)
|
5
9
|
jeweler (1.8.3)
|
6
10
|
bundler (~> 1.0)
|
7
11
|
git (>= 1.2.5)
|
@@ -9,6 +13,7 @@ GEM
|
|
9
13
|
rdoc
|
10
14
|
json (1.6.6)
|
11
15
|
modalsupport (0.8.3)
|
16
|
+
multi_json (1.3.4)
|
12
17
|
rake (0.9.2.2)
|
13
18
|
rdoc (3.12)
|
14
19
|
json (~> 1.4)
|
@@ -24,7 +29,9 @@ PLATFORMS
|
|
24
29
|
ruby
|
25
30
|
|
26
31
|
DEPENDENCIES
|
32
|
+
activesupport
|
27
33
|
bundler (~> 1)
|
34
|
+
i18n
|
28
35
|
jeweler (~> 1.8.3)
|
29
36
|
rdoc (~> 3.12)
|
30
37
|
shoulda
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0.
|
1
|
+
1.0.5
|
data/lib/h/h.rb
CHANGED
@@ -37,7 +37,7 @@ module H
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def number_to(value, options={})
|
40
|
-
options =
|
40
|
+
options = number_format_options(options).except(:precision).merge(options)
|
41
41
|
precision = options[:precision]
|
42
42
|
|
43
43
|
return options[:blank] || '' if value.nil?
|
@@ -51,7 +51,7 @@ module H
|
|
51
51
|
else
|
52
52
|
value = value.to_i if precision==0
|
53
53
|
value = value.to_s
|
54
|
-
value = value[0...-2] if value.
|
54
|
+
value = value[0...-2] if value.end_with?('.0')
|
55
55
|
end
|
56
56
|
end
|
57
57
|
if options[:delimiter]
|
@@ -73,7 +73,7 @@ module H
|
|
73
73
|
end
|
74
74
|
|
75
75
|
def number_from(txt, options={})
|
76
|
-
options =
|
76
|
+
options = number_format_options(options).except(:precision).merge(options)
|
77
77
|
type = check_type(options[:type] || (options[:precision]==0 ? Integer : Float))
|
78
78
|
|
79
79
|
return nil if txt.to_s.strip.empty? || txt==options[:blank]
|
@@ -94,7 +94,7 @@ module H
|
|
94
94
|
end
|
95
95
|
|
96
96
|
def integer_to(value, options={})
|
97
|
-
options =
|
97
|
+
options = number_format_options(options).merge(options)
|
98
98
|
if value.nil?
|
99
99
|
options[:blank] || ''
|
100
100
|
else
|
@@ -120,7 +120,7 @@ module H
|
|
120
120
|
end
|
121
121
|
|
122
122
|
def date_from(txt, options={})
|
123
|
-
options =
|
123
|
+
options = number_format_options(options).merge(options)
|
124
124
|
type = check_type(options[:type] || Date)
|
125
125
|
|
126
126
|
return nil if txt.to_s.strip.empty? || txt==options[:blank]
|
@@ -155,12 +155,12 @@ module H
|
|
155
155
|
end
|
156
156
|
|
157
157
|
def logical_to(value, options={})
|
158
|
-
options =
|
158
|
+
options = logical_format_options(options).merge(options)
|
159
159
|
value.nil? ? options[:blank] : (value ? options[:true] : options[:false])
|
160
160
|
end
|
161
161
|
|
162
162
|
def logical_from(txt, options={})
|
163
|
-
options =
|
163
|
+
options = logical_format_options(options).merge(options)
|
164
164
|
txt = normalize_txt(txt)
|
165
165
|
trues = options[:trues]
|
166
166
|
trues ||= [normalize_txt(options[:true])]
|
@@ -174,6 +174,16 @@ module H
|
|
174
174
|
private
|
175
175
|
# include ActionView::Helpers::NumberHelper
|
176
176
|
|
177
|
+
def number_format_options(options)
|
178
|
+
opt = I18n.translate(:'number.format', :locale => options[:locale])
|
179
|
+
opt.kind_of?(Hash) ? opt : {:separator=>'.'}
|
180
|
+
end
|
181
|
+
|
182
|
+
def logical_format_options(options)
|
183
|
+
opt = I18n.translate(:'logical.format', :locale => options[:locale])
|
184
|
+
opt.kind_of?(Hash) ? opt : {:separator=>'.'}
|
185
|
+
end
|
186
|
+
|
177
187
|
def round(v, ndec)
|
178
188
|
return v if (v.respond_to?(:nan?) && v.nan?) || (v.respond_to?(:infinite?) && v.infinite?)
|
179
189
|
if ndec
|
@@ -190,12 +200,7 @@ module H
|
|
190
200
|
|
191
201
|
# pos0 first digit, pos1 one past last integral digit
|
192
202
|
def digit_grouping(txt,n,sep,pos0,pos1)
|
193
|
-
if sep
|
194
|
-
while pos1>pos0
|
195
|
-
pos1 -= n
|
196
|
-
txt.insert pos1, sep if pos1>pos0
|
197
|
-
end
|
198
|
-
end
|
203
|
+
txt[pos0...pos1] = txt[pos0...pos1].gsub(/(\d)(?=(#{'\\d'*n})+(?!\d))/, "\\1#{sep}") if sep
|
199
204
|
txt
|
200
205
|
end
|
201
206
|
|
data/modalh.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "modalh"
|
8
|
-
s.version = "1.0.
|
8
|
+
s.version = "1.0.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Javier Goizueta"]
|
12
|
-
s.date = "2012-
|
12
|
+
s.date = "2012-05-08"
|
13
13
|
s.description = "Rails plugin for localization & delocalization of data values"
|
14
14
|
s.email = "jgoizueta@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -49,12 +49,16 @@ Gem::Specification.new do |s|
|
|
49
49
|
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
50
50
|
s.add_development_dependency(%q<bundler>, ["~> 1"])
|
51
51
|
s.add_development_dependency(%q<jeweler>, ["~> 1.8.3"])
|
52
|
+
s.add_development_dependency(%q<activesupport>, [">= 0"])
|
53
|
+
s.add_development_dependency(%q<i18n>, [">= 0"])
|
52
54
|
else
|
53
55
|
s.add_dependency(%q<units-system>, [">= 0"])
|
54
56
|
s.add_dependency(%q<shoulda>, [">= 0"])
|
55
57
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
56
58
|
s.add_dependency(%q<bundler>, ["~> 1"])
|
57
59
|
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
60
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
61
|
+
s.add_dependency(%q<i18n>, [">= 0"])
|
58
62
|
end
|
59
63
|
else
|
60
64
|
s.add_dependency(%q<units-system>, [">= 0"])
|
@@ -62,6 +66,8 @@ Gem::Specification.new do |s|
|
|
62
66
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
63
67
|
s.add_dependency(%q<bundler>, ["~> 1"])
|
64
68
|
s.add_dependency(%q<jeweler>, ["~> 1.8.3"])
|
69
|
+
s.add_dependency(%q<activesupport>, [">= 0"])
|
70
|
+
s.add_dependency(%q<i18n>, [">= 0"])
|
65
71
|
end
|
66
72
|
end
|
67
73
|
|
data/test/helper.rb
CHANGED
@@ -10,6 +10,10 @@ end
|
|
10
10
|
require 'test/unit'
|
11
11
|
require 'shoulda'
|
12
12
|
|
13
|
+
require 'i18n'
|
14
|
+
require 'active_support'
|
15
|
+
require 'bigdecimal'
|
16
|
+
|
13
17
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
14
18
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
19
|
require 'modalh'
|
data/test/test_modalh.rb
CHANGED
@@ -1,7 +1,83 @@
|
|
1
1
|
require 'helper'
|
2
2
|
|
3
3
|
class TestModalh < Test::Unit::TestCase
|
4
|
-
should "
|
5
|
-
|
4
|
+
should "group digits with a delimiter" do
|
5
|
+
assert_equal "123.456", H.to(123.456, :delimiter=>',')
|
6
|
+
assert_equal "123", H.to(123.0, :delimiter=>',')
|
7
|
+
assert_equal "123", H.to(123, :delimiter=>',')
|
8
|
+
assert_equal "12.456", H.to(12.456, :delimiter=>',')
|
9
|
+
assert_equal "1.456", H.to(1.456, :delimiter=>',')
|
10
|
+
assert_equal "1", H.to(1, :delimiter=>',')
|
11
|
+
assert_equal "1", H.to(1.0, :delimiter=>',')
|
12
|
+
assert_equal "1,234.567", H.to(1234.567, :delimiter=>',')
|
13
|
+
assert_equal "41,234.567", H.to(41234.567, :delimiter=>',')
|
14
|
+
assert_equal "241,234.567", H.to(241234.567, :delimiter=>',')
|
15
|
+
assert_equal "3,241,234.567", H.to(3241234.567, :delimiter=>',')
|
16
|
+
assert_equal "3,241,234", H.to(3241234.0, :delimiter=>',')
|
17
|
+
assert_equal "3,241,234", H.to(3241234, :delimiter=>',')
|
18
|
+
assert_equal "-123.456", H.to(-123.456, :delimiter=>',')
|
19
|
+
assert_equal "-123", H.to(-123.0, :delimiter=>',')
|
20
|
+
assert_equal "-123", H.to(-123, :delimiter=>',')
|
21
|
+
assert_equal "-12.456", H.to(-12.456, :delimiter=>',')
|
22
|
+
assert_equal "-1.456", H.to(-1.456, :delimiter=>',')
|
23
|
+
assert_equal "-1", H.to(-1, :delimiter=>',')
|
24
|
+
assert_equal "-1", H.to(-1.0, :delimiter=>',')
|
25
|
+
assert_equal "-1,234.567", H.to(-1234.567, :delimiter=>',')
|
26
|
+
assert_equal "-41,234.567", H.to(-41234.567, :delimiter=>',')
|
27
|
+
assert_equal "-241,234.567", H.to(-241234.567, :delimiter=>',')
|
28
|
+
assert_equal "-3,241,234.567", H.to(-3241234.567, :delimiter=>',')
|
29
|
+
assert_equal "-3,241,234", H.to(-3241234.0, :delimiter=>',')
|
30
|
+
assert_equal "-3,241,234", H.to(-3241234, :delimiter=>',')
|
6
31
|
end
|
32
|
+
|
33
|
+
should "group digits with a delimiter and round" do
|
34
|
+
assert_equal "123.46", H.to(123.456, :delimiter=>',', :precision=>2)
|
35
|
+
assert_equal "123.00", H.to(123.0, :delimiter=>',', :precision=>2)
|
36
|
+
assert_equal "123.00", H.to(123, :delimiter=>',', :precision=>2)
|
37
|
+
assert_equal "12.46", H.to(12.456, :delimiter=>',', :precision=>2)
|
38
|
+
assert_equal "1.46", H.to(1.456, :delimiter=>',', :precision=>2)
|
39
|
+
assert_equal "1.00", H.to(1, :delimiter=>',', :precision=>2)
|
40
|
+
assert_equal "1.00", H.to(1.0, :delimiter=>',', :precision=>2)
|
41
|
+
assert_equal "1,234.57", H.to(1234.567, :delimiter=>',', :precision=>2)
|
42
|
+
assert_equal "41,234.57", H.to(41234.567, :delimiter=>',', :precision=>2)
|
43
|
+
assert_equal "241,234.57", H.to(241234.567, :delimiter=>',', :precision=>2)
|
44
|
+
assert_equal "3,241,234.57", H.to(3241234.567, :delimiter=>',', :precision=>2)
|
45
|
+
assert_equal "3,241,234.00", H.to(3241234.0, :delimiter=>',', :precision=>2)
|
46
|
+
assert_equal "3,241,234.00", H.to(3241234, :delimiter=>',', :precision=>2)
|
47
|
+
assert_equal "-123.46", H.to(-123.456, :delimiter=>',', :precision=>2)
|
48
|
+
assert_equal "-123.00", H.to(-123.0, :delimiter=>',', :precision=>2)
|
49
|
+
assert_equal "-123.00", H.to(-123, :delimiter=>',', :precision=>2)
|
50
|
+
assert_equal "-12.46", H.to(-12.456, :delimiter=>',', :precision=>2)
|
51
|
+
assert_equal "-1.46", H.to(-1.456, :delimiter=>',', :precision=>2)
|
52
|
+
assert_equal "-1.00", H.to(-1, :delimiter=>',', :precision=>2)
|
53
|
+
assert_equal "-1.00", H.to(-1.0, :delimiter=>',', :precision=>2)
|
54
|
+
assert_equal "-1,234.57", H.to(-1234.567, :delimiter=>',', :precision=>2)
|
55
|
+
assert_equal "-41,234.57", H.to(-41234.567, :delimiter=>',', :precision=>2)
|
56
|
+
assert_equal "-241,234.57", H.to(-241234.567, :delimiter=>',', :precision=>2)
|
57
|
+
assert_equal "-3,241,234.57", H.to(-3241234.567, :delimiter=>',', :precision=>2)
|
58
|
+
assert_equal "-3,241,234.00", H.to(-3241234.0, :delimiter=>',', :precision=>2)
|
59
|
+
assert_equal "-3,241,234.00", H.to(-3241234, :delimiter=>',', :precision=>2)
|
60
|
+
assert_equal "123.400", H.to(123.4, :delimiter=>',', :precision=>3)
|
61
|
+
end
|
62
|
+
|
63
|
+
should "parse numbers with delimiters" do
|
64
|
+
assert_equal 123.456, H.from("123.456", :type=>Float, :delimiter=>',')
|
65
|
+
assert_equal 123.0, H.from("123", :type=>Float, :delimiter=>',')
|
66
|
+
assert_equal 123.0, H.from("123.0", :type=>Float, :delimiter=>',')
|
67
|
+
assert_equal 123, H.from("123", :type=>Integer, :delimiter=>',')
|
68
|
+
assert_equal 123, H.from("123.0", :type=>Integer, :delimiter=>',')
|
69
|
+
assert_equal 12.456, H.from("12.456", :delimiter=>',')
|
70
|
+
assert_equal 1.456, H.from("1.456", :delimiter=>',')
|
71
|
+
assert_equal 1.0, H.from("1", :delimiter=>',')
|
72
|
+
assert_equal 1234.567, H.from("1,234.567", :delimiter=>',')
|
73
|
+
assert_equal 1234.567, H.from("1234.567", :delimiter=>',')
|
74
|
+
assert_equal 41234.567, H.from("41,234.567", :delimiter=>',')
|
75
|
+
assert_equal 241234.567, H.from("241,234.567", :delimiter=>',')
|
76
|
+
assert_equal -123.456, H.from("-123.456", :delimiter=>',')
|
77
|
+
assert_equal -123.0, H.from("-123.0", :delimiter=>',')
|
78
|
+
assert_equal -123.0, H.from("-123", :delimiter=>',')
|
79
|
+
assert_equal -12.456, H.from("-12.456", :delimiter=>',')
|
80
|
+
assert_equal -1234.567, H.from("-1,234.567", :delimiter=>',')
|
81
|
+
end
|
82
|
+
|
7
83
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: modalh
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-05-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: units-system
|
@@ -91,6 +91,38 @@ dependencies:
|
|
91
91
|
- - ~>
|
92
92
|
- !ruby/object:Gem::Version
|
93
93
|
version: 1.8.3
|
94
|
+
- !ruby/object:Gem::Dependency
|
95
|
+
name: activesupport
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: i18n
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
94
126
|
description: Rails plugin for localization & delocalization of data values
|
95
127
|
email: jgoizueta@gmail.com
|
96
128
|
executables: []
|
@@ -130,7 +162,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
130
162
|
version: '0'
|
131
163
|
segments:
|
132
164
|
- 0
|
133
|
-
hash: -
|
165
|
+
hash: -230677022647937802
|
134
166
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
167
|
none: false
|
136
168
|
requirements:
|