magick_columns 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +93 -0
- data/lib/magick_columns.rb +56 -4
- data/lib/magick_columns/active_record.rb +56 -0
- data/lib/magick_columns/defaults.rb +9 -1
- data/lib/magick_columns/locale/en.yml +15 -0
- data/lib/magick_columns/locale/es.yml +13 -0
- data/lib/magick_columns/railtie.rb +9 -0
- data/lib/magick_columns/rules.rb +20 -0
- data/lib/magick_columns/tokenizer.rb +56 -0
- data/lib/magick_columns/version.rb +1 -1
- data/test/dummy/log/development.log +93 -0
- data/test/dummy/log/test.log +2143 -0
- data/test/{query_builder_test.rb → magick_columns_test.rb} +0 -14
- data/test/tokenizer_test.rb +47 -0
- metadata +16 -9
- data/README.rdoc +0 -45
- data/lib/magick_columns/magick_columns.rb +0 -80
@@ -5,20 +5,6 @@ class MagickColumnsTest < ActiveSupport::TestCase
|
|
5
5
|
assert_kind_of Module, MagickColumns
|
6
6
|
end
|
7
7
|
|
8
|
-
test 'extract magick query terms' do
|
9
|
-
terms = Person.send(:_extract_magick_query_terms, 'a b')
|
10
|
-
assert_equal [['a', 'b']], terms
|
11
|
-
|
12
|
-
terms = Person.send(:_extract_magick_query_terms, 'a or b')
|
13
|
-
assert_equal [['a'], ['b']], terms
|
14
|
-
|
15
|
-
terms = Person.send(:_extract_magick_query_terms, 'long_abc')
|
16
|
-
assert_equal [['long_abc']], terms
|
17
|
-
|
18
|
-
terms = Person.send(:_extract_magick_query_terms, ' ')
|
19
|
-
assert_equal [], terms
|
20
|
-
end
|
21
|
-
|
22
8
|
test 'map magick column operator' do
|
23
9
|
operator = Person.send(:_map_magick_column_operator, '=', 'PostgreSQL')
|
24
10
|
assert_equal '=', operator
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class TokenizerTest < ActiveSupport::TestCase
|
4
|
+
test 'extract terms' do
|
5
|
+
terms = MagickColumns::Tokenizer.new('a b').extract_terms
|
6
|
+
assert_equal [[{term: 'a'}, {term: 'b'}]], terms
|
7
|
+
|
8
|
+
terms = MagickColumns::Tokenizer.new('a or b').extract_terms
|
9
|
+
assert_equal [[{term: 'a'}], [{term: 'b'}]], terms
|
10
|
+
|
11
|
+
terms = MagickColumns::Tokenizer.new('long_abc').extract_terms
|
12
|
+
assert_equal [[{term: 'long_abc'}]], terms
|
13
|
+
|
14
|
+
terms = MagickColumns::Tokenizer.new(' ').extract_terms
|
15
|
+
assert_equal [], terms
|
16
|
+
end
|
17
|
+
|
18
|
+
test 'clean query' do
|
19
|
+
query = MagickColumns::Tokenizer.new('a b').clean_query
|
20
|
+
assert_equal 'a b', query
|
21
|
+
|
22
|
+
query = MagickColumns::Tokenizer.new('and a b').clean_query
|
23
|
+
assert_equal 'a b', query
|
24
|
+
|
25
|
+
query = MagickColumns::Tokenizer.new(' and and a b or or ').clean_query
|
26
|
+
assert_equal 'a b', query
|
27
|
+
end
|
28
|
+
|
29
|
+
test 'split in terms' do
|
30
|
+
terms = MagickColumns::Tokenizer.new.split_term_in_terms('a')
|
31
|
+
assert_equal [{term: 'a'}], terms
|
32
|
+
|
33
|
+
terms = MagickColumns::Tokenizer.new.split_term_in_terms('from a')
|
34
|
+
assert_equal [{term: 'a', operator: '>='}], terms
|
35
|
+
|
36
|
+
terms = MagickColumns::Tokenizer.new.split_term_in_terms('until a')
|
37
|
+
assert_equal [{term: 'a', operator: '<='}], terms
|
38
|
+
|
39
|
+
terms = MagickColumns::Tokenizer.new.split_term_in_terms('until today')
|
40
|
+
assert_equal [{term: Date.today.to_s(:db), operator: '<='}], terms
|
41
|
+
|
42
|
+
terms = MagickColumns::Tokenizer.new.split_term_in_terms('from a to b')
|
43
|
+
assert_equal(
|
44
|
+
[{term: 'a', operator: '>='}, {term: 'b', operator: '<='}], terms
|
45
|
+
)
|
46
|
+
end
|
47
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: magick_columns
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
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-03-
|
12
|
+
date: 2012-03-19 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -67,13 +67,18 @@ extensions: []
|
|
67
67
|
extra_rdoc_files: []
|
68
68
|
files:
|
69
69
|
- lib/tasks/magick_columns_tasks.rake
|
70
|
-
- lib/magick_columns/
|
70
|
+
- lib/magick_columns/railtie.rb
|
71
|
+
- lib/magick_columns/rules.rb
|
72
|
+
- lib/magick_columns/locale/es.yml
|
73
|
+
- lib/magick_columns/locale/en.yml
|
74
|
+
- lib/magick_columns/active_record.rb
|
71
75
|
- lib/magick_columns/defaults.rb
|
76
|
+
- lib/magick_columns/tokenizer.rb
|
72
77
|
- lib/magick_columns/version.rb
|
73
78
|
- lib/magick_columns.rb
|
74
79
|
- MIT-LICENSE
|
75
80
|
- Rakefile
|
76
|
-
- README.
|
81
|
+
- README.md
|
77
82
|
- test/dummy/Rakefile
|
78
83
|
- test/dummy/script/rails
|
79
84
|
- test/dummy/public/500.html
|
@@ -113,8 +118,9 @@ files:
|
|
113
118
|
- test/dummy/test/unit/article_test.rb
|
114
119
|
- test/dummy/test/fixtures/articles.yml
|
115
120
|
- test/dummy/test/fixtures/people.yml
|
121
|
+
- test/magick_columns_test.rb
|
116
122
|
- test/test_helper.rb
|
117
|
-
- test/
|
123
|
+
- test/tokenizer_test.rb
|
118
124
|
homepage: http://github.com/francocatena/magick_columns
|
119
125
|
licenses: []
|
120
126
|
post_install_message:
|
@@ -129,7 +135,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
129
135
|
version: '0'
|
130
136
|
segments:
|
131
137
|
- 0
|
132
|
-
hash:
|
138
|
+
hash: 165541859099290878
|
133
139
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
140
|
none: false
|
135
141
|
requirements:
|
@@ -138,10 +144,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
138
144
|
version: '0'
|
139
145
|
segments:
|
140
146
|
- 0
|
141
|
-
hash:
|
147
|
+
hash: 165541859099290878
|
142
148
|
requirements: []
|
143
149
|
rubyforge_project:
|
144
|
-
rubygems_version: 1.8.
|
150
|
+
rubygems_version: 1.8.19
|
145
151
|
signing_key:
|
146
152
|
specification_version: 3
|
147
153
|
summary: Build query conditions from simple strings
|
@@ -185,5 +191,6 @@ test_files:
|
|
185
191
|
- test/dummy/test/unit/article_test.rb
|
186
192
|
- test/dummy/test/fixtures/articles.yml
|
187
193
|
- test/dummy/test/fixtures/people.yml
|
194
|
+
- test/magick_columns_test.rb
|
188
195
|
- test/test_helper.rb
|
189
|
-
- test/
|
196
|
+
- test/tokenizer_test.rb
|
data/README.rdoc
DELETED
@@ -1,45 +0,0 @@
|
|
1
|
-
= MagickColumns
|
2
|
-
|
3
|
-
This gem extends ActiveRecord to provide queries built from *simple* strings
|
4
|
-
|
5
|
-
== Instalation
|
6
|
-
|
7
|
-
Add to your Gemfile:
|
8
|
-
|
9
|
-
gem 'magick_columns'
|
10
|
-
|
11
|
-
== Usage
|
12
|
-
|
13
|
-
When must declare `has_magick_columns` in your model:
|
14
|
-
|
15
|
-
class Person < ActiveRecord::Base
|
16
|
-
has_magick_columns name: :string, email: :email, birth: :date
|
17
|
-
end
|
18
|
-
|
19
|
-
And now you can do something like this:
|
20
|
-
|
21
|
-
people = Person.magick_search('anakin or luke')
|
22
|
-
|
23
|
-
The method returns a Relation, so you can apply any aditional method you want:
|
24
|
-
|
25
|
-
people.order('name').limit(5)
|
26
|
-
|
27
|
-
And of course you can "spy" the query with:
|
28
|
-
|
29
|
-
people.to_sql
|
30
|
-
|
31
|
-
== How to contribute
|
32
|
-
|
33
|
-
If you find what you might think is a bug:
|
34
|
-
|
35
|
-
1. Check the GitHub issue tracker to see if anyone else has had the same issue.
|
36
|
-
https://github.com/francocatena/magick_columns/issues/
|
37
|
-
2. If you do not see anything, create an issue with information on how to reproduce it.
|
38
|
-
|
39
|
-
If you want to contribute an enhancement or a fix:
|
40
|
-
|
41
|
-
1. Fork the project on GitHub.
|
42
|
-
https://github.com/francocatena/magick_columns/
|
43
|
-
2. Make your changes with tests.
|
44
|
-
3. Commit the changes without making changes to the Rakefile, VERSION, or any other files that are not related to your enhancement or fix
|
45
|
-
4. Send a pull request.
|
@@ -1,80 +0,0 @@
|
|
1
|
-
module MagickColumns
|
2
|
-
module Base
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
included do
|
6
|
-
end
|
7
|
-
|
8
|
-
module ClassMethods
|
9
|
-
def has_magick_columns(options = {})
|
10
|
-
@@_magick_columns ||= {}
|
11
|
-
@@_magick_columns[name] ||= []
|
12
|
-
|
13
|
-
options.each do |field, type|
|
14
|
-
column_options = _magick_column_options(type)
|
15
|
-
|
16
|
-
@@_magick_columns[name] << { field: field }.merge(column_options)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def magick_search(query)
|
21
|
-
or_queries = []
|
22
|
-
terms = {}
|
23
|
-
|
24
|
-
_extract_magick_query_terms(query).each_with_index do |or_term, i|
|
25
|
-
and_queries = []
|
26
|
-
|
27
|
-
or_term.each_with_index do |and_term, j|
|
28
|
-
mini_query = []
|
29
|
-
|
30
|
-
@@_magick_columns[name].each_with_index do |column, k|
|
31
|
-
if column[:condition].call(and_term)
|
32
|
-
operator = _map_magick_column_operator(column[:operator])
|
33
|
-
terms[:"t_#{i}_#{j}_#{k}"] = column[:mask] % { t: and_term }
|
34
|
-
|
35
|
-
mini_query << "#{column[:field]} #{operator} :t_#{i}_#{j}_#{k}"
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
and_queries << mini_query.join(' OR ')
|
40
|
-
end
|
41
|
-
|
42
|
-
or_queries << and_queries.map { |a_q| "(#{a_q})" }.join(' AND ')
|
43
|
-
end
|
44
|
-
|
45
|
-
where(or_queries.map { |o_q| "(#{o_q})" }.join(' OR '), terms)
|
46
|
-
end
|
47
|
-
|
48
|
-
private
|
49
|
-
|
50
|
-
def _magick_column_options(type)
|
51
|
-
type.kind_of?(Hash) ? type : MagicColumns::DEFAULTS[type.to_sym]
|
52
|
-
end
|
53
|
-
|
54
|
-
def _extract_magick_query_terms(query)
|
55
|
-
ands = Regexp.quote("#{I18n.t('magick_columns.and', default: 'and')}")
|
56
|
-
ors = Regexp.quote("#{I18n.t('magick_columns.or', default: 'or')}")
|
57
|
-
clean_query = query.strip
|
58
|
-
.gsub(%r{\A\s*(#{ands})\s+}, '')
|
59
|
-
.gsub(%r{\s+(#{ands})\s*\z}, '')
|
60
|
-
.gsub(%r{\A\s*(#{ors})\s+}, '')
|
61
|
-
.gsub(%r{\s+(#{ors})\s*\z}, '')
|
62
|
-
or_terms = []
|
63
|
-
|
64
|
-
clean_query.split(%r{\s+(#{ors})\s+}).each do |or_term|
|
65
|
-
or_terms << or_term.split(%r{\s+(#{ands})\s+|\s+}).reject do |t|
|
66
|
-
t =~ %r{\A(#{ands})\z} || t =~ %r{\A(#{ors})\z}
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
or_terms.reject(&:empty?)
|
71
|
-
end
|
72
|
-
|
73
|
-
def _map_magick_column_operator(operator, db = nil)
|
74
|
-
db ||= ActiveRecord::Base.connection.adapter_name
|
75
|
-
|
76
|
-
operator == :like ? (db == 'PostgreSQL' ? 'ILIKE' : 'LIKE') : operator
|
77
|
-
end
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|