birddog 0.0.2 → 0.0.3
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.
- data/lib/birddog/date_expression.rb +3 -9
- data/lib/birddog/field_conditions.rb +5 -5
- data/lib/birddog/numeric_expression.rb +1 -9
- data/lib/birddog/version.rb +1 -1
- data/lib/birddog.rb +15 -4
- data/specs/complex_scope_spec.rb +8 -0
- data/specs/spec_helper.rb +1 -0
- data/specs/test.db +0 -0
- metadata +13 -13
@@ -1,24 +1,18 @@
|
|
1
1
|
module Birddog
|
2
2
|
|
3
3
|
class DateExpression
|
4
|
-
attr_reader :value
|
4
|
+
attr_reader :value
|
5
5
|
|
6
6
|
def initialize(value)
|
7
|
-
# allow additional spaces to be entered between conditions and value
|
8
7
|
value.gsub!(/\s/, '')
|
9
|
-
parts = value.scan(/(?:[=<>]+|(?:[0-9]{
|
8
|
+
parts = value.scan(/(?:[=<>]+|(?:[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}))/)[0,2]
|
10
9
|
@value = Date.parse(Chronic.parse(parts.last).to_s)
|
11
|
-
@condition = sanitize_condition(parts.first)
|
12
|
-
end
|
13
|
-
|
14
|
-
def sanitize_condition(cond)
|
15
|
-
valid = %w(= == > < <= >= <>)
|
16
|
-
valid.include?(cond) ? cond.strip : "="
|
17
10
|
end
|
18
11
|
|
19
12
|
def to_s
|
20
13
|
@value.to_s
|
21
14
|
end
|
15
|
+
|
22
16
|
end
|
23
17
|
|
24
18
|
end
|
@@ -28,16 +28,16 @@ module Birddog
|
|
28
28
|
[ "#{field_to_search} #{search_con} ", value_to_search ]
|
29
29
|
end
|
30
30
|
|
31
|
-
def conditions_for_date(field, value)
|
32
|
-
[ "#{field[:attribute]} #{
|
31
|
+
def conditions_for_date(field, condition, value)
|
32
|
+
[ "#{field[:attribute]} #{condition} ? ", value.value.strftime("%Y-%m-%d")]
|
33
33
|
end
|
34
34
|
|
35
|
-
def conditions_for_numeric(field, value)
|
36
|
-
case
|
35
|
+
def conditions_for_numeric(field, condition, value)
|
36
|
+
case condition
|
37
37
|
when "=" then
|
38
38
|
[ "ABS(#{field[:attribute]}) >= ? AND ABS(#{field[:attribute]}) < ?", value.to_f.abs.floor, (value.to_f.abs + 1).floor ]
|
39
39
|
else
|
40
|
-
[ "#{field[:attribute]} #{
|
40
|
+
[ "#{field[:attribute]} #{condition} ? ", value.to_f ]
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
@@ -2,21 +2,12 @@ module Birddog
|
|
2
2
|
|
3
3
|
class NumericExpression
|
4
4
|
|
5
|
-
attr_reader :condition
|
6
|
-
|
7
5
|
def initialize(value, type)
|
8
|
-
# allow additional spaces to be entered between conditions and value
|
9
6
|
value.gsub!(/\s/, '')
|
10
7
|
parts = value.scan(/(?:[=<>]+|(?:-?\d|\.)+)/)[0,2]
|
11
8
|
|
12
9
|
@value = Float(parts.last)
|
13
10
|
@value = @value.to_i if type == :integer
|
14
|
-
@condition = sanitize_condition(parts.first)
|
15
|
-
end
|
16
|
-
|
17
|
-
def sanitize_condition(cond)
|
18
|
-
valid = %w(= == > < <= >= <>)
|
19
|
-
valid.include?(cond) ? cond.strip : "="
|
20
11
|
end
|
21
12
|
|
22
13
|
def to_i
|
@@ -30,6 +21,7 @@ module Birddog
|
|
30
21
|
def to_s
|
31
22
|
@value.to_s
|
32
23
|
end
|
24
|
+
|
33
25
|
end
|
34
26
|
|
35
27
|
end
|
data/lib/birddog/version.rb
CHANGED
data/lib/birddog.rb
CHANGED
@@ -50,7 +50,7 @@ module Birddog
|
|
50
50
|
:regex => options.fetch(:regex, false),
|
51
51
|
:wildcard => options.fetch(:wildcard, false),
|
52
52
|
:aggregate => options.fetch(:aggregate, false),
|
53
|
-
:options => options.except(:attribute, :type, :case_sensitive, :match_substring, :regex, :wildcard, :aggregate),
|
53
|
+
:options => options.except(:attribute, :type, :case_sensitive, :match_substring, :regex, :wildcard, :aggregate, :cast),
|
54
54
|
:mapping => mapping || lambda{ |v| v }
|
55
55
|
}
|
56
56
|
|
@@ -126,7 +126,7 @@ module Birddog
|
|
126
126
|
|
127
127
|
|
128
128
|
def callable_or_cast(field, value)
|
129
|
-
if field[:cast] &&
|
129
|
+
if field[:cast] && field[:cast].respond_to?(:call)
|
130
130
|
field[:cast].call(value)
|
131
131
|
else
|
132
132
|
cast_value(value, field[:type])
|
@@ -150,7 +150,18 @@ module Birddog
|
|
150
150
|
end
|
151
151
|
private :cast_value
|
152
152
|
|
153
|
+
def parse_condition(value)
|
154
|
+
valid = %w(= == > < <= >= <>)
|
155
|
+
value.gsub!(/\s/, '')
|
156
|
+
|
157
|
+
parts = value.scan(/(?:[=<>]+)/)
|
158
|
+
cond = parts.first
|
159
|
+
valid.include?(cond) ? cond.strip : "="
|
160
|
+
end
|
161
|
+
private :parse_condition
|
162
|
+
|
153
163
|
def setup_conditions(field, value)
|
164
|
+
condition = parse_condition(value)
|
154
165
|
value = callable_or_cast(field, value)
|
155
166
|
value = field[:mapping].call(value)
|
156
167
|
|
@@ -158,9 +169,9 @@ module Birddog
|
|
158
169
|
when :string then
|
159
170
|
conditions_for_string_search(field, value)
|
160
171
|
when :float, :decimal, :integer then
|
161
|
-
conditions_for_numeric(field, value)
|
172
|
+
conditions_for_numeric(field, condition, value)
|
162
173
|
when :date, :datetime, :time then
|
163
|
-
conditions_for_date(field, value)
|
174
|
+
conditions_for_date(field, condition, value)
|
164
175
|
else
|
165
176
|
{ field[:attribute] => value }
|
166
177
|
end
|
data/specs/complex_scope_spec.rb
CHANGED
@@ -10,6 +10,14 @@ describe Birddog::Birddog do
|
|
10
10
|
@bike = @john.products.create(:name => "Bike", :value => 100)
|
11
11
|
end
|
12
12
|
|
13
|
+
describe "castable" do
|
14
|
+
it "casts Date when a cast function is provided" do
|
15
|
+
prods = Product.scopes_for_query("cast_val:10")
|
16
|
+
|
17
|
+
prods.must_include(@ducky)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
13
21
|
describe "aggregates" do
|
14
22
|
it "doesn't include HAVING clause when no condition included" do
|
15
23
|
sql = User.scopes_for_query("aggregate_user").scopes_for_query("total_product_value").to_sql
|
data/specs/spec_helper.rb
CHANGED
@@ -65,6 +65,7 @@ class Product < ActiveRecord::Base
|
|
65
65
|
search.text_search "products.name", "products.value"
|
66
66
|
|
67
67
|
search.field :name, :regex => true, :wildcard => true
|
68
|
+
search.field :cast_val, :type => :decimal, :cast => lambda { |v| 10 }, :attribute => :value
|
68
69
|
search.field :value, :type => :decimal
|
69
70
|
search.field :available, :type => :boolean
|
70
71
|
|
data/specs/test.db
CHANGED
Binary file
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: birddog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-02-01 00:00:00.000000000Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: minitest
|
16
|
-
requirement: &
|
16
|
+
requirement: &22249780 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *22249780
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rake
|
27
|
-
requirement: &
|
27
|
+
requirement: &22249280 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *22249280
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: sqlite3-ruby
|
38
|
-
requirement: &
|
38
|
+
requirement: &22248740 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *22248740
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: chronic
|
49
|
-
requirement: &
|
49
|
+
requirement: &22238660 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *22238660
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: activerecord
|
60
|
-
requirement: &
|
60
|
+
requirement: &22238140 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :runtime
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *22238140
|
69
69
|
description: Seeeeeeeee Readme
|
70
70
|
email:
|
71
71
|
- brandonsdewitt@gmail.com
|
@@ -105,7 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
105
105
|
version: '0'
|
106
106
|
segments:
|
107
107
|
- 0
|
108
|
-
hash:
|
108
|
+
hash: -2231863976579242335
|
109
109
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
110
|
none: false
|
111
111
|
requirements:
|
@@ -114,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
114
|
version: '0'
|
115
115
|
segments:
|
116
116
|
- 0
|
117
|
-
hash:
|
117
|
+
hash: -2231863976579242335
|
118
118
|
requirements: []
|
119
119
|
rubyforge_project: birddog
|
120
120
|
rubygems_version: 1.8.10
|