Class: GreyscaleRecord::DataStore::Table

Inherits:
Object
  • Object
show all
Defined in:
lib/greyscale_record/data_store/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, store) ⇒ Table



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/greyscale_record/data_store/table.rb', line 6

def initialize(name, store)
  @name = name
  @store = store

  # initialize the index array for later use
  @indices = {}

  # generate IDs for the records based on YAML keys
  generate_ids!

  # preemptively index the IDs
  add_index :id
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/greyscale_record/data_store/table.rb', line 4

def name
  @name
end

Instance Method Details

#add_index(column) ⇒ Object



24
25
26
27
# File 'lib/greyscale_record/data_store/table.rb', line 24

def add_index( column )
  return if @store.patched?
  @indices = @indices.merge( { column => Index.new(column, rows) } )
end

#allObject



20
21
22
# File 'lib/greyscale_record/data_store/table.rb', line 20

def all
  rows.values
end

#find(params = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/greyscale_record/data_store/table.rb', line 29

def find( params = {} )
  return all if params.empty?
  sets = params.map do | column, values |
    if !patched? && indexed?( column )
      find_in_index column, values
    else
      GreyscaleRecord.logger.warn "You are running a query on #{@name}.#{column} which is not indexed. This will perform a table scan."
      find_in_column column, values
    end
  end

  sets.inject( sets.first ) do |result, subset|
    result & subset
  end
end