testingrecord 1.0.1 → 1.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: 228079a525e184348661a215f33fcae6daf628c54592793343f41525b15ea717
4
- data.tar.gz: f95a3e63f583936668b4ccebe7eae1186605f47e53895fd2e72e1019955b05e1
3
+ metadata.gz: 72145033dd89e4249cf60d2b07eca3020e335e43134ea1ff02a0be99b626b29f
4
+ data.tar.gz: d8e459e3e0a03ca7ce79daaa2bccd66d2d9d682f4fa088c046725df48643ec00
5
5
  SHA512:
6
- metadata.gz: b8c1e5f2a892e41ee591422a9febeca1e52c8a6043f3010b564496f072bb0cb5183f67d16f5afcc58d16655f8a179f00271ccf168124d7c3d0bddec3471a0823
7
- data.tar.gz: 2d1ffd7be42bed6f2a483d5adb46cf1cfec4c465f0b62ff805ca0a1a0280248e7984c1a3220609d13c686c307fcf020c466c2ce087164451c8eede24a471d006
6
+ metadata.gz: 98350263e6164ed2711ab2e065d39805f404b42de3fb74f31cbc4d5440eda3125d784cdb0e1f8cac72290679bb9032651beeddfab7b91fa6a47d29d80a30f20e
7
+ data.tar.gz: 8d06c3d1c6a1f8bee5a3d48a51c7be36cc6d1c0e0a8afd1325fd202346abb70a3f3f7c01abe857d3a06efef2e2487746c5ecc1a7d68bdebbeef8ab675bc060f8
@@ -6,6 +6,8 @@ module TestingRecord
6
6
  # [TestingRecord::DSL::Builder::Filters]
7
7
  # Ways in which we can filter our collection to find specific models
8
8
  module Filters
9
+ include DSL::Validation::Input
10
+
9
11
  # Checks to see whether an entity exists with the provided attributes
10
12
  #
11
13
  # @return [Boolean]
@@ -13,12 +15,31 @@ module TestingRecord
13
15
  find_by(attributes).any?
14
16
  end
15
17
 
18
+ # Finds all entities that match specified attribute values
19
+ # attributes (Hash) -> The attributes you wish to filter on, each is iterated through sequentially
20
+ # :logic (Symbol) -> Whether to use `and` (Intersection), or `or` (Union), logic to combine each key in attributes hash
21
+ #
22
+ # @return [Array<TestingRecord::Model>]
23
+ def find_by(attributes, logic: :and)
24
+ raise Error::InvalidArgumentError, 'Invalid filtering logic option, must be `:and` or `:or`' unless filter_logic_valid?(logic)
25
+
26
+ TestingRecord.logger.debug("Filtering Entity: '#{self}' list by #{attributes}. Logic: '#{logic}'")
27
+ if logic == :and
28
+ find_by_and(attributes)
29
+ else
30
+ find_by_or(attributes)
31
+ end
32
+ end
33
+
16
34
  # Finds an entity with the provided email address
17
35
  # If one is found, set it as the current entity
18
36
  #
19
37
  # @return [TestingRecord::Model, nil]
20
38
  def with_email(email_address)
21
- find_by({ email_address: })&.first&.tap { |entity| entity.class.current = entity }
39
+ find_by(
40
+ { email_address: email_address, email: email_address, 'email-address': email_address },
41
+ logic: :or
42
+ )&.first&.tap { |entity| entity.class.current = entity }
22
43
  end
23
44
 
24
45
  # Checks to see whether an entity exists with the provided id
@@ -53,17 +74,20 @@ module TestingRecord
53
74
 
54
75
  private
55
76
 
56
- # Finds all entities that match specified attribute values
57
- #
58
- # @return [Array<TestingRecord::Model>]
59
- def find_by(attributes)
60
- pool = all
61
- attributes.each do |key, value|
62
- TestingRecord.logger.debug("Current user pool size: #{pool.length}")
63
- TestingRecord.logger.debug("Filtering User list by #{key}: #{value}")
64
- pool = pool.select { |entity| entity.attributes[key] == value }
77
+ def find_by_and(attributes)
78
+ all.select do |entity|
79
+ attributes.all? do |key, value|
80
+ entity.attributes[key] == value
81
+ end
82
+ end
83
+ end
84
+
85
+ def find_by_or(attributes)
86
+ all.select do |entity|
87
+ attributes.any? do |key, value|
88
+ entity.attributes[key] == value
89
+ end
65
90
  end
66
- pool
67
91
  end
68
92
  end
69
93
  end
@@ -24,7 +24,7 @@ module TestingRecord
24
24
  #
25
25
  # @return [Symbol]
26
26
  def caching(option)
27
- raise Error::InvalidConfigurationError, '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)
28
28
  return unless option == :enabled
29
29
 
30
30
  instance_variable_set(:@all, [])
@@ -10,12 +10,12 @@ module TestingRecord
10
10
  #
11
11
  # @return [Boolean]
12
12
  def caching_valid?(input)
13
- enabled_or_disabled.include?(input)
13
+ %i[enabled disabled].include?(input)
14
14
  end
15
15
 
16
- private
17
-
18
- def enabled_or_disabled = %i[enabled disabled]
16
+ def filter_logic_valid?(input)
17
+ %i[and or].include?(input)
18
+ end
19
19
  end
20
20
  end
21
21
  end
@@ -1,4 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'dsl/builder'
3
+ # Must be loaded before builder as some validations are needed during build phase
4
4
  require_relative 'dsl/validation'
5
+
6
+ require_relative 'dsl/builder'
@@ -5,5 +5,6 @@ module TestingRecord
5
5
  class AttributeError < StandardError; end
6
6
  class EntityError < StandardError; end
7
7
  class InvalidConfigurationError < StandardError; end
8
+ class InvalidArgumentError < StandardError; end
8
9
  end
9
10
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'automation_helpers/extensions/string'
4
+
4
5
  require_relative 'dsl'
5
6
 
6
7
  module TestingRecord
@@ -11,8 +12,6 @@ module TestingRecord
11
12
  extend DSL::Builder::Helpers
12
13
  extend DSL::Builder::Settings
13
14
 
14
- attr_reader :attributes
15
-
16
15
  class << self
17
16
  attr_reader :current
18
17
 
@@ -25,7 +24,7 @@ module TestingRecord
25
24
  #
26
25
  # @return [TestingRecord::Model]
27
26
  def create(attributes)
28
- attributes.transform_keys!(&:to_sym)
27
+ attributes.transform_keys! { |key| key.to_s.tr('-', '_').to_sym }
29
28
  if respond_to?(:all)
30
29
  create_with_caching(attributes)
31
30
  else
@@ -110,6 +109,8 @@ module TestingRecord
110
109
  end
111
110
  end
112
111
 
112
+ attr_reader :attributes
113
+
113
114
  def initialize(attributes = {})
114
115
  @attributes = attributes
115
116
  end
@@ -135,7 +136,7 @@ module TestingRecord
135
136
  #
136
137
  # @return [TestingRecord::Model]
137
138
  def update(attrs)
138
- attrs.transform_keys(&:to_sym).each do |key, value|
139
+ attrs.transform_keys! { |key| key.to_s.tr('-', '_').to_sym }.each do |key, value|
139
140
  attributes[key] = value
140
141
  instance_variable_set("@#{key}", value)
141
142
  TestingRecord.logger.info("Updated '#{key}' on the #{self.class} entity to be '#{value}'")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TestingRecord
4
- VERSION = '1.0.1'
4
+ VERSION = '1.1'
5
5
  end
@@ -18,6 +18,7 @@ module TestingRecord
18
18
  # Configure a default primary key for all TestingRecord models
19
19
  def default_primary_key=(value)
20
20
  raise InvalidConfigurationError, 'Invalid primary key value, must be a Symbol' unless value.is_a?(Symbol)
21
+ raise InvalidConfigurationError, 'Primary key value cannot contain `-` please use `_` instead' if value.to_s.include?('-')
21
22
 
22
23
  @default_primary_key = value
23
24
  TestingRecord::Model.primary_key value
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: 1.0.1
4
+ version: '1.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-05-05 00:00:00.000000000 Z
11
+ date: 2026-05-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: automation_helpers
@@ -145,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
145
  - !ruby/object:Gem::Version
146
146
  version: '0'
147
147
  requirements: []
148
- rubygems_version: 3.3.27
148
+ rubygems_version: 3.4.19
149
149
  signing_key:
150
150
  specification_version: 4
151
151
  summary: Thread based caching system to store and edit records