Class: Foobara::Persistence::CrudDrivers::InMemoryMinimal::Table

Inherits:
EntityAttributesCrudDriver::Table show all
Defined in:
foobara-0.0.110/projects/in_memory_crud_driver_minimal/src/in_memory_minimal.rb

Instance Attribute Summary collapse

Attributes inherited from EntityAttributesCrudDriver::Table

#crud_driver, #entity_class, #raw_connection, #table_name

Instance Method Summary collapse

Methods inherited from EntityAttributesCrudDriver::Table

#all_exist?, #exists?, #find_all_by_attribute_any_of, #find_all_by_attribute_containing_any_of, #find_by, #find_by_attribute_containing, #find_many!, #find_many_by, #first, #hard_delete_all!, #hard_delete_many, #insert_many, #matches_attributes_filter?, #normalize_attribute_filter_value, #primary_key_attribute, #record_id_for, #select

Constructor Details

#initializeTable

Returns a new instance of Table.



8
9
10
11
12
13
# File 'foobara-0.0.110/projects/in_memory_crud_driver_minimal/src/in_memory_minimal.rb', line 8

def initialize(...)
  @last_id = 0
  self.records = {}

  super
end

Instance Attribute Details

#recordsObject

Returns the value of attribute records.



6
7
8
# File 'foobara-0.0.110/projects/in_memory_crud_driver_minimal/src/in_memory_minimal.rb', line 6

def records
  @records
end

Instance Method Details

#allObject

CRUD TODO: all multiple record methods should return enumerators and code further up should only use the lazy enumerator interface… to encourage that/catch bugs we will return lazy enumerators in these built-in crud drivers



23
24
25
# File 'foobara-0.0.110/projects/in_memory_crud_driver_minimal/src/in_memory_minimal.rb', line 23

def all
  records.each_value.lazy
end

#countObject



27
28
29
# File 'foobara-0.0.110/projects/in_memory_crud_driver_minimal/src/in_memory_minimal.rb', line 27

def count
  records.count
end

#find(record_id) ⇒ Object



31
32
33
# File 'foobara-0.0.110/projects/in_memory_crud_driver_minimal/src/in_memory_minimal.rb', line 31

def find(record_id)
  Util.deep_dup(records[record_id])
end

#find!(record_id) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'foobara-0.0.110/projects/in_memory_crud_driver_minimal/src/in_memory_minimal.rb', line 35

def find!(record_id)
  attributes = find(record_id)

  unless attributes
    raise CannotFindError.new(record_id, "does not exist")
  end

  attributes
end

#get_idObject



15
16
17
# File 'foobara-0.0.110/projects/in_memory_crud_driver_minimal/src/in_memory_minimal.rb', line 15

def get_id
  @last_id += 1
end

#hard_delete(record_id) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'foobara-0.0.110/projects/in_memory_crud_driver_minimal/src/in_memory_minimal.rb', line 76

def hard_delete(record_id)
  unless exists?(record_id)
    # :nocov:
    raise CannotUpdateError.new(record_id, "does not exist")
    # :nocov:
  end

  records.delete(record_id)
end

#hard_delete_allObject



86
87
88
# File 'foobara-0.0.110/projects/in_memory_crud_driver_minimal/src/in_memory_minimal.rb', line 86

def hard_delete_all
  self.records = {}
end

#insert(attributes) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'foobara-0.0.110/projects/in_memory_crud_driver_minimal/src/in_memory_minimal.rb', line 45

def insert(attributes)
  attributes = Util.deep_dup(attributes)

  record_id = record_id_for(attributes)

  if record_id
    if exists?(record_id)
      raise CannotInsertError.new(record_id, "already exists")
    end
  else
    record_id = get_id
    attributes.merge!(primary_key_attribute => record_id)
  end

  records[record_id] = attributes
  find(record_id)
end

#update(attributes) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
# File 'foobara-0.0.110/projects/in_memory_crud_driver_minimal/src/in_memory_minimal.rb', line 63

def update(attributes)
  record_id = record_id_for(attributes)

  unless exists?(record_id)
    # :nocov:
    raise CannotUpdateError.new(record_id, "does not exist")
    # :nocov:
  end

  records[record_id] = Util.deep_dup(attributes)
  find(record_id)
end