mongery 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1c2ea2dd29e75e89c48aae9e0aa1614a8896625e
4
- data.tar.gz: eeb65b09d38eb4ace06422f046caa6657324ec6e
3
+ metadata.gz: 1124d4cc8a8887a26daf6cd0167b395692eb2b80
4
+ data.tar.gz: e0cc0961e38626e40cc26f079471e0e0c52186c2
5
5
  SHA512:
6
- metadata.gz: a2f38853f1ffd8fe79824dc01efa935dfd4e253399e9f8324b403a93c6a0fa446bf2e0133f93cf0d7f04dd4fa74bf5338301401dfdb1906d4e2aa57cb84716f1
7
- data.tar.gz: e56c8c81a04e7c55bc6e21dcdb5343719e760e43e3843f51e73f555a9e8be921ce351cd599c8b818dd32a12668fea3fa05e93f70a88bf9ea7e31c6c67bae3b33
6
+ metadata.gz: e4de70968015998d26f1bd15e15e8e08c24a67356ae1ba3351f1be7ca6baed823a8c7688bab266b3fddb7dd9a31cbdfbb5540b1e81d06235bea504ab5e294409
7
+ data.tar.gz: 8861688d905bb127308cde48cd9797903d00620f500c15d89413527a20fce2bca18262af44761ee237611eb4923576f0c7dbf0abc33a414c8b21a096cf13454b
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## 0.0.5 (2014/11/10)
2
+ - Use JSON path expression for nested data to avoid errors like "cannot extract element from a scalar"
3
+
1
4
  ## 0.0.4 (2014/10/23)
2
5
  - Support insert(), update() and delete() for more compatibilities
3
6
 
data/README.md CHANGED
@@ -24,8 +24,6 @@ CREATE TABLE objects (
24
24
 
25
25
  Currently Mongery assumes the `id` values are stored as `_id` key duplicated in the json data as well. This can be customized in the future updates.
26
26
 
27
- ### Selects
28
-
29
27
  ```ruby
30
28
  builder = Mongery::Builder.new(:objects)
31
29
 
@@ -34,12 +32,6 @@ builder.find({ _id: 'abcd' }).limit(1).to_sql
34
32
 
35
33
  builder.find({ age: {"$gte" => 21 } }).sort({ name: -1 }).to_sql
36
34
  # => SELECT data FROM objects WHERE (data->>'age')::integer >= 21 ORDER BY data->>'name' DESC
37
- ```
38
-
39
- ### Inserts, Updates and Deletes
40
-
41
- ```ruby
42
- builder = Mongery::Builder.new(:objects)
43
35
 
44
36
  builder.insert({ _id: 'foobar' }).to_sql
45
37
  # => INSERT INTO "objects" ("id", "data") VALUES ('foobar', '{"_id":"foobar"}')
@@ -49,6 +41,7 @@ builder.find({ age: {"$gte" => 21 } }).update({ name: "John" }).to_sql
49
41
 
50
42
  builder.find({ age: {"$eq" => 55 } }).delete.to_sql
51
43
  # => DELETE FROM "objects" WHERE (data->>'age')::integer = 21
44
+ ```
52
45
 
53
46
  ## See Also
54
47
 
@@ -1,3 +1,3 @@
1
1
  module Mongery
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/mongery.rb CHANGED
@@ -2,8 +2,6 @@ require "mongery/version"
2
2
  require "arel"
3
3
 
4
4
  module Mongery
5
- # Translate Mongo query to Arel AST
6
-
7
5
  class Builder
8
6
  attr_reader :model, :table
9
7
 
@@ -192,13 +190,16 @@ module Mongery
192
190
  end
193
191
 
194
192
  def sql_json_path(col)
195
- path = "data"
196
- parts = col.to_s.split('.')
197
- parts.each_with_index do |part, index|
198
- sep = index == parts.size - 1 ? "->>" : "->"
199
- path += sep + quote(part)
193
+ paths = col.to_s.split('.')
194
+ if paths.size > 1
195
+ Arel.sql("data#>>#{json_pathize(paths)}")
196
+ else
197
+ Arel.sql("data->>#{quote(paths.first)}")
200
198
  end
201
- Arel.sql(path)
199
+ end
200
+
201
+ def json_pathize(paths)
202
+ quote("{#{paths.join(',')}}")
202
203
  end
203
204
 
204
205
  def quote(str)
@@ -41,7 +41,9 @@ describe Mongery::Builder do
41
41
  [ { bool: true }, { },
42
42
  /WHERE data->>'bool' = 'true'$/ ],
43
43
  [ { 'email.address' => 'john@example.com' }, { },
44
- /WHERE data->'email'->>'address' = 'john@example.com'$/ ],
44
+ /WHERE data#>>'{email,address}' = 'john@example.com'$/ ],
45
+ [ { "x'y.z" => 'john' }, { },
46
+ /WHERE data#>>'{x''y,z}' = 'john'$/ ],
45
47
  [ { type: "food", "$or" => [{name: "miso"}, {name: "tofu"}]}, { },
46
48
  /WHERE data->>'type' = 'food' AND \(data->>'name' = 'miso' OR data->>'name' = 'tofu'\)$/ ],
47
49
  [ { "$or" => [{ _id: "foo" }, { _id: "bar" }] }, { },
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tatsuhiko Miyagawa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-10-23 00:00:00.000000000 Z
11
+ date: 2014-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: arel