mongery 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/mongery.rb +33 -7
- data/lib/mongery/version.rb +1 -1
- data/spec/mongery/builder_spec.rb +1 -0
- data/spec/mongery/mapped_properties_spec.rb +34 -0
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c654aa684e186267a8ad90f66b57a7c000e2529b
|
4
|
+
data.tar.gz: 31e9d9fab939af0b876f68782ae44950da8ecda6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 627d0288cfc9409bd7f00f374b2a50ec0c6acdbaaaac90e47855a0cf57b99b427e24cec0a2fd6b485131a56b3b8f507c02c221db2c2afe9552960543d46fc0fd
|
7
|
+
data.tar.gz: 5768a7da04168ee460e1e88cdb05a28d576d46cbd422819ffc5370a2a6569b6ba14cdd2958a8f29291e3a2c1878c9c130edf6fd998e24efdb9c96531b07c8518
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
## 1.0.2 (2015/01/09)
|
2
|
+
- Support mapped_properties to map system columns on Postgres
|
3
|
+
- Look up all values using string keys rather than symbol. Make sure you use indifferent_access, or stringify_key before passing the value hash to Mongery
|
4
|
+
|
1
5
|
## 1.0.1 (2014/12/22)
|
2
6
|
- Force string comparison on non-typed property
|
3
7
|
|
data/lib/mongery.rb
CHANGED
@@ -4,29 +4,40 @@ require "arel"
|
|
4
4
|
|
5
5
|
module Mongery
|
6
6
|
class Builder
|
7
|
-
attr_reader :model, :table, :schema
|
7
|
+
attr_reader :model, :table, :schema, :mapped_properties
|
8
8
|
|
9
9
|
def initialize(model, engine = ActiveRecord::Base, schema = nil)
|
10
10
|
@model = model
|
11
11
|
@table = Arel::Table.new(model, engine)
|
12
12
|
@schema = Schema.new(schema) if schema
|
13
|
+
@mapped_properties = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def mapped_properties=(value)
|
17
|
+
case value
|
18
|
+
when Array
|
19
|
+
@mapped_properties = Hash[ value.map { |v| [v.to_s, v] } ]
|
20
|
+
else
|
21
|
+
@mapped_properties = value
|
22
|
+
end
|
13
23
|
end
|
14
24
|
|
15
25
|
def find(*args)
|
16
|
-
Query.new(table, schema).where(*args)
|
26
|
+
Query.new(table, schema, mapped_properties).where(*args)
|
17
27
|
end
|
18
28
|
|
19
29
|
def insert(*args)
|
20
|
-
Query.new(table, schema).insert(*args)
|
30
|
+
Query.new(table, schema, mapped_properties).insert(*args)
|
21
31
|
end
|
22
32
|
end
|
23
33
|
|
24
34
|
class Query
|
25
|
-
attr_reader :table, :schema
|
35
|
+
attr_reader :table, :schema, :mapped_properties
|
26
36
|
|
27
|
-
def initialize(table, schema)
|
37
|
+
def initialize(table, schema, mapped_properties)
|
28
38
|
@table = table
|
29
39
|
@schema = schema
|
40
|
+
@mapped_properties = mapped_properties
|
30
41
|
@condition = nil
|
31
42
|
end
|
32
43
|
|
@@ -70,17 +81,26 @@ module Mongery
|
|
70
81
|
self
|
71
82
|
end
|
72
83
|
|
84
|
+
def mapped_values(args)
|
85
|
+
pairs = []
|
86
|
+
mapped_properties.each do |key, column|
|
87
|
+
pairs.push([table[column], args[key]]) if args.key?(key)
|
88
|
+
end
|
89
|
+
pairs
|
90
|
+
end
|
91
|
+
private :mapped_values
|
92
|
+
|
73
93
|
def insert(args)
|
74
94
|
Arel::InsertManager.new(table.engine).tap do |manager|
|
75
95
|
manager.into(table)
|
76
|
-
manager.insert([[table[:id], args[
|
96
|
+
manager.insert([[table[:id], args['_id']], [table[:data], args.to_json], *mapped_values(args)])
|
77
97
|
end
|
78
98
|
end
|
79
99
|
|
80
100
|
def update(args)
|
81
101
|
Arel::UpdateManager.new(table.engine).tap do |manager|
|
82
102
|
manager.table(table)
|
83
|
-
manager.set([[table[:data], args.to_json]])
|
103
|
+
manager.set([[table[:data], args.to_json], *mapped_values(args)])
|
84
104
|
manager.where(condition) if condition
|
85
105
|
end
|
86
106
|
end
|
@@ -94,6 +114,10 @@ module Mongery
|
|
94
114
|
|
95
115
|
private
|
96
116
|
|
117
|
+
def mapped_keys
|
118
|
+
mapped_properties.keys
|
119
|
+
end
|
120
|
+
|
97
121
|
def condition
|
98
122
|
@condition ||= translate(@where)
|
99
123
|
end
|
@@ -118,6 +142,8 @@ module Mongery
|
|
118
142
|
raise UnsupportedQuery, "Unsupported operator #{col}"
|
119
143
|
when "_id"
|
120
144
|
translate_value(table[:id], value)
|
145
|
+
when *mapped_keys
|
146
|
+
translate_value(table[mapped_properties[col.to_s]], value)
|
121
147
|
else
|
122
148
|
if schema
|
123
149
|
translate_value_schema(col, sql_json_path(col), value)
|
data/lib/mongery/version.rb
CHANGED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'active_support/core_ext/hash/indifferent_access'
|
2
|
+
|
3
|
+
describe "#mapped_properties" do
|
4
|
+
let(:builder) {
|
5
|
+
Mongery::Builder.new(:test).tap do |builder|
|
6
|
+
builder.mapped_properties = [:user_id, :created_at, :updated_at]
|
7
|
+
end
|
8
|
+
}
|
9
|
+
|
10
|
+
let(:args) {
|
11
|
+
{ '_id' => 1, 'user_id' => 2, 'created_at' => Time.now, 'foo' => "bar" }.with_indifferent_access
|
12
|
+
}
|
13
|
+
|
14
|
+
it 'generates INSERT' do
|
15
|
+
expect(builder.insert(args).to_sql).to match /VALUES \(1, '{.*?}', 2, '\d{4}-\d{2}-\d{2} .*'\)$/
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'generates UPDATE' do
|
19
|
+
expect(builder.find(_id: 1).update(args).to_sql).to match /SET "data" = .*, "user_id" = 2, "created_at" = '\d{4}-.*' WHERE/;
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'searches WHERE with symbol' do
|
23
|
+
expect(builder.find(:user_id => '2').to_sql).to match /WHERE "test"\."user_id" = '2'/;
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'searches WHERE with string' do
|
27
|
+
expect(builder.find("user_id" => '2').to_sql).to match /WHERE "test"\."user_id" = '2'/;
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'searches WHERE with operators' do
|
31
|
+
expect(builder.find("created_at" => {'$in' => ['2014-01-01', '2015-01-01']}, "foo" => "bar").to_sql)
|
32
|
+
.to match /WHERE "test"\."created_at" IN \('2014-01-01', '2015-01-01'\) AND data#>>'{foo}' = 'bar'/;
|
33
|
+
end
|
34
|
+
end
|
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: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tatsuhiko Miyagawa
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: arel
|
@@ -127,6 +127,7 @@ files:
|
|
127
127
|
- mongery.gemspec
|
128
128
|
- spec/mongery/builder_schema_spec.rb
|
129
129
|
- spec/mongery/builder_spec.rb
|
130
|
+
- spec/mongery/mapped_properties_spec.rb
|
130
131
|
- spec/spec_helper.rb
|
131
132
|
homepage: https://github.com/miyagawa/mongery
|
132
133
|
licenses:
|
@@ -148,12 +149,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
148
149
|
version: '0'
|
149
150
|
requirements: []
|
150
151
|
rubyforge_project:
|
151
|
-
rubygems_version: 2.4.
|
152
|
+
rubygems_version: 2.4.5
|
152
153
|
signing_key:
|
153
154
|
specification_version: 4
|
154
155
|
summary: Convert MongoDB query to Arel for PostgreSQL + JSON
|
155
156
|
test_files:
|
156
157
|
- spec/mongery/builder_schema_spec.rb
|
157
158
|
- spec/mongery/builder_spec.rb
|
159
|
+
- spec/mongery/mapped_properties_spec.rb
|
158
160
|
- spec/spec_helper.rb
|
159
|
-
has_rdoc:
|