Class: Dupe::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/superdupe/record.rb,
lib/superdupe/database.rb

Overview

:nodoc:

Defined Under Namespace

Classes: InvalidQueryError, Record, TableDoesNotExistError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabase

by default, there are not tables in the database



12
13
14
# File 'lib/superdupe/database.rb', line 12

def initialize
  @tables = {}
end

Instance Attribute Details

#tablesObject (readonly)

Returns the value of attribute tables.



3
4
5
# File 'lib/superdupe/database.rb', line 3

def tables
  @tables
end

Instance Method Details

#create_table(model_name) ⇒ Object



59
60
61
# File 'lib/superdupe/database.rb', line 59

def create_table(model_name)
  @tables[model_name.to_sym] ||= []
end

#delete(resource_name, conditions = nil) ⇒ Object

database.delete :books # –> would delete all books database.delete :book # –> would delete the first book record found database.delete :book, proc {|b| b.id < 10} # –> would delete all books found who’s id is less than 10 database.delete :books, proc {|b| b.id < 10} # –> would delete all books found who’s id is less than 10

Raises:

  • (StandardError)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/superdupe/database.rb', line 20

def delete(resource_name, conditions=nil)
  model_name = resource_name.plural? ? resource_name.to_s.singularize.to_sym : resource_name.to_s.to_sym
  raise StandardError, "Invalid DELETE operation: The resource #{model_name} has not been defined" unless @tables[model_name]
  
  if conditions
    @tables[model_name].reject!(&conditions) 
  elsif resource_name.singular?
    @tables[model_name].shift
  elsif resource_name.plural?
    @tables[model_name] = []
  end
end

#insert(record) ⇒ Object

pass in a Dupe::Database::Record object, and this method will store the record in the appropriate table



35
36
37
38
39
40
41
42
43
# File 'lib/superdupe/database.rb', line 35

def insert(record)
  if !record.kind_of?(Dupe::Database::Record) || !record.__model__
    raise ArgumentError, "You may only insert well-defined Dupe::Database::Record objects" 
  end
  record[:id] ||= (@tables[record.__model__.name].blank? ? 1 : @tables[record.__model__.name].map {|r| r.id}.last + 1)
  @tables[record.__model__.name] ||= []
  @tables[record.__model__.name] << record
  record.__model__.run_after_create_callbacks(record)
end

#select(model_name, conditions = nil) ⇒ Object

pass in a model_name (e.g., :book) and optionally a proc with conditions (e.g., {|b| b.genre == ‘Science Fiction’}) and recieve a (possibly empty) array of results



48
49
50
51
52
53
54
55
56
57
# File 'lib/superdupe/database.rb', line 48

def select(model_name, conditions=nil)
  raise TableDoesNotExistError, "The table ':#{model_name}' does not exist." unless @tables[model_name]
  raise(
    InvalidQueryError, 
    "There was a problem with your select conditions. Please consult the API."
  ) if conditions and (!conditions.kind_of?(Proc) || conditions.arity != 1)
  
  return @tables[model_name] if !conditions
  @tables[model_name].select {|r| conditions.call(r)}
end

#truncate_tablesObject



63
64
65
66
67
# File 'lib/superdupe/database.rb', line 63

def truncate_tables
  @tables.each do |table_name, table_records|
    @tables[table_name] = []
  end
end