Class: QueryablePStore

Inherits:
PStore
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/queryable_pstore.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store_name) ⇒ QueryablePStore

Returns a new instance of QueryablePStore.



31
32
33
34
35
# File 'lib/queryable_pstore.rb', line 31

def initialize(store_name)
  FileUtils.mkdir_p(File.dirname(store_name)) # create the directory if it doesn't exist where we are saving the file
  super(store_name)
  @queries = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, argument = nil, &blk) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/queryable_pstore.rb', line 43

def method_missing(method, argument = nil, &blk)
  attribute = method.to_s.split("_")[0..-2].join("_").to_sym
  modifier = method.to_s.split("_")[-1].to_sym

  query = Query.new(attribute, modifier, argument || blk)
  @queries << query if query.valid?(records)
  self
end

Instance Attribute Details

#csv_importerObject

Returns the value of attribute csv_importer.



27
28
29
# File 'lib/queryable_pstore.rb', line 27

def csv_importer
  @csv_importer
end

Class Method Details

.import_csv(filename, opts = {}) ⇒ Object



13
14
15
# File 'lib/queryable_pstore.rb', line 13

def import_csv(filename, opts = {})
  CSVImporter.new.import_csv(filename, opts)
end

.import_csv_from_string(string, opts = {}) ⇒ Object



17
18
19
# File 'lib/queryable_pstore.rb', line 17

def import_csv_from_string(string, opts = {})
  CSVImporter.new.import_csv_from_string(string, opts)
end

.queryable_header(header) ⇒ Object



21
22
23
# File 'lib/queryable_pstore.rb', line 21

def queryable_header(header)
  CSVImporter.new.convert_header_to_methodable_name(header)
end

Instance Method Details

#recordsObject



37
38
39
40
41
# File 'lib/queryable_pstore.rb', line 37

def records
  transaction do
    roots.map { |root| fetch(root) }
  end
end

#resultsObject



52
53
54
55
56
57
58
59
60
# File 'lib/queryable_pstore.rb', line 52

def results
  answer = @queries.inject(records) { |records, queryable| queryable.filter(records) }
  @queries = []
  answer
rescue StandardError => e
  # In the event something bad happens, get us back to a good state without any queries hanging around
  @queries = []
  raise e
end