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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/queryable_pstore.rb +22 -1
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a67a66ac25423553b04d2dbf782154bc13799e40
4
- data.tar.gz: 166c36f6de553d2fd42f64d800a2be5ca437c29a
3
+ metadata.gz: 36d40cfb43fa8aa45f3ae2dc01b4a4b6a08c67a5
4
+ data.tar.gz: 94afb9d4bc06e5196ddc7de2c606b0a155cd2d4d
5
5
  SHA512:
6
- metadata.gz: d7fe4f56a72f6c88d11baea62c81987fcf8a8ac1f4c02950c162d80be2084bfeabcf6d449a4ce6670f7f92d169ee71f60cf206bd12b807255edfc2a1ee454c82
7
- data.tar.gz: 66ebc03759b53d00e1a478b2ef3d01421df0688d3848e5c366e33d42dbb8fdd1c9d027613f73ac1e11e031dc029dcbfd44cd0f465570a353359d671a72e1002f
6
+ metadata.gz: 6abd1167c469f92a577be7a85b2cd5c8fa076f88e9a6a4ed9627000adfd594a9c7b0f99b4f2c3a043c478250d856f29e72bd097b33b51b5dd8ec02a020dea947
7
+ data.tar.gz: b589b315454b6a7a70bdd60584b805b89cb6ef7d6098e510e67941af91edc0c3eb9ac145977e5a5ccc19193a910f58cd4767bd3b24aecd5b4840e31f4a09d69f
@@ -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
- @queries << Query.new(attribute, modifier, argument || blk)
92
+ query = Query.new(attribute, modifier, argument || blk)
93
+ @queries << query if query.valid?(records)
73
94
  self
74
95
  end
75
96
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: queryable_pstore
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
  - Jacob Smith