birddog 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -160,10 +160,10 @@ module Birddog
160
160
  private :cast_value
161
161
 
162
162
  def parse_condition(value)
163
- valid = %w(= == > < <= >= <>)
163
+ valid = %w(= == > < <= >= <> =~ ~=)
164
164
  value.gsub!(/\s/, '')
165
165
 
166
- parts = value.scan(/(?:[=<>]+)/)
166
+ parts = value.scan(/(?:[=<>~]+)/)
167
167
  cond = parts.first
168
168
  valid.include?(cond) ? cond.strip : "="
169
169
  end
@@ -2,6 +2,12 @@ module Birddog
2
2
 
3
3
  module FieldConditions
4
4
 
5
+ FieldConversions = {
6
+ :integer => lambda{ |v| v.to_i },
7
+ :decimal => lambda{ |v| v.to_f },
8
+ :float => lambda{ |v| v.to_f }
9
+ }
10
+
5
11
  def conditions_for_string_search(field, value)
6
12
  search_con = " LIKE ? "
7
13
  field_to_search = "lower(#{field[:attribute]})"
@@ -33,11 +39,13 @@ module Birddog
33
39
  end
34
40
 
35
41
  def conditions_for_numeric(field, condition, value)
42
+ db_value = FieldConversions[field[:type]].call(value)
43
+
36
44
  case condition
37
- when "=" then
38
- [ "ABS(#{field[:attribute]}) >= ? AND ABS(#{field[:attribute]}) < ?", value.to_f.abs.floor, (value.to_f.abs + 1).floor ]
45
+ when "=~", "~=" then
46
+ [ "ABS(#{field[:attribute]}) >= ? AND ABS(#{field[:attribute]}) < ?", db_value.abs.floor, (db_value.abs + 1).floor ]
39
47
  else
40
- [ "#{field[:attribute]} #{condition} ? ", value.to_f ]
48
+ [ "#{field[:attribute]} #{condition} ? ", db_value]
41
49
  end
42
50
  end
43
51
 
@@ -4,7 +4,7 @@ module Birddog
4
4
 
5
5
  def initialize(value, type)
6
6
  value.gsub!(/\s/, '')
7
- parts = value.scan(/(?:[=<>]+|(?:-?\d|\.)+)/)[0,2]
7
+ parts = value.scan(/(?:[=<>~]+|(?:-?\d|\.)+)/)[0,2]
8
8
 
9
9
  @value = Float(parts.last)
10
10
  @value = @value.to_i if type == :integer
@@ -1,3 +1,3 @@
1
1
  module Birddog
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -6,6 +6,7 @@ describe Birddog::Birddog do ####################
6
6
  Product.destroy_all
7
7
  @john = User.create(:first_name => "John", :last_name => "Doe")
8
8
  @ducky = @john.products.create(:name => "Rubber Duck", :value => 10)
9
+ @apple_duck = @john.products.create(:name => "Apple Duck", :value => 10.38)
9
10
  @tv = @john.products.create(:name => "TV", :value => 200)
10
11
  end
11
12
 
@@ -51,15 +52,28 @@ describe Birddog::Birddog do ####################
51
52
  end
52
53
 
53
54
  it "allows defining arbitrary keywords to create scopes" do
54
- @john.products.scopes_for_query("sort:name").all.must_equal([@tv, @ducky])
55
+ @john.products.scopes_for_query("sort:name").all.must_equal([@tv, @ducky, @apple_duck])
55
56
  end
56
57
 
57
58
  describe "numeric fields" do ########################
58
59
  it "searches by equals" do
59
60
  @john.products.scopes_for_query("value:=10").must_include(@ducky)
61
+ @john.products.scopes_for_query("value:=10").wont_include(@apple_duck)
60
62
  @john.products.scopes_for_query("value:=10").size.must_equal(1)
61
63
  end
62
64
 
65
+ it "includes range in ~= search" do
66
+ @john.products.scopes_for_query("value:~=10").must_include(@ducky)
67
+ @john.products.scopes_for_query("value:~=10").must_include(@apple_duck)
68
+ @john.products.scopes_for_query("value:~=10").size.must_equal(2)
69
+ end
70
+
71
+ it "includes range in =~ search" do
72
+ @john.products.scopes_for_query("value:=~10").must_include(@ducky)
73
+ @john.products.scopes_for_query("value:=~10").must_include(@apple_duck)
74
+ @john.products.scopes_for_query("value:=~10").size.must_equal(2)
75
+ end
76
+
63
77
  it "searches by equality without =" do
