arel_extensions 1.2.23 → 2.0.0.rc3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -7
- data/.travis.yml +91 -61
- data/Gemfile +20 -15
- data/README.md +12 -17
- data/Rakefile +29 -40
- data/appveyor.yml +1 -1
- data/arel_extensions.gemspec +3 -3
- data/functions.html +3 -3
- data/gemfiles/rails3.gemfile +9 -9
- data/gemfiles/rails4.gemfile +13 -13
- data/gemfiles/rails5_0.gemfile +13 -13
- data/gemfiles/rails5_1_4.gemfile +13 -13
- data/gemfiles/rails5_2.gemfile +13 -13
- data/init/mssql.sql +4 -4
- data/init/mysql.sql +38 -38
- data/init/postgresql.sql +21 -21
- data/lib/arel_extensions.rb +19 -69
- data/lib/arel_extensions/attributes.rb +1 -0
- data/lib/arel_extensions/boolean_functions.rb +14 -55
- data/lib/arel_extensions/common_sql_functions.rb +8 -7
- data/lib/arel_extensions/comparators.rb +15 -14
- data/lib/arel_extensions/date_duration.rb +5 -4
- data/lib/arel_extensions/insert_manager.rb +16 -17
- data/lib/arel_extensions/math.rb +12 -11
- data/lib/arel_extensions/math_functions.rb +22 -29
- data/lib/arel_extensions/nodes.rb +1 -1
- data/lib/arel_extensions/nodes/abs.rb +1 -0
- data/lib/arel_extensions/nodes/blank.rb +1 -0
- data/lib/arel_extensions/nodes/case.rb +8 -11
- data/lib/arel_extensions/nodes/cast.rb +2 -4
- data/lib/arel_extensions/nodes/ceil.rb +1 -1
- data/lib/arel_extensions/nodes/change_case.rb +0 -0
- data/lib/arel_extensions/nodes/coalesce.rb +3 -2
- data/lib/arel_extensions/nodes/collate.rb +2 -1
- data/lib/arel_extensions/nodes/concat.rb +16 -7
- data/lib/arel_extensions/nodes/date_diff.rb +13 -10
- data/lib/arel_extensions/nodes/duration.rb +3 -0
- data/lib/arel_extensions/nodes/find_in_set.rb +1 -0
- data/lib/arel_extensions/nodes/floor.rb +1 -1
- data/lib/arel_extensions/nodes/format.rb +8 -34
- data/lib/arel_extensions/nodes/formatted_number.rb +23 -22
- data/lib/arel_extensions/nodes/function.rb +16 -25
- data/lib/arel_extensions/nodes/json.rb +36 -43
- data/lib/arel_extensions/nodes/length.rb +1 -0
- data/lib/arel_extensions/nodes/levenshtein_distance.rb +0 -0
- data/lib/arel_extensions/nodes/locate.rb +1 -0
- data/lib/arel_extensions/nodes/log10.rb +2 -1
- data/lib/arel_extensions/nodes/matches.rb +6 -4
- data/lib/arel_extensions/nodes/md5.rb +1 -0
- data/lib/arel_extensions/nodes/power.rb +5 -5
- data/lib/arel_extensions/nodes/rand.rb +1 -0
- data/lib/arel_extensions/nodes/repeat.rb +4 -2
- data/lib/arel_extensions/nodes/replace.rb +6 -22
- data/lib/arel_extensions/nodes/round.rb +6 -5
- data/lib/arel_extensions/nodes/soundex.rb +15 -15
- data/lib/arel_extensions/nodes/std.rb +21 -18
- data/lib/arel_extensions/nodes/substring.rb +16 -8
- data/lib/arel_extensions/nodes/then.rb +0 -0
- data/lib/arel_extensions/nodes/trim.rb +5 -3
- data/lib/arel_extensions/nodes/union.rb +5 -2
- data/lib/arel_extensions/nodes/union_all.rb +3 -0
- data/lib/arel_extensions/nodes/wday.rb +4 -0
- data/lib/arel_extensions/null_functions.rb +7 -5
- data/lib/arel_extensions/predications.rb +34 -35
- data/lib/arel_extensions/railtie.rb +5 -5
- data/lib/arel_extensions/set_functions.rb +4 -2
- data/lib/arel_extensions/string_functions.rb +22 -43
- data/lib/arel_extensions/tasks.rb +5 -5
- data/lib/arel_extensions/version.rb +1 -1
- data/lib/arel_extensions/visitors.rb +60 -68
- data/lib/arel_extensions/visitors/ibm_db.rb +12 -5
- data/lib/arel_extensions/visitors/mssql.rb +57 -63
- data/lib/arel_extensions/visitors/mysql.rb +98 -149
- data/lib/arel_extensions/visitors/oracle.rb +68 -71
- data/lib/arel_extensions/visitors/oracle12.rb +15 -2
- data/lib/arel_extensions/visitors/postgresql.rb +63 -116
- data/lib/arel_extensions/visitors/sqlite.rb +70 -83
- data/lib/arel_extensions/visitors/to_sql.rb +109 -141
- data/test/database.yml +0 -2
- data/test/helper.rb +18 -0
- data/test/real_db_test.rb +43 -28
- data/test/support/fake_record.rb +2 -2
- data/test/test_comparators.rb +12 -9
- data/test/visitors/test_bulk_insert_oracle.rb +8 -8
- data/test/visitors/test_bulk_insert_sqlite.rb +10 -9
- data/test/visitors/test_bulk_insert_to_sql.rb +10 -8
- data/test/visitors/test_oracle.rb +42 -42
- data/test/visitors/test_to_sql.rb +196 -361
- data/test/with_ar/all_agnostic_test.rb +160 -195
- data/test/with_ar/insert_agnostic_test.rb +4 -3
- data/test/with_ar/test_bulk_sqlite.rb +9 -6
- data/test/with_ar/test_math_sqlite.rb +12 -8
- data/test/with_ar/test_string_mysql.rb +11 -5
- data/test/with_ar/test_string_sqlite.rb +12 -4
- metadata +11 -22
- data/.github/workflows/ruby.yml +0 -102
- data/gemfiles/rails6.gemfile +0 -30
- data/gemfiles/rails6_1.gemfile +0 -30
- data/gemspecs/arel_extensions-v1.gemspec +0 -28
- data/gemspecs/arel_extensions-v2.gemspec +0 -28
- data/generate_gems.sh +0 -15
- data/lib/arel_extensions/nodes/aggregate_function.rb +0 -13
- data/lib/arel_extensions/nodes/sum.rb +0 -7
- data/lib/arel_extensions/visitors/convert_format.rb +0 -37
- data/test/arelx_test_helper.rb +0 -26
- data/version_v1.rb +0 -3
- data/version_v2.rb +0 -3
@@ -1,15 +1,14 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
1
|
require 'arel_extensions/nodes/then'
|
4
2
|
|
5
3
|
module ArelExtensions
|
6
4
|
module BooleanFunctions
|
5
|
+
|
7
6
|
def ⋀(other)
|
8
7
|
self.and(other)
|
9
8
|
end
|
10
9
|
|
11
10
|
def and *others
|
12
|
-
Arel::Nodes::And.new
|
11
|
+
Arel::Nodes::And.new([self]+ others.flatten)
|
13
12
|
end
|
14
13
|
|
15
14
|
def ⋁(other)
|
@@ -17,7 +16,12 @@ module ArelExtensions
|
|
17
16
|
end
|
18
17
|
|
19
18
|
def or *others
|
20
|
-
|
19
|
+
args = others.flatten
|
20
|
+
if args.length == 1
|
21
|
+
Arel::Nodes::Or.new(self, args.first)
|
22
|
+
else
|
23
|
+
ArelExtensions::Nodes::Or.new([self]+ args)
|
24
|
+
end
|
21
25
|
end
|
22
26
|
|
23
27
|
def then(t, f = nil)
|
@@ -26,61 +30,16 @@ module ArelExtensions
|
|
26
30
|
end
|
27
31
|
end
|
28
32
|
|
29
|
-
|
33
|
+
Arel::Nodes::And.class_eval do
|
30
34
|
include ArelExtensions::BooleanFunctions
|
31
|
-
|
32
|
-
def self.new *children
|
33
|
-
children =
|
34
|
-
children.flatten.map { |c|
|
35
|
-
c.is_a?(self) ? c.children : c
|
36
|
-
}.flatten
|
37
|
-
super(children)
|
38
|
-
end
|
39
35
|
end
|
40
36
|
|
41
|
-
|
42
|
-
# stores @children, and hashes it all). However Arel's Or is defined
|
43
|
-
# as binary, with only @left and @right, and hashing only @left and @right.
|
44
|
-
#
|
45
|
-
# So reimplement its ctor and accessors.
|
46
|
-
|
47
|
-
class Arel::Nodes::Or
|
37
|
+
Arel::Nodes::Or.class_eval do
|
48
38
|
include ArelExtensions::BooleanFunctions
|
39
|
+
end
|
49
40
|
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
children =
|
54
|
-
children.flatten.map { |c|
|
55
|
-
c.is_a?(self) ? c.children : c
|
56
|
-
}.flatten
|
57
|
-
super(*children)
|
58
|
-
end
|
59
|
-
|
60
|
-
def initialize *children
|
61
|
-
@children = children
|
62
|
-
end
|
63
|
-
|
64
|
-
def initialize_copy(other)
|
65
|
-
super
|
66
|
-
@children = other.children.copy if other.children
|
67
|
-
end
|
68
|
-
|
69
|
-
def left
|
70
|
-
children.first
|
71
|
-
end
|
72
|
-
|
73
|
-
def right
|
74
|
-
children[1]
|
75
|
-
end
|
41
|
+
ArelExtensions::Nodes.const_set('Or',Class.new(Arel::Nodes::And)).class_eval do
|
42
|
+
include ArelExtensions::BooleanFunctions
|
43
|
+
end
|
76
44
|
|
77
|
-
def hash
|
78
|
-
children.hash
|
79
|
-
end
|
80
45
|
|
81
|
-
def eql?(other)
|
82
|
-
self.class == other.class &&
|
83
|
-
children == other.children
|
84
|
-
end
|
85
|
-
alias :== :eql?
|
86
|
-
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
module ArelExtensions
|
2
2
|
class CommonSqlFunctions
|
3
|
+
|
3
4
|
def initialize(cnx)
|
4
5
|
@cnx = cnx
|
5
6
|
if cnx && cnx.adapter_name =~ /sqlite/i && !$load_extension_disabled
|
@@ -11,7 +12,7 @@ module ArelExtensions
|
|
11
12
|
db.enable_load_extension(0)
|
12
13
|
rescue => e
|
13
14
|
$load_extension_disabled = true
|
14
|
-
puts "
|
15
|
+
puts "can not load extensions #{e.inspect}"
|
15
16
|
end
|
16
17
|
end
|
17
18
|
end
|
@@ -38,12 +39,11 @@ module ArelExtensions
|
|
38
39
|
|
39
40
|
def add_sql_functions(env_db = nil)
|
40
41
|
env_db ||= @cnx.adapter_name
|
41
|
-
env_db = 'mysql' if env_db =~ /mysql/i
|
42
42
|
if env_db =~ /sqlite/i
|
43
43
|
begin
|
44
44
|
add_sqlite_functions
|
45
45
|
rescue => e
|
46
|
-
puts "
|
46
|
+
puts "can not add sqlite functions #{e.inspect}"
|
47
47
|
end
|
48
48
|
end
|
49
49
|
if File.exist?("init/#{env_db}.sql")
|
@@ -52,14 +52,15 @@ module ArelExtensions
|
|
52
52
|
sql.split(/^GO\s*$/).each {|str|
|
53
53
|
@cnx.execute(str.strip) unless str.blank?
|
54
54
|
}
|
55
|
-
elsif env_db =='mysql'
|
56
|
-
|
57
|
-
|
58
|
-
|
55
|
+
elsif env_db == 'mysql'
|
56
|
+
sql.split("$$")[1..-2].each { |str|
|
57
|
+
@cnx.execute(str.strip) unless str.strip.blank?
|
58
|
+
}
|
59
59
|
else
|
60
60
|
@cnx.execute(sql) unless sql.blank?
|
61
61
|
end
|
62
62
|
end
|
63
63
|
end
|
64
|
+
|
64
65
|
end
|
65
66
|
end
|
@@ -1,5 +1,6 @@
|
|
1
1
|
module ArelExtensions
|
2
2
|
module Comparators
|
3
|
+
|
3
4
|
def >(other)
|
4
5
|
Arel::Nodes::GreaterThan.new self, Arel::Nodes.build_quoted(other, self)
|
5
6
|
end
|
@@ -16,34 +17,33 @@ module ArelExtensions
|
|
16
17
|
Arel::Nodes::LessThanOrEqual.new self, Arel::Nodes.build_quoted(other, self)
|
17
18
|
end
|
18
19
|
|
19
|
-
|
20
|
-
#
|
20
|
+
|
21
|
+
#REGEXP function
|
22
|
+
#Pattern matching using regular expressions
|
21
23
|
def =~(other)
|
22
|
-
|
23
|
-
|
24
|
+
# arg = self.relation.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s].type
|
25
|
+
# if arg == :string || arg == :text
|
24
26
|
Arel::Nodes::Regexp.new self, convert_regexp(other)
|
25
|
-
|
27
|
+
# end
|
26
28
|
end
|
27
29
|
|
28
|
-
#
|
29
|
-
#
|
30
|
+
#NOT_REGEXP function
|
31
|
+
#Negation of Regexp
|
30
32
|
def !~(other)
|
31
|
-
|
32
|
-
|
33
|
+
# arg = self.relation.engine.connection.schema_cache.columns_hash(self.relation.table_name)[self.name.to_s].type
|
34
|
+
# if arg == :string || arg == :text
|
33
35
|
Arel::Nodes::NotRegexp.new self, convert_regexp(other)
|
34
|
-
|
36
|
+
# end
|
35
37
|
end
|
36
38
|
|
37
39
|
private
|
38
|
-
|
39
|
-
# Function used for not_regexp.
|
40
|
+
#Function use for not_regexp
|
40
41
|
def convert_regexp(other)
|
41
42
|
case other
|
42
43
|
when String
|
43
|
-
#
|
44
|
+
#Do nothing
|
44
45
|
when Regexp
|
45
46
|
other = other.source.gsub('\A','^')
|
46
|
-
other.gsub!('\z','$')
|
47
47
|
other.gsub!('\Z','$')
|
48
48
|
other.gsub!('\d','[0-9]')
|
49
49
|
other.gsub!('\D','[^0-9]')
|
@@ -54,5 +54,6 @@ module ArelExtensions
|
|
54
54
|
end
|
55
55
|
Arel::Nodes.build_quoted(other, self)
|
56
56
|
end
|
57
|
+
|
57
58
|
end
|
58
59
|
end
|
@@ -4,22 +4,22 @@ require 'arel_extensions/nodes/wday'
|
|
4
4
|
|
5
5
|
module ArelExtensions
|
6
6
|
module DateDuration
|
7
|
-
#
|
7
|
+
#function returns the year (as a number) given a date value.
|
8
8
|
def year
|
9
9
|
ArelExtensions::Nodes::Duration.new "y", self
|
10
10
|
end
|
11
11
|
|
12
|
-
|
12
|
+
#function returns the month (as a number) given a date value.
|
13
13
|
def month
|
14
14
|
ArelExtensions::Nodes::Duration.new "m", self
|
15
15
|
end
|
16
16
|
|
17
|
-
#
|
17
|
+
#function returns the week (as a number) given a date value.
|
18
18
|
def week
|
19
19
|
ArelExtensions::Nodes::Duration.new "w", self
|
20
20
|
end
|
21
21
|
|
22
|
-
#
|
22
|
+
#function returns the month (as a number) given a date value.
|
23
23
|
def day
|
24
24
|
ArelExtensions::Nodes::Duration.new "d", self
|
25
25
|
end
|
@@ -43,5 +43,6 @@ module ArelExtensions
|
|
43
43
|
def format(tpl)
|
44
44
|
ArelExtensions::Nodes::Format.new [self, tpl]
|
45
45
|
end
|
46
|
+
|
46
47
|
end
|
47
48
|
end
|
@@ -2,33 +2,32 @@ require 'arel'
|
|
2
2
|
|
3
3
|
module ArelExtensions
|
4
4
|
module InsertManager
|
5
|
+
|
5
6
|
def bulk_insert(cols, data)
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
7
|
+
case cols.first
|
8
|
+
when String, Symbol
|
9
|
+
cols.each { |c|
|
10
|
+
@ast.columns << @ast.relation[c]
|
11
|
+
}
|
12
|
+
when Array
|
13
|
+
if String === cols.first.first
|
14
|
+
@ast.columns = cols.map {|c| [@ast.relation[c.first]] }
|
15
|
+
elsif Arel::Attributes::Attribute == cols.first.first
|
16
|
+
@ast.columns = cols
|
17
|
+
end
|
18
|
+
when NilClass
|
19
|
+
@ast.columns = @ast.relation.columns
|
17
20
|
end
|
18
|
-
|
19
|
-
res_columns = @ast.relation.columns
|
20
|
-
end
|
21
|
-
self.values = BulkValues.new(res_columns, data)
|
22
|
-
@ast.columns = res_columns
|
21
|
+
self.values = BulkValues.new(@ast.columns, data)
|
23
22
|
end
|
24
23
|
|
25
24
|
class BulkValues < Arel::Nodes::Node
|
26
25
|
attr_accessor :left, :cols
|
27
|
-
|
28
26
|
def initialize(cols, values)
|
29
27
|
@left = values
|
30
28
|
@cols = cols
|
31
29
|
end
|
32
30
|
end
|
31
|
+
|
33
32
|
end
|
34
33
|
end
|
data/lib/arel_extensions/math.rb
CHANGED
@@ -11,9 +11,9 @@ require 'arel_extensions/nodes/union_all'
|
|
11
11
|
|
12
12
|
module ArelExtensions
|
13
13
|
module Math
|
14
|
-
#
|
15
|
-
#
|
16
|
-
#
|
14
|
+
#function + between
|
15
|
+
#String and others (convert in string) allows you to concatenate 2 or more strings together.
|
16
|
+
#Date and integer adds or subtracts a specified time interval from a date.
|
17
17
|
def +(other)
|
18
18
|
case self
|
19
19
|
when Arel::Nodes::Quoted
|
@@ -24,7 +24,7 @@ module ArelExtensions
|
|
24
24
|
else
|
25
25
|
return Arel::Nodes::Grouping.new(Arel::Nodes::Addition.new self, other)
|
26
26
|
end
|
27
|
-
when
|
27
|
+
when ArelExtensions::Nodes::Function,ArelExtensions::Nodes::Case
|
28
28
|
return case self.return_type
|
29
29
|
when :string, :text
|
30
30
|
self.concat(other)
|
@@ -35,7 +35,7 @@ module ArelExtensions
|
|
35
35
|
else
|
36
36
|
self.concat(other)
|
37
37
|
end
|
38
|
-
when
|
38
|
+
when Arel::Nodes::Function
|
39
39
|
Arel::Nodes::Grouping.new(Arel::Nodes::Addition.new self, other)
|
40
40
|
else
|
41
41
|
begin
|
@@ -43,7 +43,7 @@ module ArelExtensions
|
|
43
43
|
rescue Exception
|
44
44
|
col = nil
|
45
45
|
end
|
46
|
-
if (!col) #
|
46
|
+
if (!col) #if the column doesn't exist in the database
|
47
47
|
Arel::Nodes::Grouping.new(Arel::Nodes::Addition.new(self, other))
|
48
48
|
else
|
49
49
|
arg = col.type
|
@@ -62,8 +62,8 @@ module ArelExtensions
|
|
62
62
|
end
|
63
63
|
end
|
64
64
|
|
65
|
-
#
|
66
|
-
#
|
65
|
+
#function returns the time between two dates
|
66
|
+
#function returns the substraction between two ints
|
67
67
|
def -(other)
|
68
68
|
case self
|
69
69
|
when Arel::Nodes::Grouping
|
@@ -91,7 +91,7 @@ module ArelExtensions
|
|
91
91
|
rescue Exception
|
92
92
|
col = nil
|
93
93
|
end
|
94
|
-
if (!col) #
|
94
|
+
if (!col) #if the column doesn't exist in the database
|
95
95
|
Arel::Nodes::Grouping.new(Arel::Nodes::Subtraction.new(self, other))
|
96
96
|
else
|
97
97
|
arg = col.type
|
@@ -103,7 +103,7 @@ module ArelExtensions
|
|
103
103
|
rescue Exception
|
104
104
|
col2 = nil
|
105
105
|
end
|
106
|
-
if (!col2) #
|
106
|
+
if (!col2) #if the column doesn't exist in the database
|
107
107
|
ArelExtensions::Nodes::DateSub.new [self, other]
|
108
108
|
else
|
109
109
|
arg2 = col2.type
|
@@ -115,7 +115,7 @@ module ArelExtensions
|
|
115
115
|
end
|
116
116
|
when Arel::Nodes::Node, DateTime, Time, String, Date
|
117
117
|
ArelExtensions::Nodes::DateDiff.new [self, other]
|
118
|
-
when
|
118
|
+
when ArelExtensions::Nodes::Duration, Integer
|
119
119
|
ArelExtensions::Nodes::DateSub.new [self, other]
|
120
120
|
else # ActiveSupport::Duration
|
121
121
|
ArelExtensions::Nodes::DateAdd.new [self, -other]
|
@@ -133,5 +133,6 @@ module ArelExtensions
|
|
133
133
|
end
|
134
134
|
end
|
135
135
|
end
|
136
|
+
|
136
137
|
end
|
137
138
|
end
|
@@ -7,10 +7,10 @@ require 'arel_extensions/nodes/formatted_number'
|
|
7
7
|
require 'arel_extensions/nodes/log10'
|
8
8
|
require 'arel_extensions/nodes/power'
|
9
9
|
require 'arel_extensions/nodes/std'
|
10
|
-
require 'arel_extensions/nodes/sum'
|
11
10
|
|
12
11
|
module ArelExtensions
|
13
12
|
module MathFunctions
|
13
|
+
|
14
14
|
# Arel does not handle Decimal literal properly
|
15
15
|
def * other
|
16
16
|
case other
|
@@ -46,7 +46,7 @@ module ArelExtensions
|
|
46
46
|
ArelExtensions::Nodes::Floor.new [self]
|
47
47
|
end
|
48
48
|
|
49
|
-
|
49
|
+
# function gives the base 10 log
|
50
50
|
def log10
|
51
51
|
ArelExtensions::Nodes::Log10.new [self]
|
52
52
|
end
|
@@ -62,29 +62,21 @@ module ArelExtensions
|
|
62
62
|
end
|
63
63
|
|
64
64
|
# Aggregate Functions
|
65
|
-
def std
|
66
|
-
ArelExtensions::Nodes::Std.new self,
|
67
|
-
end
|
68
|
-
|
69
|
-
def variance opts = {unbiased: true}
|
70
|
-
ArelExtensions::Nodes::Variance.new self, **opts
|
65
|
+
def std unbiased = true
|
66
|
+
ArelExtensions::Nodes::Std.new [self,unbiased]
|
71
67
|
end
|
72
68
|
|
73
|
-
def
|
74
|
-
|
75
|
-
Arel::Nodes::Sum.new [self]
|
76
|
-
else
|
77
|
-
ArelExtensions::Nodes::Sum.new self, **opts
|
78
|
-
end
|
69
|
+
def variance unbiased = true
|
70
|
+
ArelExtensions::Nodes::Variance.new [self,unbiased]
|
79
71
|
end
|
80
72
|
|
81
|
-
#
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
alias_method
|
73
|
+
#function that can be invoked to produce random numbers between 0 and 1
|
74
|
+
# def rand seed = nil
|
75
|
+
# ArelExtensions::Nodes::Rand.new [seed]
|
76
|
+
# end
|
77
|
+
alias_method :random, :rand
|
86
78
|
|
87
|
-
#
|
79
|
+
#function is used to round a numeric field to the number of decimals specified
|
88
80
|
def round precision = nil
|
89
81
|
if precision
|
90
82
|
ArelExtensions::Nodes::Round.new [self, precision]
|
@@ -94,19 +86,19 @@ module ArelExtensions
|
|
94
86
|
end
|
95
87
|
|
96
88
|
# function returning a number at a specific format
|
97
|
-
def format_number format_string, locale
|
89
|
+
def format_number format_string, locale=nil
|
98
90
|
begin
|
99
91
|
sprintf(format_string,0) # this line is to get the right error message if the format_string is not correct
|
100
92
|
m = /^(.*)%([ #+\-0]*)([1-9][0-9]+|[1-9]?)[.]?([0-9]*)([a-zA-Z])(.*)$/.match(format_string)
|
101
93
|
opts = {
|
102
|
-
prefix
|
103
|
-
flags
|
104
|
-
width
|
105
|
-
precision
|
106
|
-
type
|
107
|
-
suffix
|
108
|
-
locale
|
109
|
-
original_string
|
94
|
+
:prefix => m[1],
|
95
|
+
:flags => m[2].split(//).uniq.join,
|
96
|
+
:width => m[3].to_i,
|
97
|
+
:precision => m[4] != '' ? m[4].to_i : 6,
|
98
|
+
:type => m[5],
|
99
|
+
:suffix => m[6],
|
100
|
+
:locale => locale,
|
101
|
+
:original_string => format_string
|
110
102
|
}
|
111
103
|
# opts = {:locale => 'fr_FR', :type => "e"/"f"/"d", :prefix => "$ ", :suffix => " %", :flags => " +-#0", :width => 5, :precision => 6}
|
112
104
|
ArelExtensions::Nodes::FormattedNumber.new [self,opts]
|
@@ -114,5 +106,6 @@ module ArelExtensions
|
|
114
106
|
Arel::Nodes.build_quoted('Wrong Format')
|
115
107
|
end
|
116
108
|
end
|
109
|
+
|
117
110
|
end
|
118
111
|
end
|