set_builder 1.2.0.beta1 → 1.2.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/set_builder/modifier/verb.rb +1 -1
- data/lib/set_builder/modifiers/date_modifier.rb +26 -0
- data/lib/set_builder/modifiers/number_modifier.rb +21 -2
- data/lib/set_builder/modifiers/string_modifier.rb +20 -4
- data/lib/set_builder/version.rb +1 -1
- data/set_builder.gemspec +2 -0
- data/test/string_modifier_test.rb +19 -0
- data/test/support/fake_connection.rb +111 -0
- data/test/test_helper.rb +21 -3
- metadata +34 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad5a691ebe74c5868f2b6bd1ed58b6bfc1e3d5c3
|
4
|
+
data.tar.gz: 98ad1930d226f419016b39588013352fb50ffc80
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 50f80ac7bf9a588b0f60c1ac5a5836462b37aedebe25fb97b1509241cd8ba352cd9a463ad41af411b9fb405ee6531be5a42b579f6490d5590f1370490dc1f122
|
7
|
+
data.tar.gz: 5df7956306cc4a5063e0550ea78d20aa636b898f19557e4d455876b9af631aa7caeebc0e59ac6c54d0964a6ffe38862a09b6ccc22f680f313e70ac9117c41cbf
|
@@ -47,6 +47,32 @@ module SetBuilder
|
|
47
47
|
|
48
48
|
|
49
49
|
|
50
|
+
def build_arel_for(selector)
|
51
|
+
case operator
|
52
|
+
when :ever
|
53
|
+
selector.not_eq(nil)
|
54
|
+
when :before
|
55
|
+
selector.lt(get_date)
|
56
|
+
when :after
|
57
|
+
selector.gt(get_date)
|
58
|
+
when :on
|
59
|
+
selector.eq(get_date)
|
60
|
+
when :during_month
|
61
|
+
Arel::Nodes::Extract.new(selector, "month").eq(values[0].to_i)
|
62
|
+
when :during_year
|
63
|
+
Arel::Nodes::Extract.new(selector, "year").eq(values[0].to_i)
|
64
|
+
when :in_the_last
|
65
|
+
selector.gteq(get_date)
|
66
|
+
when :between
|
67
|
+
selector.between(
|
68
|
+
Date.parse(values[0]),
|
69
|
+
Date.parse(values[1])
|
70
|
+
)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
|
50
76
|
protected
|
51
77
|
|
52
78
|
|
@@ -33,16 +33,35 @@ module SetBuilder
|
|
33
33
|
|
34
34
|
|
35
35
|
|
36
|
+
def build_arel_for(selector)
|
37
|
+
case operator
|
38
|
+
when :is
|
39
|
+
selector.eq(value)
|
40
|
+
when :is_less_than
|
41
|
+
selector.lt(value)
|
42
|
+
when :is_greater_than
|
43
|
+
selector.gt(value)
|
44
|
+
when :is_between
|
45
|
+
selector.gteq(value).and(selector.lteq(value))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
|
36
51
|
private
|
37
52
|
|
38
53
|
|
39
54
|
|
40
|
-
def
|
55
|
+
def value
|
41
56
|
values[0]
|
42
57
|
end
|
43
58
|
|
59
|
+
def format_value
|
60
|
+
value
|
61
|
+
end
|
62
|
+
|
44
63
|
|
45
64
|
|
46
65
|
end
|
47
66
|
end
|
48
|
-
end
|
67
|
+
end
|
@@ -59,17 +59,33 @@ module SetBuilder
|
|
59
59
|
|
60
60
|
|
61
61
|
|
62
|
+
def build_arel_for(selector, operator=nil)
|
63
|
+
operator ||= self.operator
|
64
|
+
case operator
|
65
|
+
when :is
|
66
|
+
selector.eq(values[0])
|
67
|
+
when :is_not
|
68
|
+
selector.not_eq(values[0])
|
69
|
+
when :contains, :begins_with, :ends_with
|
70
|
+
selector.matches(get_like_value_for_operator)
|
71
|
+
when :does_not_contain, :does_not_begin_with, :does_not_end_with
|
72
|
+
selector.does_not_match(get_like_value_for_operator)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
|
62
78
|
private
|
63
79
|
|
64
80
|
|
65
81
|
|
66
82
|
def get_like_value_for_operator
|
67
83
|
case operator
|
68
|
-
when :contains
|
84
|
+
when :contains, :does_not_contain
|
69
85
|
"%#{values[0]}%"
|
70
|
-
when :begins_with
|
86
|
+
when :begins_with, :does_not_begin_with
|
71
87
|
"#{values[0]}%"
|
72
|
-
when :ends_with
|
88
|
+
when :ends_with, :does_not_end_with
|
73
89
|
"%#{values[0]}"
|
74
90
|
end
|
75
91
|
end
|
@@ -78,4 +94,4 @@ module SetBuilder
|
|
78
94
|
|
79
95
|
end
|
80
96
|
end
|
81
|
-
end
|
97
|
+
end
|
data/lib/set_builder/version.rb
CHANGED
data/set_builder.gemspec
CHANGED
@@ -19,7 +19,9 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_dependency "rails"
|
22
|
+
spec.add_dependency "arel"
|
22
23
|
spec.add_development_dependency "bundler", "~> 1.3"
|
23
24
|
spec.add_development_dependency "rake"
|
24
25
|
spec.add_development_dependency "turn"
|
26
|
+
spec.add_development_dependency "pry"
|
25
27
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class StringModifierTest < ActiveSupport::TestCase
|
4
|
+
include SetBuilder::Modifiers
|
5
|
+
|
6
|
+
attr_reader :table
|
7
|
+
|
8
|
+
setup do
|
9
|
+
@table = Arel::Table.new(:fruits)
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
test "#build_arel_for should generate the correct SQL" do
|
14
|
+
modifier = StringModifier.new({:does_not_contain => ["banana"]})
|
15
|
+
assert_equal "\"fruits\".\"name\" NOT LIKE '%banana%'", modifier.build_arel_for(table[:name]).to_sql
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
module Fake
|
2
|
+
class Column < Struct.new(:name, :type)
|
3
|
+
end
|
4
|
+
|
5
|
+
class Connection
|
6
|
+
attr_reader :tables
|
7
|
+
attr_accessor :visitor
|
8
|
+
|
9
|
+
def initialize(visitor = nil)
|
10
|
+
@tables = %w{ users photos developers products}
|
11
|
+
@columns = {
|
12
|
+
'users' => [
|
13
|
+
Column.new('id', :integer),
|
14
|
+
Column.new('name', :string),
|
15
|
+
Column.new('bool', :boolean),
|
16
|
+
Column.new('created_at', :date)
|
17
|
+
],
|
18
|
+
'products' => [
|
19
|
+
Column.new('id', :integer),
|
20
|
+
Column.new('price', :decimal)
|
21
|
+
]
|
22
|
+
}
|
23
|
+
@columns_hash = {
|
24
|
+
'users' => Hash[@columns['users'].map { |x| [x.name, x] }],
|
25
|
+
'products' => Hash[@columns['products'].map { |x| [x.name, x] }]
|
26
|
+
}
|
27
|
+
@primary_keys = {
|
28
|
+
'users' => 'id',
|
29
|
+
'products' => 'id'
|
30
|
+
}
|
31
|
+
@visitor = visitor
|
32
|
+
end
|
33
|
+
|
34
|
+
def columns_hash table_name
|
35
|
+
@columns_hash[table_name]
|
36
|
+
end
|
37
|
+
|
38
|
+
def primary_key name
|
39
|
+
@primary_keys[name.to_s]
|
40
|
+
end
|
41
|
+
|
42
|
+
def table_exists? name
|
43
|
+
@tables.include? name.to_s
|
44
|
+
end
|
45
|
+
|
46
|
+
def columns name, message = nil
|
47
|
+
@columns[name.to_s]
|
48
|
+
end
|
49
|
+
|
50
|
+
def quote_table_name name
|
51
|
+
"\"#{name.to_s}\""
|
52
|
+
end
|
53
|
+
|
54
|
+
def quote_column_name name
|
55
|
+
"\"#{name.to_s}\""
|
56
|
+
end
|
57
|
+
|
58
|
+
def schema_cache
|
59
|
+
self
|
60
|
+
end
|
61
|
+
|
62
|
+
def quote thing, column = nil
|
63
|
+
if column && column.type == :integer
|
64
|
+
return 'NULL' if thing.nil?
|
65
|
+
return thing.to_i
|
66
|
+
end
|
67
|
+
|
68
|
+
case thing
|
69
|
+
when true
|
70
|
+
"'t'"
|
71
|
+
when false
|
72
|
+
"'f'"
|
73
|
+
when nil
|
74
|
+
'NULL'
|
75
|
+
when Numeric
|
76
|
+
thing
|
77
|
+
else
|
78
|
+
"'#{thing}'"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class ConnectionPool
|
84
|
+
class Spec < Struct.new(:config)
|
85
|
+
end
|
86
|
+
|
87
|
+
attr_reader :spec, :connection
|
88
|
+
|
89
|
+
def initialize
|
90
|
+
@spec = Spec.new(:adapter => 'america')
|
91
|
+
@connection = Connection.new
|
92
|
+
@connection.visitor = Arel::Visitors::ToSql.new(connection)
|
93
|
+
end
|
94
|
+
|
95
|
+
def with_connection
|
96
|
+
yield connection
|
97
|
+
end
|
98
|
+
|
99
|
+
def table_exists? name
|
100
|
+
connection.tables.include? name.to_s
|
101
|
+
end
|
102
|
+
|
103
|
+
def columns_hash
|
104
|
+
connection.columns_hash
|
105
|
+
end
|
106
|
+
|
107
|
+
def schema_cache
|
108
|
+
connection
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
-
require
|
1
|
+
require "rubygems"
|
2
2
|
require "rails"
|
3
3
|
require "rails/test_help"
|
4
4
|
require "active_support/core_ext"
|
5
5
|
require "turn"
|
6
|
-
require
|
6
|
+
require "set_builder"
|
7
|
+
require "pry"
|
8
|
+
require "support/fake_connection"
|
7
9
|
|
8
10
|
|
9
11
|
|
@@ -55,4 +57,20 @@ class Friend
|
|
55
57
|
|
56
58
|
|
57
59
|
|
58
|
-
|
60
|
+
# Stubs so that Arel can SQL
|
61
|
+
|
62
|
+
attr_accessor :connection_pool
|
63
|
+
|
64
|
+
def initialize
|
65
|
+
@connection_pool = Fake::ConnectionPool.new
|
66
|
+
end
|
67
|
+
|
68
|
+
def connection
|
69
|
+
connection_pool.connection
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
Arel::Table.engine = Arel::Sql::Engine.new(Friend.new)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: set_builder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.0.
|
4
|
+
version: 1.2.0.beta2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bob Lail
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -24,6 +24,20 @@ dependencies:
|
|
24
24
|
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: arel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
27
41
|
- !ruby/object:Gem::Dependency
|
28
42
|
name: bundler
|
29
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,6 +80,20 @@ dependencies:
|
|
66
80
|
- - '>='
|
67
81
|
- !ruby/object:Gem::Version
|
68
82
|
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
69
97
|
description: A gem for describing constraints on data sets
|
70
98
|
email:
|
71
99
|
- bob.lailfamily@gmail.com
|
@@ -128,6 +156,8 @@ files:
|
|
128
156
|
- test/inflector_test.rb
|
129
157
|
- test/modifier_test.rb
|
130
158
|
- test/set_test.rb
|
159
|
+
- test/string_modifier_test.rb
|
160
|
+
- test/support/fake_connection.rb
|
131
161
|
- test/test_helper.rb
|
132
162
|
- test/trait_test.rb
|
133
163
|
- test/traits_test.rb
|
@@ -186,6 +216,8 @@ test_files:
|
|
186
216
|
- test/inflector_test.rb
|
187
217
|
- test/modifier_test.rb
|
188
218
|
- test/set_test.rb
|
219
|
+
- test/string_modifier_test.rb
|
220
|
+
- test/support/fake_connection.rb
|
189
221
|
- test/test_helper.rb
|
190
222
|
- test/trait_test.rb
|
191
223
|
- test/traits_test.rb
|