dm-json-search 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{dm-json-search}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nathan Herald"]
@@ -23,13 +23,22 @@ Gem::Specification.new do |s|
23
23
  "dm-json-search.gemspec",
24
24
  "lib/dm-json-search.rb",
25
25
  "lib/dm-json-search/query_ext.rb",
26
- "lib/dm-json-search/searchable.rb"
26
+ "lib/dm-json-search/searchable.rb",
27
+ "spec/.bacon",
28
+ "spec/dm-json-search_spec.rb",
29
+ "spec/spec_fixtures.rb",
30
+ "spec/spec_helper.rb"
27
31
  ]
28
32
  s.homepage = %q{http://github.com/myobie/dm-json-search}
29
33
  s.rdoc_options = ["--charset=UTF-8"]
30
34
  s.require_paths = ["lib"]
31
35
  s.rubygems_version = %q{1.3.5}
32
36
  s.summary = %q{Search your models with JSON!}
37
+ s.test_files = [
38
+ "spec/dm-json-search_spec.rb",
39
+ "spec/spec_fixtures.rb",
40
+ "spec/spec_helper.rb"
41
+ ]
33
42
 
34
43
  if s.respond_to? :specification_version then
35
44
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -1,4 +1,5 @@
1
1
  require 'dm-core'
2
+ require 'json'
2
3
  require File.dirname(__FILE__) + '/dm-json-search/query_ext'
3
4
  require File.dirname(__FILE__) + '/dm-json-search/searchable'
4
5
 
@@ -110,6 +110,11 @@ class DataMapper::Query
110
110
  compare = comparison.values.first
111
111
  subject = string_to_subject(compare.keys.first)
112
112
  value = compare.values.first
113
+
114
+ if value.is_a?(Hash) && value.keys.length == 1 && value.keys.first == "collection"
115
+ value = DataMapper::Collection.new(DataMapper::Query.from_hash(value.values.first))
116
+ end
117
+
113
118
  DataMapper::Query::Conditions::Comparison.new(slug, subject, value)
114
119
  end
115
120
 
@@ -125,6 +130,8 @@ class DataMapper::Query
125
130
 
126
131
  private
127
132
  def operation_to_hash(operation)
133
+ return {} if operation.slug == :null # just forget null's, they don't exist
134
+
128
135
  {
129
136
  operation.slug => operation.operands.collect { |o| operand_to_hash(o) }
130
137
  }
@@ -143,9 +150,12 @@ private
143
150
  end
144
151
 
145
152
  def comparison_to_hash(comparison)
153
+ value = comparison.value
154
+ value = { "collection" => value.query.to_hash } if value.class == DataMapper::Collection
155
+
146
156
  {
147
157
  comparison.slug => {
148
- subject_to_hash(comparison.subject) => comparison.value
158
+ subject_to_hash(comparison.subject) => value
149
159
  }
150
160
  }
151
161
  end
File without changes
@@ -0,0 +1,19 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe "dm-json-search" do
4
+
5
+ before do
6
+ DataMapper.auto_migrate!
7
+ end
8
+
9
+ should "produce the same json it wants to consume" do
10
+ j = Book.all(:author => Author.all(:place => Place.all)).query.to_json
11
+ k = Book.all_from_json(j).query.to_json
12
+ j.should == k
13
+
14
+ j = Book.all(:title.like => "foo").query.to_json
15
+ k = Book.all_from_json(j).query.to_json
16
+ j.should == k
17
+ end
18
+
19
+ end
@@ -0,0 +1,37 @@
1
+ include DataMapper::Sweatshop::Unique
2
+
3
+ class Book
4
+ include DataMapper::Resource
5
+ property :id, Serial
6
+ property :title, String
7
+ belongs_to :author
8
+ end
9
+
10
+ class Author
11
+ include DataMapper::Resource
12
+ property :id, Serial
13
+ property :name, String
14
+ has n, :books
15
+ belongs_to :place
16
+ end
17
+
18
+ class Place
19
+ include DataMapper::Resource
20
+ property :id, Serial
21
+ property :name, String
22
+ has n, :authors
23
+ end
24
+
25
+ Book.fixture {{
26
+ :title => ((/\w{10}/.gen+' ')*2).strip,
27
+ :author => Author.make
28
+ }}
29
+
30
+ Author.fixture {{
31
+ :name => ((/\w{10}/.gen+' ')*2).strip,
32
+ :place => Place.make
33
+ }}
34
+
35
+ Place.fixture {{
36
+ :name => ((/\w{10}/.gen+' ')*2).strip
37
+ }}
@@ -0,0 +1,74 @@
1
+ require "bacon"
2
+ require "mocha"
3
+ require File.dirname(__FILE__) + "/../lib/dm-json-search"
4
+
5
+ require 'dm-sweatshop'
6
+ require File.join(File.dirname(__FILE__), 'spec_fixtures')
7
+
8
+ DataMapper.setup(:default, "sqlite3::memory:")
9
+ DataMapper.auto_migrate!
10
+
11
+ class Proc
12
+ def increase?
13
+ pre_result = yield
14
+ called = call
15
+ post_result = yield
16
+ pre_result < post_result
17
+ end
18
+
19
+ def decrease?
20
+ pre_result = yield
21
+ called = call
22
+ post_result = yield
23
+ pre_result > post_result
24
+ end
25
+ end
26
+
27
+ # from 1 to 10 how likely, 1 being not very likely and 10 being all the time
28
+ def do_i?(i = 5)
29
+ rand(500) < 50 * i
30
+ end
31
+
32
+
33
+
34
+
35
+ ##
36
+ # Hash additions.
37
+ #
38
+ # From
39
+ # * http://wincent.com/knowledge-base/Fixtures_considered_harmful%3F
40
+ # * Neil Rahilly
41
+
42
+ class Hash
43
+
44
+ ##
45
+ # Filter keys out of a Hash.
46
+ #
47
+ # { :a => 1, :b => 2, :c => 3 }.except(:a)
48
+ # => { :b => 2, :c => 3 }
49
+
50
+ def except(*keys)
51
+ self.reject { |k,v| keys.include?(k || k.to_sym) }
52
+ end
53
+
54
+ ##
55
+ # Override some keys.
56
+ #
57
+ # { :a => 1, :b => 2, :c => 3 }.with(:a => 4)
58
+ # => { :a => 4, :b => 2, :c => 3 }
59
+
60
+ def with(overrides = {})
61
+ self.merge overrides
62
+ end
63
+
64
+ ##
65
+ # Returns a Hash with only the pairs identified by +keys+.
66
+ #
67
+ # { :a => 1, :b => 2, :c => 3 }.only(:a)
68
+ # => { :a => 1 }
69
+
70
+ def only(*keys)
71
+ self.reject { |k,v| !keys.include?(k || k.to_sym) }
72
+ end
73
+
74
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dm-json-search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Herald
@@ -30,6 +30,10 @@ files:
30
30
  - lib/dm-json-search.rb
31
31
  - lib/dm-json-search/query_ext.rb
32
32
  - lib/dm-json-search/searchable.rb
33
+ - spec/.bacon
34
+ - spec/dm-json-search_spec.rb
35
+ - spec/spec_fixtures.rb
36
+ - spec/spec_helper.rb
33
37
  has_rdoc: true
34
38
  homepage: http://github.com/myobie/dm-json-search
35
39
  licenses: []
@@ -58,5 +62,7 @@ rubygems_version: 1.3.5
58
62
  signing_key:
59
63
  specification_version: 3
60
64
  summary: Search your models with JSON!
61
- test_files: []
62
-
65
+ test_files:
66
+ - spec/dm-json-search_spec.rb
67
+ - spec/spec_fixtures.rb
68
+ - spec/spec_helper.rb