queryable_pstore 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/queryable_pstore.rb +22 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 36d40cfb43fa8aa45f3ae2dc01b4a4b6a08c67a5
|
4
|
+
data.tar.gz: 94afb9d4bc06e5196ddc7de2c606b0a155cd2d4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6abd1167c469f92a577be7a85b2cd5c8fa076f88e9a6a4ed9627000adfd594a9c7b0f99b4f2c3a043c478250d856f29e72bd097b33b51b5dd8ec02a020dea947
|
7
|
+
data.tar.gz: b589b315454b6a7a70bdd60584b805b89cb6ef7d6098e510e67941af91edc0c3eb9ac145977e5a5ccc19193a910f58cd4767bd3b24aecd5b4840e31f4a09d69f
|
data/lib/queryable_pstore.rb
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
require 'pstore'
|
2
2
|
require 'ostruct'
|
3
|
+
require 'pry'
|
3
4
|
|
4
5
|
class QueryablePStore < PStore
|
5
6
|
class Query
|
6
7
|
attr_reader :attribute, :condition, :argument
|
8
|
+
TERMINATING_FUNCTIONS = [:pluck]
|
7
9
|
|
8
10
|
def initialize(attribute, condition, argument)
|
9
11
|
@attribute = attribute
|
@@ -11,6 +13,24 @@ class QueryablePStore < PStore
|
|
11
13
|
@argument = argument
|
12
14
|
end
|
13
15
|
|
16
|
+
def valid?(records)
|
17
|
+
valid = attribute_present?(records) || argument_is_block? || terminating_function?
|
18
|
+
raise ArgumentError.new("The attribute `#{@attribute}` is not present in the PStore.") unless valid
|
19
|
+
valid
|
20
|
+
end
|
21
|
+
|
22
|
+
def attribute_present?(records)
|
23
|
+
records.any? { |record| record.keys.include? @attribute }
|
24
|
+
end
|
25
|
+
|
26
|
+
def argument_is_block?
|
27
|
+
@argument.respond_to?(:call)
|
28
|
+
end
|
29
|
+
|
30
|
+
def terminating_function?
|
31
|
+
TERMINATING_FUNCTIONS.include?(@condition)
|
32
|
+
end
|
33
|
+
|
14
34
|
def filter(results)
|
15
35
|
if @attribute.empty?
|
16
36
|
handle_terminating_function(results)
|
@@ -69,7 +89,8 @@ class QueryablePStore < PStore
|
|
69
89
|
attribute = method.to_s.split("_")[0..-2].join("_").to_sym
|
70
90
|
modifier = method.to_s.split("_")[-1].to_sym
|
71
91
|
|
72
|
-
|
92
|
+
query = Query.new(attribute, modifier, argument || blk)
|
93
|
+
@queries << query if query.valid?(records)
|
73
94
|
self
|
74
95
|
end
|
75
96
|
|