hecks-adapters-sql-database 0.1.16.rc → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,50 +1,57 @@
1
- module Hecks
2
- module Adapters
3
- class SQLDatabase
4
- module Commands
5
- class Create
6
- class FindOrCreateReferences
7
- attr_reader :reference_ids
8
-
9
- def initialize(head:, attributes:)
10
- @head = head
11
- @attributes = attributes
12
- @reference_ids = {}
13
- end
1
+ module HecksAdapters
2
+ class SQLDatabase
3
+ module Commands
4
+ class Create
5
+ # Find and update, or create Reference Rows
6
+ class FindOrCreateReferences
7
+ attr_reader :reference_ids
8
+
9
+ def initialize(head:, attributes:)
10
+ @head = head
11
+ @attributes = attributes
12
+ @reference_ids = {}
13
+ end
14
14
 
15
- def call
16
- find_or_create_reference
17
- find_or_create_references
18
- self
19
- end
15
+ def call
20
16
 
21
- private
17
+ find_or_create_reference
18
+ find_or_create_references
22
19
 
23
- def find_or_create_reference
24
- @head.references.each do |reference|
25
- next if reference.list?
26
- attributes = @attributes.delete(reference.name.to_sym)
27
- column = Column.factory(reference)
28
- result = DB[column.to_table_name].first(attributes)
20
+ self
21
+ end
29
22
 
30
- @reference_ids[reference.name] = result[:id] and return if result
31
- @reference_ids[reference.name] = DB[column.as_table_name].insert(attributes)
32
- end
23
+ private
24
+
25
+ def find_or_create_reference
26
+ @head.references.each do |reference|
27
+ next if reference.list?
28
+ attributes = @attributes.delete(reference.name.to_sym)
29
+ column = Column.factory(reference)
30
+ result = DB[column.to_table_name].first(attributes)
31
+
32
+ @reference_ids[reference.name] = result[:id] and return if result
33
+ id = SecureRandom.uuid
34
+ with_id = attributes.merge!(id: id)
35
+ DB[column.to_table_name].insert(with_id)
36
+ @reference_ids[reference.name] = id
33
37
  end
38
+ end
34
39
 
35
- def find_or_create_references
36
- @head.references.each do |reference|
37
- next unless reference.list?
38
- attributes = @attributes.delete(reference.name.to_sym)
39
- attributes.each do |attributes|
40
- @reference_ids[reference.name] = [] unless @reference_ids[reference.name]
40
+ def find_or_create_references
41
+ @head.references.each do |reference|
42
+ next unless reference.list?
43
+ attributes = @attributes.delete(reference.name.to_sym)
44
+ attributes.each do |attributes|
45
+ @reference_ids[reference.name] = [] unless @reference_ids[reference.name]
41
46
 
42
- column = Column.factory(reference)
43
- result = DB[column.to_table_name].first(attributes)
47
+ column = Column.factory(reference)
48
+ result = DB[column.to_table_name].first(attributes)
44
49
 
45
- @reference_ids[reference.name] << result[:id] and return if result
46
- @reference_ids[reference.name] << DB[column.to_table_name].insert(attributes)
47
- end
50
+ @reference_ids[reference.name] << result[:id] and return if result
51
+ id = SecureRandom.uuid
52
+ with_id = attributes.merge!(id: id)
53
+ DB[column.to_table_name].insert(with_id)
54
+ @reference_ids[reference.name] << id
48
55
  end
49
56
  end
50
57
  end
@@ -1,18 +1,17 @@
1
- module Hecks
2
- module Adapters
3
- class SQLDatabase
4
- module Commands
5
- class Delete
6
- def initialize(id:, head:)
7
- @id = id
8
- @head = head
9
- end
1
+ module HecksAdapters
2
+ class SQLDatabase
3
+ module Commands
4
+ # Delete a resource
5
+ class Delete
6
+ def initialize(id:, head:)
7
+ @id = id
8
+ @head = head
9
+ end
10
10
 