64
78
  @john.products.scopes_for_query("value:10").must_include(@ducky)
65
79
  @john.products.scopes_for_query("value:10").size.must_equal(1)
@@ -72,7 +86,8 @@ describe Birddog::Birddog do ####################
72
86
 
73
87
  it "searches by less than" do
74
88
  @john.products.scopes_for_query("value:<100").must_include(@ducky)
75
- @john.products.scopes_for_query("value:<100").size.must_equal(1)
89
+ @john.products.scopes_for_query("value:<100").must_include(@apple_duck)
90
+ @john.products.scopes_for_query("value:<100").size.must_equal(2)
76
91
  end
77
92
 
78
93
  it "searches by =>" do
@@ -83,7 +98,8 @@ describe Birddog::Birddog do ####################
83
98
  it "searches on negatives" do
84
99
  @john.products.scopes_for_query("value:>=-200").must_include(@tv)
85
100
  @john.products.scopes_for_query("value:>=-200").must_include(@ducky)
86
- @john.products.scopes_for_query("value:>=-200").size.must_equal(2)
101
+ @john.products.scopes_for_query("value:>=-200").must_include(@apple_duck)
102
+ @john.products.scopes_for_query("value:>=-200").size.must_equal(3)
87
103
  end
88
104
 
89
105
  describe "spacing" do ###########################
data/specs/spec_helper.rb CHANGED
@@ -24,7 +24,7 @@ ActiveRecord::Schema.define(:version => 1) do
24
24
 
25
25
  create_table :products do |t|
26
26
  t.string :name
27
- t.integer :value
27
+ t.decimal :value
28
28
  t.boolean :available, :default => true
29
29
  t.belongs_to :user
30
30
  end
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.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-29 00:00:00.000000000Z
12
+ date: 2012-03-03 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: minitest
16
- requirement: &11974160 !ruby/object:Gem::Requirement
16
+ requirement: &19678920 !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: *11974160
24
+ version_requirements: *19678920
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rake
27
- requirement: &11973660 !ruby/object:Gem::Requirement
27
+ requirement: &19678160 !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: *11973660
35
+ version_requirements: *19678160
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3-ruby
38
- requirement: &11969740 !ruby/object:Gem::Requirement
38
+ requirement: &19677700 !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: *11969740
46
+ version_requirements: *19677700
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: pry
49
- requirement: &11969100 !ruby/object:Gem::Requirement
49
+ requirement: &19677240 !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: :development
56
56
  prerelease: false
57
- version_requirements: *11969100
57
+ version_requirements: *19677240
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: chronic
60
- requirement: &11968340 !ruby/object:Gem::Requirement
60
+ requirement: &19676800 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *11968340
68
+ version_requirements: *19676800
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: activerecord
71
- requirement: &11967900 !ruby/object:Gem::Requirement
71
+ requirement: &19676360 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *11967900
79
+ version_requirements: *19676360
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: activesupport
82
- requirement: &11967400 !ruby/object:Gem::Requirement
82
+ requirement: &19663100 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '0'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *11967400
90
+ version_requirements: *19663100
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: squeel
93
- requirement: &11966580 !ruby/object:Gem::Requirement
93
+ requirement: &19662480 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,7 +98,7 @@ dependencies:
98
98
  version: '0'
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *11966580
101
+ version_requirements: *19662480
102
102
  description: Seeeeeeeee Readme
103
103
  email:
104
104
  - brandonsdewitt@gmail.com
@@ -139,7 +139,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
139
139
  version: '0'
140
140
  segments:
141
141
  - 0
142
- hash: -2053552367670732027
142
+ hash: 1270257218013225858
143
143
  required_rubygems_version: !ruby/object:Gem::Requirement
144
144
  none: false
145
145
  requirements:
@@ -148,7 +148,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  version: '0'
149
149
  segments:
150
150
  - 0
151
- hash: -2053552367670732027
151
+ hash: 1270257218013225858
152
152
  requirements: []
153
153
  rubyforge_project: birddog
154
154
  rubygems_version: 1.8.10