pgrel 0.1.0 → 0.1.1

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: 642a7d4b7a7b19f5acc220e6f74d41d6d0e00524
4
- data.tar.gz: 007ba40877cfc0c6bdbb260502b98b1627ceaaa6
3
+ metadata.gz: 9b99f0c11a5e624145d178ed3bdb97dd9345ccad
4
+ data.tar.gz: 7db4f0a1086111e10ded6e6f9af923179ddc2012
5
5
  SHA512:
6
- metadata.gz: 82cf0b3c4ed6c5ec72a55e6572ee0a197cb15079c70f48d61499acaac15d4d98bcb2262f5965a55c5c669bd4a98e04edd0e18da9b0ff7e018ae94ca82b2363e5
7
- data.tar.gz: 130ed3ac3d71c6c98f72a33a881877f23f147653a1735a68a4bc8016996345cca77721713874d9f0a8d9b2991f2ccc4758b026224b4aa450e9b2cf0e0c8c8ce9
6
+ metadata.gz: 4ef4b9230787c49c3ccb1b24d68113cf90b57c0ae8281e6814eb6d01c01925fc04031ee3f02e1cc09abf60386b2d3c12f6b2f46674f6b75ccc27cd34d3a08619
7
+ data.tar.gz: 8aaeaab12dd78aac0ad21a49b68af1a5efb83aeb261e0cfd431f24662b5f8b0dc88dcc4b93c8115b3a0d4999ddf3ca842c32d75f92270ebab7113eac31b71acf
data/.gitignore CHANGED
@@ -26,7 +26,8 @@ Thumbs.db
26
26
  coverage/
27
27
 
28
28
  .bundle/
29
- log/*.log
29
+ *.log
30
+ *.gem
30
31
  pkg/
31
32
  spec/dummy/log/*.log
32
33
  spec/dummy/tmp/
@@ -19,10 +19,6 @@ AllCops:
19
19
  DisplayCopNames: true
20
20
  StyleGuideCopsOnly: false
21
21
 
22
- Style/Documentation:
23
- Exclude:
24
- - 'lib/**/version.rb'
25
-
26
22
  Metrics/LineLength:
27
23
  Max: 100
28
24
 
data/README.md CHANGED
@@ -2,6 +2,112 @@
2
2
 
3
3
  ## Pgrel
4
4
 
5
- **Rails 5**
5
+ ActiveRecord extension for querying hstore, array and jsonb.
6
+
7
+ Compatible with **Rails** >= 4.0 (and even with upcoming **Rails 5**!).
8
+
9
+ ### General
10
+
11
+ The functionality is based on ActiveRecord `WhereChain`.
12
+ To start querying call `where(:store_name)` and chain it with store-specific call (see below).
13
+
14
+ #### Install
15
+
16
+ In your Gemfile:
17
+
18
+ ```ruby
19
+ gem "pgrel", "~>0.1"
20
+ ```
21
+
22
+ ### HStore
23
+
24
+ Query by key value:
25
+
26
+ ```ruby
27
+ Hstore.where.store(:tags, a: 1, b: 2)
28
+ #=> select * from hstores where tags->'a' = '1' and tags->'b' = '2'
29
+
30
+ Hstore.where.store(:tags, a: [1, 2])
31
+ #=> select * from hstores where tags->'a' in ('1', '2')
32
+ ```
33
+
34
+ Keys existence:
35
+
36
+ ```ruby
37
+ # Retrieve items that have key 'a' in 'tags'::hstore
38
+ Hstore.where.store(:tags).key(:a)
39
+ #=> select * from hstores where tags ? 'a'
40
+
41
+ # Retrieve items that have both keys 'a' and 'b' in 'tags'::hstore
42
+ Hstore.where.store(:tags).keys('a', 'b')
43
+ #=> select * from hstores where tags ?& array['a', 'b']
44
+
45
+ # Retrieve items that have either key 'a' or 'b' in 'tags'::hstore
46
+ Hstore.where.store(:tags).any('a', 'b')
47
+ #=> select * from hstores where tags ?| array['a', 'b']
48
+ ```
49
+
50
+ Containment:
51
+
52
+ ```ruby
53
+ Hstore.where.store(:tags).contains(a: 1, b: 2)
54
+ #=> select * from hstores where tags @> '\"a\"=>\"1\", \"b\"=>\"2\"'
55
+
56
+ Hstore.where.store(:tags).contained(a: 1, b: 2)
57
+ #=> select * from hstores where tags <@ '\"a\"=>\"1\", \"b\"=>\"2\"'
58
+ ```
59
+
60
+ ### JSONB
61
+
62
+ All queries for Hstore also available for JSONB.
63
+
64
+ **NOTE**. Querying by array value always resolves to `IN (...)` statement.
65
+ Thus it's impossible to query json array value, e.g.:
66
+
67
+ ```ruby
68
+ Model.create!(tags: {main: ['a', 'b']})
69
+
70
+ Model.where.store(:tags, main: ['a', 'b']).empty? == true
71
+ #=> select * from models where tags->'main' in ('a', 'b')
72
+ ```
73
+
74
+ Path query:
75
+
76
+ ```ruby
77
+ Model.create!(tags: {main: ['a', 'b'], user: { name: 'john' } })
78
+
79
+ # You can use object to query by simple value
80
+ Model.where.store(:tags).path(user: { name: 'john' })
81
+ #=> select * from hstores where tags#>>'{\"user\",\"name\"}' = 'john'
82
+ # or passing path parts as args one by one with value at the end
83
+ Model.where.store(:tags).path(:user, :name, 'john')
84
+
85
+ # Match by complex value (array or object)
86
+ Model.where.store(:tags).path(:main, ['a', 'b'])
87
+ #=> select * from hstores where tags#>'{\"main\"}' = '[\"a\",\"b\"]'
88
+ ```
89
+
90
+ ### Array
91
+
92
+ Array stores support containment queries (just like Hstore and JSONB) and also `overlap` operator.
93
+
94
+ **NOTE**. There are some other array operators ('ANY', 'ALL', querying by index - value) which I'm not going to implement – PRs are welcomed!
95
+
96
+ Overlap:
97
+ ```ruby
98
+ Model.where.store(:tags).overlap('a', 'b')
99
+ #=> select * from hstores where tags && '{\"a\",\"b\"}'
100
+ ```
101
+
102
+ ### Negation
103
+
104
+ Use `not` before operator to constuct negation or pass arguments to `not` to run key-value query.
105
+
106
+ ```ruby
107
+ Model.where.store(:tags).not.overlap('a', 'b')
108
+ #=> select * from hstores where not (tags && '{\"a\",\"b\"}')
109
+
110
+ Hstore.where.store(:tags).not(a: 1)
111
+ #=> select * from hstores where tags->'a' != '1'
112
+ ```
6
113
 
