queryable_pstore 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/queryable_pstore.rb +12 -66
- 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: 6c5824998d9e38d575dd1dcf003ee15a3bcbb3a5
|
4
|
+
data.tar.gz: 21ac5f9b08055b31647061d83724d1b24b3b9341
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 911b154063bca79eefc44859cbfa7214d5d16dbf120e7c4d15d9671f9e67604773fc15e9a8aa9b06979ddfb8e9789203821bd70c64a85c21c86813c71c1316f3
|
7
|
+
data.tar.gz: 5e32dfea85ab83ca7e8c72e9f2c1b5a9883553bc928cd57f5cb7fc9079182adab0e6450fcbd42aa5210e59e95cc1c39409cf909163504bf87e6df6e9ea33b3e6
|
data/lib/queryable_pstore.rb
CHANGED
@@ -1,78 +1,24 @@
|
|
1
1
|
require 'pstore'
|
2
2
|
require 'ostruct'
|
3
|
-
require '
|
3
|
+
require 'securerandom'
|
4
|
+
require_relative 'query'
|
5
|
+
require_relative 'csv_converter'
|
4
6
|
|
5
7
|
class QueryablePStore < PStore
|
6
|
-
|
7
|
-
|
8
|
-
TERMINATING_FUNCTIONS = [:pluck]
|
8
|
+
def self.import_csv(filename, opts = {})
|
9
|
+
csv_converters = opts.fetch(:convert, []).map { |conversion| CSVConverter.new(conversion) }
|
9
10
|
|
10
|
-
|
11
|
-
@attribute = attribute
|
12
|
-
@condition = condition
|
13
|
-
@argument = argument
|
14
|
-
end
|
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
|
11
|
+
csv = CSV.read(File.open(filename), headers: true, header_converters: -> (header) { header.downcase.to_sym })
|
33
12
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
results.select do |result|
|
39
|
-
conditional(result, @condition, @argument)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
def handle_terminating_function(results)
|
45
|
-
case @condition
|
46
|
-
when :pluck
|
47
|
-
results.map { |r| r[@argument] }
|
48
|
-
else
|
49
|
-
raise "Unknown Terminating Function: #{@condition}"
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def conditional(row, condition, argument)
|
54
|
-
thing_to_check = row[@attribute] # for direct comparison methods
|
55
|
-
|
56
|
-
case condition
|
57
|
-
when :gt
|
58
|
-
thing_to_check > argument
|
59
|
-
when :lt
|
60
|
-
thing_to_check < argument
|
61
|
-
when :eq
|
62
|
-
thing_to_check == argument
|
63
|
-
when :between
|
64
|
-
argument.include?(thing_to_check)
|
65
|
-
when :ilike
|
66
|
-
thing_to_check.downcase.include? argument.downcase
|
67
|
-
when :lambda
|
68
|
-
argument.call(OpenStruct.new(row)) # lambda takes the whole row
|
69
|
-
else
|
70
|
-
raise "Unknown Conditional: #{condition}"
|
13
|
+
store = new("#{filename}.pstore")
|
14
|
+
store.transaction do
|
15
|
+
csv.each do |row|
|
16
|
+
store[SecureRandom.uuid] = csv_converters.inject(row.to_h) { |hash, converter| converter.convert(hash) }
|
71
17
|
end
|
72
18
|
end
|
19
|
+
|
20
|
+
store
|
73
21
|
end
|
74
|
-
|
75
|
-
### QueryablePStore
|
76
22
|
|
77
23
|
def initialize(store_name)
|
78
24
|
super(store_name)
|