11
- def call
12
- table = Table.factory([@head]).first
13
- DB[table.name.to_sym].where(id: @id).delete
14
- self
15
- end
11
+ def call
12
+ table = Table.factory([@head]).first
13
+ DB[table.name.to_sym].where(id: @id).delete
14
+ self
16
15
  end
17
16
  end
18
17
  end
data/lib/commands/read.rb CHANGED
@@ -1,31 +1,30 @@
1
1
  require_relative 'read/fetch_references'
2
- module Hecks
3
- module Adapters
4
- class SQLDatabase
5
- module Commands
6
- class Read
7
- attr_reader :head, :entity, :table, :id
2
+ module HecksAdapters
3
+ class SQLDatabase
4
+ module Commands
5
+ # Read a resource
6
+ class Read
7
+ attr_reader :head, :entity, :table, :id
8
8
 
9
- def initialize(id:, head:, entity_class:)
10
- @head = head
11
- @table = Table.factory([@head]).first
12
- @id = id
13
- @entity_class = entity_class
14
- end
9
+ def initialize(id:, head:, entity_class:)
10
+ @head = head
11
+ @table = Table.factory([@head]).first
12
+ @id = id
13
+ @entity_class = entity_class
14
+ end
15
15
 
16
- def call
17
- fetch_entity
18
- return if @entity.nil?
19
- @entity_class.new(
20
- @entity.merge(FetchReferences.new(self).call.reference_map)
21
- )
22
- end
16
+ def call
17
+ fetch_entity
18
+ return if @entity.nil?
19
+ @entity_class.new(
20
+ @entity.merge(FetchReferences.new(self).call.reference_map)
21
+ )
22
+ end
23
23
 
24
- private
24
+ private
25
25
 
26
- def fetch_entity
27
- @entity = DB[@table.name.to_sym].first(id: @id)
28
- end
26
+ def fetch_entity
27
+ @entity = DB[@table.name.to_sym].first(id: @id)
29
28
  end
30
29
  end
31
30
  end
@@ -1,52 +1,52 @@
1
- module Hecks
2
- module Adapters
3
- class SQLDatabase
4
- module Commands
5
- class Read
6
- class FetchReferences
7
- attr_reader :reference_map
8
- def initialize(read_command)
9
- @head = read_command.head
10
- @reference_map = {}
11
- @entity = read_command.entity
12
- @table = read_command.table
13
- @id = read_command.id
14
- end
1
+ module HecksAdapters
2
+ class SQLDatabase
3
+ module Commands
4
+ class Read
5
+ # Look up any references so we can fill in relationship data on the
6
+ # object
7
+ class FetchReferences
8
+ attr_reader :reference_map
9
+ def initialize(read_command)
10
+ @head = read_command.head
11
+ @reference_map = {}
12
+ @entity = read_command.entity
13
+ @table = read_command.table
14
+ @id = read_command.id
15
+ end
15
16
 
16
- def call
17
- @head.references.each do |reference|
18
- fetch_reference(reference)
19
- fetch_references(reference)
20
- end
21
- self
17
+ def call
18
+ @head.references.each do |reference|
19
+ fetch_reference(reference)
20
+ fetch_references(reference)
22
21
  end
22
+ self
23
+ end
23
24
 
24
- private
25
+ private
25
26
 
26
- def fetch_reference(reference)
27
- return if reference.list?
28
- column = Column.factory(reference)
29
- value = DB[column.to_table_name].first(id: @entity[column.to_foreign_key])
27
+ def fetch_reference(reference)
28
+ return if reference.list?
29
+ column = Column.factory(reference)
30
+ value = DB[column.to_table_name].first(id: @entity[column.to_foreign_key])
30
31
 
31
- @entity.delete(column.to_foreign_key)
32
- value.delete(:id)
33
- @reference_map[column.name.to_sym] = value
34
- end
32
+ @entity.delete(column.to_foreign_key)
33
+ value.delete(:id)
34
+ @reference_map[column.name.to_sym] = value
35
+ end
35
36
 
36
- def fetch_references(reference)
37
- return unless reference.list?
37
+ def fetch_references(reference)
38
+ return unless reference.list?
38
39
 
39
- where_clause = {}
40
- where_clause[@table.to_foreign_key.to_sym] = @id
41
- column = Column.factory(reference)
40
+ where_clause = {}
41
+ where_clause[@table.to_foreign_key.to_sym] = @id
42
+ column = Column.factory(reference)
42
43
 
43
- @reference_map[column.name.to_sym] ||= []
44
+ @reference_map[column.name.to_sym] ||= []
44
45
 
45
- DB[JoinTable.new(@table, reference).name.to_sym].where(where_clause).map do |record|
46
- value_object = DB[column.to_table_name].first(id: record[column.to_foreign_key])
47
- value_object.delete(:id)
48
- @reference_map[column.name.to_sym] << value_object
49
- end
46
+ DB[JoinTable.new(@table, reference).name.to_sym].where(where_clause).map do |record|
47
+ value_object = DB[column.to_table_name].first(id: record[column.to_foreign_key])
48
+ value_object.delete(:id)
49
+ @reference_map[column.name.to_sym] << value_object
50
50
  end
51
51
  end
52
52
  end
@@ -3,42 +3,41 @@ require_relative 'update/link_to_references'
3
3
  require_relative 'update/create_new_value'
4
4
  require_relative 'update/delete_references'
5
5
 
6
- module Hecks
7
- module Adapters
8
- class SQLDatabase
9
- module Commands
10
- class Update
11
- attr_reader :id
6
+ module HecksAdapters
7
+ class SQLDatabase
8
+ module Commands
9
+ # Upate a resource
10
+ class Update
11
+ attr_reader :id
12
12
 
13
- def initialize(attributes:, head:, id:)
14
- @attributes = attributes.clone
15
- @references = head.references
16
- @head_table = Table.factory([head]).first
17
- @id = id
18
- end
13
+ def initialize(attributes:, head:, id:)
14
+ @attributes = attributes.clone
15
+ @references = head.references
16
+ @head_table = Table.factory([head]).first
17
+ @id = id
18
+ end
19
19
 
20
- def call
21
- DB.transaction do
22
- update_references
23
- fetch_record
24
- update_record
25
- end
26
- self
20
+ def call
21
+ DB.transaction do
22
+ update_references
23
+ fetch_record
24
+ update_record
27
25
  end
26
+ self
27
+ end
28
28
 
29
- private
29
+ private
30
30
 
31
- def update_references
32
- UpdateValues.new(@references, @attributes, @head_table, @id).call
33
- end
31
+ def update_references
32
+ UpdateValues.new(@references, @attributes, @head_table, @id).call
33
+ end
34
34
 
35
- def update_record
36
- @record.update(@attributes)
37
- end
35
+ def update_record
36
+ @record.update(@attributes)
37
+ end
38
38
 
39
- def fetch_record
40
- @record = DB[@head_table.name.to_sym].where(id: @id)
41
- end
39
+ def fetch_record
40
+ @record = DB[@head_table.name.to_sym].where(id: @id)
42
41
  end
43
42
  end
44
43
  end
@@ -1,39 +1,42 @@
1
- module Hecks
2
- module Adapters
3
- class SQLDatabase
4
- module Commands
5
- class Update
6
- class CreateNewValue
7
- attr_reader :reference_ids
1
+ module HecksAdapters
2
+ class SQLDatabase
3
+ module Commands
4
+ class Update
5
+ # Create any new values
6
+ class CreateNewValue
7
+ attr_reader :reference_ids
8
8
 
9
- def initialize(reference:, attributes:, reference_ids:)
10
- @reference = reference
11
- @attributes = attributes
12
- @reference_ids = {}
13
- @column = Column.factory(reference)
14
- @column_name = @column.name.to_sym
15
- end
9
+ def initialize(reference:, attributes:, reference_ids:)
10
+ @reference = reference
11
+ @attributes = attributes
12
+ @reference_ids = {}
13
+ @column = Column.factory(reference)
14
+ @column_name = @column.name.to_sym
15
+ end
16
16
 
17
- def call
18
- create_values
19
- create_value
20
- self
21
- end
17
+ def call
18
+ create_values
19
+ create_value
20
+ self
21
+ end
22
22
 
23
- private
23
+ private
24
24
 
25
- def create_values
26
- return unless @reference.list?
27
- @attributes[@column_name].each do |value|
28
- @reference_ids[@column_name] ||= []
29
- @reference_ids[@column_name] << DB[@column.to_table_name].insert(value)
30
- end
25
+ def create_values
26
+ return unless @reference.list?
27
+ @attributes[@column_name].each do |value|
28
+ @reference_ids[@column_name] ||= []
29
+ id = SecureRandom.uuid
30
+ DB[@column.to_table_name].insert(value.merge(id: id))
31
+ @reference_ids[@column_name] << id
31
32
  end
33
+ end
32
34
 
33
- def create_value
34
- return if @reference.list?
35
- @reference_ids[@reference.name] = DB[@column.to_table_name].insert(@attributes[@column.name.to_sym])
36
- end
35
+ def create_value
36
+ return if @reference.list?
37
+ id = SecureRandom.uuid
38
+ DB[@column.to_table_name].insert(@attributes[@column.name.to_sym].merge(id: id))
39
+ @reference_ids[@reference.name] = id
37
40
  end
38
41
  end
39
42
  end
@@ -1,40 +1,39 @@
1
- module Hecks
2
- module Adapters
3
- class SQLDatabase
4
- module Commands
5
- class Update
6
- class DeleteReferences
7
- attr_reader :attributes
1
+ module HecksAdapters
2
+ class SQLDatabase
3
+ module Commands
4
+ class Update
5
+ # Delete references to old values
6
+ class DeleteReferences
7
+ attr_reader :attributes
8
8
 
9
- def initialize(table:, attributes:, reference:)
10
- @table = table
11
- @attributes = attributes
12
- @reference = reference
13
- @where_clause = {}
14
- @dataset = DB[JoinTable.new(@table, @reference).name.to_sym]
15
- end
9
+ def initialize(table:, attributes:, reference:)
10
+ @table = table
11
+ @attributes = attributes
12
+ @reference = reference
13
+ @where_clause = {}
14
+ @dataset = DB[JoinTable.new(@table, @reference).name.to_sym]
15
+ end
16
16
 
17
- def call
18
- # build_where_clause
19
- delete_references
20
- remove_references_from_attributes
21
- self
22
- end
17
+ def call
18
+ # build_where_clause
19
+ delete_references
20
+ remove_references_from_attributes
21
+ self
22
+ end
23
23
 
24
- private
24
+ private
25
25
 
26
- def build_where_clause
27
- DB[JoinTable.new(@table, @reference).name.to_sym]
28
- end
26
+ def build_where_clause
27
+ DB[JoinTable.new(@table, @reference).name.to_sym]
28
+ end
29
29
 
30
- def remove_references_from_attributes
31
- @attributes.delete(@reference.name.to_sym)
32
- end
30
+ def remove_references_from_attributes
31
+ @attributes.delete(@reference.name.to_sym)
32
+ end
33
33
 
34
- def delete_references
35
- return unless @reference.list?
36
- @dataset.where(@where_clause).delete
37
- end
34
+ def delete_references
35
+ return unless @reference.list?
36
+ @dataset.where(@where_clause).delete
38
37
  end
39
38
  end
40
39
  end