7
- ActiveRecord extension for querying hstore and jsonb.
@@ -26,7 +26,7 @@ module ActiveRecord
26
26
  when Array
27
27
  val.map { |v| stringify(v) }
28
28
  when Hash
29
- Hash[val.map { |k, v| [k, stringify(v)] }]
29
+ Hash[val.map { |k, v| [stringify(k), stringify(v)] }]
30
30
  else
31
31
  val.to_s
32
32
  end
@@ -1,3 +1,3 @@
1
- module Pgrel
2
- VERSION = "0.1.0"
1
+ module Pgrel # :nodoc:
2
+ VERSION = "0.1.1"
3
3
  end
@@ -24,6 +24,7 @@ describe Hstore do
24
24
  Hstore.create!(name: 'c', tags: { f: true, d: 'b' })
25
25
  Hstore.create!(name: 'd', tags: { f: false })
26
26
  Hstore.create!(name: 'e', tags: { a: 2, c: 'x', d: 'c', g: 'c' })
27
+ Hstore.create!(tags: { 1 => 2 })
27
28
  end
28
29
 
29
30
  context '#where' do
@@ -33,6 +34,10 @@ describe Hstore do
33
34
  expect(Hstore.where.store(:tags, f: false).first.name).to eq 'd'
34
35
  end
35
36
 
37
+ it 'integer keys' do
38
+ expect(Hstore.where.store(:tags, 1 => 2).size).to eq 1
39
+ end
40
+
36
41
  it 'arrays' do
37
42
  expect(Hstore.where.store(:tags, a: [1, 2, 3]).size).to eq 3
38
43
  end
@@ -115,11 +120,11 @@ describe Hstore do
115
120
  end
116
121
 
117
122
  it '#any' do
118
- expect(Hstore.where.store(:tags).not.any('a', 'f').size).to eq 0
123
+ expect(Hstore.where.store(:tags).not.any('a', 'f').size).to eq 1
119
124
  end
120
125
 
121
126
  it '#keys' do
122
- expect(Hstore.where.store(:tags).not.keys('a', 'f').size).to eq 4
127
+ expect(Hstore.where.store(:tags).not.keys('a', 'f').size).to eq 5
123
128
  end
124
129
  end
125
130
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgrel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - palkan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-02 00:00:00.000000000 Z
11
+ date: 2015-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord