queryable_pstore 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/csv_converter.rb +20 -0
  3. data/lib/query.rb +68 -0
  4. metadata +3 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6c5824998d9e38d575dd1dcf003ee15a3bcbb3a5
4
- data.tar.gz: 21ac5f9b08055b31647061d83724d1b24b3b9341
3
+ metadata.gz: f5720955066adad1544e426f2f2294e8be6a7583
4
+ data.tar.gz: 7c4efd3bbecd436764f2e7ffd67edb8d0b9f8a8c
5
5
  SHA512:
6
- metadata.gz: 911b154063bca79eefc44859cbfa7214d5d16dbf120e7c4d15d9671f9e67604773fc15e9a8aa9b06979ddfb8e9789203821bd70c64a85c21c86813c71c1316f3
7
- data.tar.gz: 5e32dfea85ab83ca7e8c72e9f2c1b5a9883553bc928cd57f5cb7fc9079182adab0e6450fcbd42aa5210e59e95cc1c39409cf909163504bf87e6df6e9ea33b3e6
6
+ metadata.gz: 3f5165b82bac9c9abb2605ad517241df8a47b86d8c13bfce626f4211bfb821c3cbd64b3cd2a461dcb208e46df8757e9557749fc52e88d2d770cf819507e6616d
7
+ data.tar.gz: e2e59afff0482a82d3831484ca12d3759239702a80af0401b7c57a7ac95d262009e6446add04ca7d606800be54ba17e20d0142f43f76c7977860cf042aa49f6e
@@ -0,0 +1,20 @@
1
+ class CSVConverter
2
+ def initialize(convert)
3
+ @key = convert.keys.first
4
+ @conversion = convert[@key]
5
+ end
6
+
7
+ def convert(hash)
8
+ case @conversion
9
+ when :integer
10
+ hash[@key] = hash[@key].to_i
11
+ when :float
12
+ hash[@key] = hash[@key].to_f
13
+ else
14
+ raise ArgumentError.new("Unknown converter: `#{@conversion}`")
15
+ end
16
+
17
+ hash
18
+ end
19
+
20
+ end
data/lib/query.rb ADDED
@@ -0,0 +1,68 @@
1
+ class Query
2
+ attr_reader :attribute, :condition, :argument
3
+ TERMINATING_FUNCTIONS = [:pluck]
4
+
5
+ def initialize(attribute, condition, argument)
6
+ @attribute = attribute
7
+ @condition = condition
8
+ @argument = argument
9
+ end
10
+
11
+ def valid?(records)
12
+ valid = attribute_present?(records) || argument_is_block? || terminating_function?
13
+ raise ArgumentError.new("The attribute `#{@attribute}` is not present in the PStore.") unless valid
14
+ valid
15
+ end
16
+
17
+ def attribute_present?(records)
18
+ records.any? { |record| record.keys.include? @attribute }
19
+ end
20
+
21
+ def argument_is_block?
22
+ @argument.respond_to?(:call)
23
+ end
24
+
25
+ def terminating_function?
26
+ TERMINATING_FUNCTIONS.include?(@condition)
27
+ end
28
+
29
+ def filter(results)
30
+ if @attribute.empty?
31
+ handle_terminating_function(results)
32
+ else
33
+ results.select do |result|
34
+ conditional(result, @condition, @argument)
35
+ end
36
+ end
37
+ end
38
+
39
+ def handle_terminating_function(results)
40
+ case @condition
41
+ when :pluck
42
+ results.map { |r| r[@argument] }
43
+ else
44
+ raise "Unknown Terminating Function: #{@condition}"
45
+ end
46
+ end
47
+
48
+ def conditional(row, condition, argument)
49
+ thing_to_check = row[@attribute] # for direct comparison methods
50
+
51
+ case condition
52
+ when :gt
53
+ thing_to_check > argument
54
+ when :lt
55
+ thing_to_check < argument
56
+ when :eq
57
+ thing_to_check == argument
58
+ when :between
59
+ argument.include?(thing_to_check)
60
+ when :ilike
61
+ thing_to_check.downcase.include? argument.downcase
62
+ when :lambda
63
+ argument.call(OpenStruct.new(row)) # lambda takes the whole row
64
+ else
65
+ raise "Unknown Conditional: #{condition}"
66
+ end
67
+ end
68
+ end
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.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacob Smith
@@ -18,6 +18,8 @@ executables: []
18
18
  extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
+ - lib/csv_converter.rb
22
+ - lib/query.rb
21
23
  - lib/queryable_pstore.rb
22
24
  homepage: https://github.com/jacobsmith/queryable_pstore
23
25
  licenses: