testingrecord 0.8 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63ee9271871c1194e05e12d7f4d5d23bc744f703461b8519d45300f180f5432c
4
- data.tar.gz: 251d286e38df1ed55a6e25f40c615ca7ccc1673c50758ab0bec861eabc354da7
3
+ metadata.gz: 228079a525e184348661a215f33fcae6daf628c54592793343f41525b15ea717
4
+ data.tar.gz: f95a3e63f583936668b4ccebe7eae1186605f47e53895fd2e72e1019955b05e1
5
5
  SHA512:
6
- metadata.gz: 45cc639fbc3e7d7ca82c4d08ad4a867f7ff550a78738de717ca07ae7b3fa5e6bf7b9991f07bc745690eab33db0e2a39a58cb8f1fb61f259567280651941630bb
7
- data.tar.gz: 68975a0f8c4efbe1ed14aab9e47f0809c6f58377c5f587aa20c5192847869ff15becb145356d06a1a8fd78c1a3257e43ee85184806f91a40ed71eda331ba978c
6
+ metadata.gz: b8c1e5f2a892e41ee591422a9febeca1e52c8a6043f3010b564496f072bb0cb5183f67d16f5afcc58d16655f8a179f00271ccf168124d7c3d0bddec3471a0823
7
+ data.tar.gz: 2d1ffd7be42bed6f2a483d5adb46cf1cfec4c465f0b62ff805ca0a1a0280248e7984c1a3220609d13c686c307fcf020c466c2ce087164451c8eede24a471d006
@@ -36,6 +36,21 @@ module TestingRecord
36
36
  find_by({ id: })&.first&.tap { |entity| entity.class.current = entity }
37
37
  end
38
38
 
39
+ # Checks to see whether an entity exists with the provided primary_key
40
+ #
41
+ # @return [Boolean]
42
+ def with_primary_key?(primary_key)
43
+ !with_primary_key(primary_key).nil?
44
+ end
45
+
46
+ # Finds an entity with the provided primary_key
47
+ # If one is found, set it as the current entity
48
+ #
49
+ # @return [TestingRecord::Model, nil]
50
+ def with_primary_key(primary_key)
51
+ find_by({ __primary_key => primary_key })&.first&.tap { |entity| entity.class.current = entity }
52
+ end
53
+
39
54
  private
40
55
 
41
56
  # Finds all entities that match specified attribute values
@@ -10,13 +10,21 @@ module TestingRecord
10
10
  module Settings
11
11
  include DSL::Validation::Input
12
12
 
13
- attr_reader :__primary_key
13
+ def __primary_key
14
+ if instance_variable_defined?(:@__primary_key)
15
+ @__primary_key
16
+ elsif superclass.respond_to?(:__primary_key)
17
+ superclass.__primary_key
18
+ else
19
+ TestingRecord.default_primary_key
20
+ end
21
+ end
14
22
 
15
23
  # Create a cache of the entities, named according to the classname
16
24
  #
17
25
  # @return [Symbol]
18
26
  def caching(option)
19
- raise Error, 'Invalid caching option, must be :enabled or :disabled' unless caching_valid?(option)
27
+ raise Error::InvalidConfigurationError, 'Invalid caching option, must be :enabled or :disabled' unless caching_valid?(option)
20
28
  return unless option == :enabled
21
29
 
22
30
  instance_variable_set(:@all, [])
@@ -28,7 +36,9 @@ module TestingRecord
28
36
  #
29
37
  # @return [Symbol]
30
38
  def primary_key(option)
31
- instance_variable_set(:@__primary_key, option.to_sym)
39
+ raise Error::InvalidConfigurationError, 'Invalid primary key value, must be a Symbol' unless option.is_a?(Symbol)
40
+
41
+ instance_variable_set(:@__primary_key, option)
32
42
  end
33
43
  end
34
44
  end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TestingRecord
4
+ module Error
5
+ class AttributeError < StandardError; end
6
+ class EntityError < StandardError; end
7
+ class InvalidConfigurationError < StandardError; end
8
+ end
9
+ end
@@ -17,19 +17,25 @@ module TestingRecord
17
17
  attr_reader :current
18
18
 
19
19
  # Creates an instance of the model
20
- # -> Creating iVar values for each attribute that was provided
21
- # -> Adding it to the cache if caching is enabled
22
- # -> Keeping a track of all originally supplied attributes in symbolized format in the `attributes` iVar
20
+ # -> Ensures that the primary key is specified in the attribute payload
21
+ # -> Validates that a duplicate entity has not been made (If caching is enabled)
22
+ # -> Creates iVar values (Symbol format) and attr_reader's for each attribute that was provided
23
+ # -> Adds helper methods (If the model has been configured to include helpers)
24
+ # -> Adds it to the cache (If caching is enabled)
23
25
  #
24
26
  # @return [TestingRecord::Model]
25
27
  def create(attributes)
26
- new(attributes.transform_keys(&:to_sym)).tap do |entity|
27
- configure_data(entity, attributes)
28
- add_helpers(attributes) if entity.class.instance_variable_get(:@include_helpers)
29
- cache_entity(entity)
28
+ attributes.transform_keys!(&:to_sym)
29
+ if respond_to?(:all)
30
+ create_with_caching(attributes)
31
+ else
32
+ create_without_caching(attributes)
30
33
  end
31
34
  end
32
35
 
36
+ # Sets the current entity instance to the one supplied (Or removes it if supplied `nil`)
37
+ #
38
+ # @return [TestingRecord::Model, nil]
33
39
  def current=(entity)
34
40
  if entity
35
41
  TestingRecord.logger.info("Switching current user from #{@current} to #{entity}")
@@ -41,23 +47,42 @@ module TestingRecord
41
47
 
42
48
  # Deletes the instance of the model from the cache (Does nothing if caching is disabled)
43
49
  #
44
- # @return [TestingRecord::Model]
50
+ # @return [TestingRecord::Model, nil]
45
51
  def delete(entity)
46
- all.delete(entity) if respond_to?(:all)
52
+ return unless respond_to?(:all)
53
+
54
+ self.current = nil if entity == current
55
+ all.delete(entity)
47
56
  end
48
57
 
49
58
  # Deletes the instance of the model from the cache (Does nothing if caching is disabled)
50
59
  #
51
- # @return [TestingRecord::Model]
60
+ # @return [TestingRecord::Model, nil]
52
61
  def delete_by_id(id)
53
62
  delete(with_id(id)) if respond_to?(:all)
54
63
  end
55
64
 
56
65
  private
57
66
 
58
- def cache_entity(entity)
59
- return unless respond_to?(:all)
67
+ def create_with_caching(attributes)
68
+ ensure_primary_key_presence(attributes)
69
+ ensure_deduplication(attributes)
70
+ new(attributes).tap do |entity|
71
+ configure_data(entity, attributes)
72
+ add_helpers(attributes) if entity.class.instance_variable_get(:@include_helpers)
73
+ cache_entity(entity)
74
+ end
75
+ end
76
+
77
+ def create_without_caching(attributes)
78
+ ensure_primary_key_presence(attributes)
79
+ new(attributes).tap do |entity|
80
+ configure_data(entity, attributes)
81
+ add_helpers(attributes) if entity.class.instance_variable_get(:@include_helpers)
82
+ end
83
+ end
60
84
 
85
+ def cache_entity(entity)
61
86
  self.current = entity
62
87
  all << entity
63
88
  TestingRecord.logger.debug("Entity: #{entity} added to cache")
@@ -69,6 +94,20 @@ module TestingRecord
69
94
  entity.class.attr_reader attribute_key
70
95
  end
71
96
  end
97
+
98
+ def ensure_deduplication(attributes)
99
+ pk_value = attributes[__primary_key]
100
+ return unless with_primary_key?(pk_value)
101
+
102
+ TestingRecord.logger.error("#{name} entity already exists with primary key: #{pk_value}")
103
+ raise Error::EntityError, "#{name} entity already exists with primary key: #{pk_value}"
104
+ end
105
+
106
+ def ensure_primary_key_presence(attributes)
107
+ return if attributes.key?(__primary_key)
108
+
109
+ raise Error::AttributeError, "#{name} entity has not been supplied with the primary key: #{__primary_key}"
110
+ end
72
111
  end
73
112
 
74
113
  def initialize(attributes = {})
@@ -107,7 +146,6 @@ module TestingRecord
107
146
  private
108
147
 
109
148
  def reorder_attributes_for_inspect!
110
- return unless self.class.__primary_key
111
149
  return if attributes.keys.first == self.class.__primary_key
112
150
 
113
151
  pk_value = attributes.delete(self.class.__primary_key)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TestingRecord
4
- VERSION = '0.8'
4
+ VERSION = '1.0.1'
5
5
  end
@@ -1,16 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'testing_record/dsl'
4
+ require_relative 'testing_record/error'
4
5
  require_relative 'testing_record/logger'
5
6
  require_relative 'testing_record/model'
6
7
  require_relative 'testing_record/version'
7
8
 
8
9
  # {TestingRecord} namespace
9
10
  module TestingRecord
10
- # Generic TestingRecord error. Will be extended in the future
11
- class Error < StandardError; end
12
-
13
11
  class << self
12
+ attr_reader :default_primary_key
13
+
14
+ def configure
15
+ yield self
16
+ end
17
+
18
+ # Configure a default primary key for all TestingRecord models
19
+ def default_primary_key=(value)
20
+ raise InvalidConfigurationError, 'Invalid primary key value, must be a Symbol' unless value.is_a?(Symbol)
21
+
22
+ @default_primary_key = value
23
+ TestingRecord::Model.primary_key value
24
+ end
25
+
26
+ # Specify the default primary key to `:id` (This is procedural so will be overwritten at runtime if config defined)
27
+ TestingRecord.default_primary_key = :id
28
+
14
29
  # The Testing Record logger object - This is called automatically in several
15
30
  # locations and will log messages according to the normal Ruby protocol
16
31
  #
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: testingrecord
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.8'
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luke Hill
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-03-10 00:00:00.000000000 Z
11
+ date: 2026-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: automation_helpers
@@ -64,14 +64,14 @@ dependencies:
64
64
  requirements:
65
65
  - - "~>"
66
66
  - !ruby/object:Gem::Version
67
- version: 1.85.1
67
+ version: 1.86.0
68
68
  type: :development
69
69
  prerelease: false
70
70
  version_requirements: !ruby/object:Gem::Requirement
71
71
  requirements:
72
72
  - - "~>"
73
73
  - !ruby/object:Gem::Version
74
- version: 1.85.1
74
+ version: 1.86.0
75
75
  - !ruby/object:Gem::Dependency
76
76
  name: rubocop-performance
77
77
  requirement: !ruby/object:Gem::Requirement
@@ -119,6 +119,7 @@ files:
119
119
  - lib/testing_record/dsl/builder/settings.rb
120
120
  - lib/testing_record/dsl/validation.rb
121
121
  - lib/testing_record/dsl/validation/input.rb
122
+ - lib/testing_record/error.rb
122
123
  - lib/testing_record/logger.rb
123
124
  - lib/testing_record/model.rb
124
125
  - lib/testing_record/version.rb