queryable_pstore 0.0.1

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 +7 -0
  2. data/lib/queryable_pstore.rb +85 -0
  3. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6771243c144cd9d3fb77eb8ad424c077b897376e
4
+ data.tar.gz: 5a7382fca3ebee4cdd76534687beb3b80266060d
5
+ SHA512:
6
+ metadata.gz: 8dda89f76d6b46b9007dc5dea405891f2df0b41375d983a92b5d87bbb07d080f2a18b605c1f7210579e6a92761b57ae106574aa9dda23d515e548ddaec966b51
7
+ data.tar.gz: 4864edc75a08d626e80e700ef0c5a9dcd86b2e1a4845bb3457e642423cc2098786ab4afc253cc42fbe97a4e2dbb3d10f970d712d496f70867d4641d711d320ec
@@ -0,0 +1,85 @@
1
+ require 'pstore'
2
+ require 'ostruct'
3
+
4
+ class QueryablePStore < PStore
5
+ class Query
6
+ attr_reader :attribute, :condition, :argument
7
+
8
+ def initialize(attribute, condition, argument)
9
+ @attribute = attribute
10
+ @condition = condition
11
+ @argument = argument
12
+ end
13
+
14
+ def filter(results)
15
+ if @attribute.empty?
16
+ handle_terminating_function(results)
17
+ else
18
+ results.select do |result|
19
+ conditional(result, @condition, @argument)
20
+ end
21
+ end
22
+ end
23
+
24
+ def handle_terminating_function(results)
25
+ case @condition
26
+ when :pluck
27
+ results.map { |r| r[@argument] }
28
+ else
29
+ raise "Unknown Terminating Function: #{@condition}"
30
+ end
31
+ end
32
+
33
+ def conditional(row, condition, argument)
34
+ thing_to_check = row[@attribute] # for direct comparison methods
35
+
36
+ case condition
37
+ when :gt
38
+ thing_to_check > argument
39
+ when :lt
40
+ thing_to_check < argument
41
+ when :eq
42
+ thing_to_check == argument
43
+ when :between
44
+ argument.include?(thing_to_check)
45
+ when :ilike
46
+ thing_to_check.downcase.include? argument.downcase
47
+ when :lambda
48
+ argument.call(OpenStruct.new(row)) # lambda takes the whole row
49
+ else
50
+ raise "Unknown Conditional: #{condition}"
51
+ end
52
+ end
53
+ end
54
+
55
+ ### QueryablePStore
56
+
57
+ def initialize(store_name)
58
+ super(store_name)
59
+ @queries = []
60
+ end
61
+
62
+ def records
63
+ transaction do
64
+ roots.map { |root| fetch(root) }
65
+ end
66
+ end
67
+
68
+ def method_missing(method, argument = nil, &blk)
69
+ attribute = method.to_s.split("_")[0..-2].join("_").to_sym
70
+ modifier = method.to_s.split("_")[-1].to_sym
71
+
72
+ @queries << Query.new(attribute, modifier, argument || blk)
73
+ self
74
+ end
75
+
76
+ def results
77
+ answer = @queries.inject(records) { |records, queryable| queryable.filter(records) }
78
+ @queries = []
79
+ answer
80
+ rescue StandardError => e
81
+ # In the event something bad happens, get us back to a good state without any queries hanging around
82
+ @queries = []
83
+ raise e
84
+ end
85
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: queryable_pstore
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jacob Smith
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-02-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 'This provides a very simple way of querying PStores. It allows you to
14
+ write queries like: `pstore.age_gt(40).height_lt(1.8).results` Additional documentation
15
+ is available on the GitHub page.'
16
+ email: jacob.wesley.smith@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/queryable_pstore.rb
22
+ homepage: http://rubygems.org/gems/queryable_pstore
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.6.8
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: A simple wrapper for making querying PStores easier.
46
+ test_files: []