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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/queryable_pstore.rb +12 -66
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 36d40cfb43fa8aa45f3ae2dc01b4a4b6a08c67a5
4
- data.tar.gz: 94afb9d4bc06e5196ddc7de2c606b0a155cd2d4d
3
+ metadata.gz: 6c5824998d9e38d575dd1dcf003ee15a3bcbb3a5
4
+ data.tar.gz: 21ac5f9b08055b31647061d83724d1b24b3b9341
5
5
  SHA512:
6
- metadata.gz: 6abd1167c469f92a577be7a85b2cd5c8fa076f88e9a6a4ed9627000adfd594a9c7b0f99b4f2c3a043c478250d856f29e72bd097b33b51b5dd8ec02a020dea947
7
- data.tar.gz: b589b315454b6a7a70bdd60584b805b89cb6ef7d6098e510e67941af91edc0c3eb9ac145977e5a5ccc19193a910f58cd4767bd3b24aecd5b4840e31f4a09d69f
6
+ metadata.gz: 911b154063bca79eefc44859cbfa7214d5d16dbf120e7c4d15d9671f9e67604773fc15e9a8aa9b06979ddfb8e9789203821bd70c64a85c21c86813c71c1316f3
7
+ data.tar.gz: 5e32dfea85ab83ca7e8c72e9f2c1b5a9883553bc928cd57f5cb7fc9079182adab0e6450fcbd42aa5210e59e95cc1c39409cf909163504bf87e6df6e9ea33b3e6
@@ -1,78 +1,24 @@
1
1
  require 'pstore'
2
2
  require 'ostruct'
3
- require 'pry'
3
+ require 'securerandom'
4
+ require_relative 'query'
5
+ require_relative 'csv_converter'
4
6
 
5
7
  class QueryablePStore < PStore
6
- class Query
7
- attr_reader :attribute, :condition, :argument
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
- def initialize(attribute, condition, argument)
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
- def filter(results)
35
- if @attribute.empty?
36
- handle_terminating_function(results)
37
- else
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)
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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacob Smith