testingrecord 0.4 → 0.5

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: 6f6570d13d91ba9f4785d7ea4c2be9c742710339406337c0c70c2095c727aa97
4
- data.tar.gz: 9e94862ed788d679feb98ad62a83c7aac84d3062931f2121bc6997f10ee86bd1
3
+ metadata.gz: 38eafdf3beadae05ae9138e4701d4644b3299dfa34e17ecb952d3f1cc7d2d3e0
4
+ data.tar.gz: beb36e06363dfaa6823f48f7b89b20502974d0d7c3f77a975991ab8e32104678
5
5
  SHA512:
6
- metadata.gz: f7f46a7072f63f08f67502bc6fb4076a7eec22d2bd3942670f871ef2df267e019a8bbc0edefc28d97792ce3cb6b504c3326b81026efda330ef508dfb96856f12
7
- data.tar.gz: e4c3fad3c5b588fd8a4db23bd27b106c509d7314bd34d008d4aef379cadc8c68fdc317d124862282dd9194969dc31cdacdec7968c786db1c724675055ca1a3be
6
+ metadata.gz: 1e2f489178b2aa12ab5a40756495d19f78d8e5d3d40deaa5c743e0e8288f278d90325a2db6f05fdfef40753360869543717cbedd00756eb0d472bd1ba142db3e
7
+ data.tar.gz: da06002f629a270cafba057303a3957e118b3f87f223731e1f8d9180191293abc979553da1ae4ca21570e81a0be823f905e8c11f87ac3c84ea79f20bb77b5a3e
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TestingRecord
4
+ module DSL
5
+ module Builder
6
+ # [TestingRecord::DSL::Builder::Filters]
7
+ # Ways in which we can filter our collection to find specific models
8
+ module Filters
9
+ # Checks to see whether an entity exists with the provided attributes
10
+ #
11
+ # @return [Boolean]
12
+ def exists?(attributes)
13
+ find_by(attributes).any?
14
+ end
15
+
16
+ # Finds an entity with the provided email address
17
+ # If one is found, set it as the current entity
18
+ #
19
+ # @return [TestingRecord::Model, nil]
20
+ def with_email(email_address)
21
+ find_by({ email_address: })&.first&.tap { |entity| entity.class.current = entity }
22
+ end
23
+
24
+ private
25
+
26
+ # Finds all entities that match specified attribute values
27
+ #
28
+ # @return [Array<TestingRecord::Model>]
29
+ def find_by(attributes)
30
+ pool = all
31
+ attributes.each do |key, value|
32
+ # TODO: Enable this logging once v0.5 is released with logger support
33
+ # AutomationLogger.debug("Current user pool size: #{pool.length}")
34
+ # AutomationLogger.debug("Filtering User list by #{key}: #{value}")
35
+ pool = pool.select { |entity| entity.attributes[key] == value }
36
+ end
37
+ pool
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -11,7 +11,7 @@ module TestingRecord
11
11
  # @return [Array]
12
12
  def add_helpers
13
13
  attributes.each do |attribute|
14
- add_presence_helper(attribute[:name], attribute[:type])
14
+ add_presence_helper(attribute)
15
15
  end
16
16
  end
17
17
 
@@ -22,9 +22,10 @@ module TestingRecord
22
22
  # For plural attributes whether the array has any values
23
23
  #
24
24
  # @return [TestingRecord::Model]
25
- def add_presence_helper(name, type)
25
+ def add_presence_helper(name)
26
26
  define_method(:"#{name}?") do
27
- type == :plural ? send(:name).any? : send(:name).empty?
27
+ obj = send(name)
28
+ !(obj.nil? || obj.empty?)
28
29
  end
29
30
  end
30
31
  end
@@ -10,55 +10,49 @@ module TestingRecord
10
10
  module Settings
11
11
  include DSL::Validation::Input
12
12
 
13
- # Create a cache of the entities, named according to the classname
14
- #
15
- # @return [Symbol]
16
- def caching(option)
17
- raise Error, 'Invalid caching option, must be :enabled or :disabled' unless caching_valid?(option)
18
- return unless option == :enabled
19
-
20
- instance_variable_set(ivar_name, [])
21
- define_singleton_method(cache_name) { instance_variable_get(ivar_name) }
22
- end
23
-
24
- # Sets an attribute on the model, this should have a name and an optional type (Defaults to `:singular`)
13
+ # Sets an attribute on the model
25
14
  #
26
- # @return [Array<Hash>]
27
- def attribute(name, type: :singular)
28
- raise Error, 'Invalid type option, must be :singular or :plural if specified' unless type_valid?(type)
29
-
15
+ # @return [Array<Symbol>]
16
+ def attribute(name)
30
17
  attr_reader name
31
18
 
32
- attributes << { name:, type: }
19
+ attributes << name
33
20
  end
34
21
 
35
22
  def attributes
36
23
  @attributes ||= []
37
24
  end
38
25
 
39
- # Set the type of model, this should be one of `:singular` or `:plural`
26
+ # Create a cache of the entities, named according to the classname
40
27
  #
41
28
  # @return [Symbol]
42
- def type(option)
43
- raise Error, 'Invalid type option, must be :singular or :plural' unless type_valid?(option)
29
+ def caching(option)
30
+ raise Error, 'Invalid caching option, must be :enabled or :disabled' unless caching_valid?(option)
31
+ return unless option == :enabled
32
+
33
+ instance_variable_set(:@all, [])
34
+ define_singleton_method(:all) { instance_variable_get(:@all) }
35
+ end
44
36
 
45
- @type = option
37
+ # Sets the primary key value of all entities - used for deduplication
38
+ # TODO: Use this for deduplication proper
39
+ #
40
+ # @return [Symbol]
41
+ def primary_key(option)
42
+ instance_variable_set(:@__primary_key, option.to_sym)
43
+ define_singleton_method(:__primary_key) { instance_variable_get(:@__primary_key) }
46
44
  end
47
45
 
48
46
  private
49
47
 
50
48
  def add_to_cache(entity)
51
49
  self.current = entity
52
- send(cache_name) << entity
50
+ all << entity
53
51
  # TODO: Add log message (Requires adding logger)
54
52
  end
55
53
 
56
- def cache_name
57
- :"#{to_s.snake_case}s"
58
- end
59
-
60
- def ivar_name
61
- "@#{to_s.snake_case}s"
54
+ def update_cache(entity)
55
+ # TODO: This needs implementing properly
62
56
  end
63
57
  end
64
58
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'builder/filters'
3
4
  require_relative 'builder/helpers'
4
5
  require_relative 'builder/settings'
@@ -13,17 +13,9 @@ module TestingRecord
13
13
  enabled_or_disabled.include?(input)
14
14
  end
15
15
 
16
- # Check whether the type setting is valid
17
- #
18
- # @return [Boolean]
19
- def type_valid?(input)
20
- singular_or_plural.include?(input)
21
- end
22
-
23
16
  private
24
17
 
25
18
  def enabled_or_disabled = %i[enabled disabled]
26
- def singular_or_plural = %i[singular plural]
27
19
  end
28
20
  end
29
21
  end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module TestingRecord
6
+ #
7
+ # @api private
8
+ #
9
+ class Logger
10
+ #
11
+ # Generate the Logger used in the gem
12
+ #
13
+ def self.create(output = $stdout)
14
+ logger = ::Logger.new(output)
15
+ logger.progname = 'Testing Record'
16
+ logger.level = :DEBUG
17
+ logger.formatter = proc do |severity, time, progname, msg|
18
+ "#{time.strftime('%F %T')} - #{severity} - #{progname} - #{msg}\n"
19
+ end
20
+
21
+ logger
22
+ end
23
+ end
24
+ end
@@ -7,6 +7,7 @@ module TestingRecord
7
7
  # The top level Model. Most of the behaviours specified here are fairly rudimentary ones that will then
8
8
  # include other behaviour(s), from the included modules
9
9
  class Model
10
+ extend DSL::Builder::Filters
10
11
  extend DSL::Builder::Helpers
11
12
  extend DSL::Builder::Settings
12
13
 
@@ -17,17 +18,16 @@ module TestingRecord
17
18
 
18
19
  # Creates an instance of the model
19
20
  # -> Adding it to the cache if caching is enabled
20
- # -> Creating iVar values for each property that was set
21
- # -> For now these will be set to `''` or `[]`
21
+ # -> Creating iVar values for each attribute that was provided
22
22
  #
23
23
  # @return [TestingRecord::Model]
24
24
  def create(attributes = self.attributes)
25
25
  new(attributes).tap do |entity|
26
- attributes.each do |attribute|
27
- default_value = attribute[:type] == :singular ? '' : []
28
- entity.instance_variable_set("@#{attribute[:name]}", default_value)
26
+ attributes.each do |attribute_key, attribute_value|
27
+ entity.instance_variable_set("@#{attribute_key}", attribute_value)
28
+ attr_reader attribute_key
29
29
  end
30
- add_to_cache(entity) if respond_to?(cache_name)
30
+ add_to_cache(entity) if respond_to?(:all)
31
31
  end
32
32
  end
33
33
  end
@@ -35,5 +35,22 @@ module TestingRecord
35
35
  def initialize(attributes = {})
36
36
  @attributes = attributes
37
37
  end
38
+
39
+ def inspect
40
+ "#<#{self.class.name} #{attributes.map { |k, v| "@#{k}=#{v.inspect}" }.join(', ')}>"
41
+ end
42
+
43
+ def to_s
44
+ inspect
45
+ end
46
+
47
+ def update(attrs)
48
+ attrs.each do |key, value|
49
+ # TODO: Once logger is implemented this needs modifying to output a log message
50
+ # AutomationLogger.debug("Updating '#{key}' on current User to be '#{value}'")
51
+ attributes[key] = value
52
+ instance_variable_set("@#{key}", value)
53
+ end
54
+ end
38
55
  end
39
56
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TestingRecord
4
- VERSION = '0.4'
4
+ VERSION = '0.5'
5
5
  end
@@ -1,10 +1,36 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'testing_record/dsl'
4
+ require_relative 'testing_record/logger'
4
5
  require_relative 'testing_record/model'
5
6
  require_relative 'testing_record/version'
6
7
 
8
+ # {TestingRecord} namespace
7
9
  module TestingRecord
10
+ # Generic TestingRecord error. Will be extended in the future
8
11
  class Error < StandardError; end
9
- # Your code goes here...
12
+
13
+ class << self
14
+ # The Testing Record logger object - This is called automatically in several
15
+ # locations and will log messages according to the normal Ruby protocol
16
+ #
17
+ # This logger object can also be used to manually log messages
18
+ #
19
+ # To Manually log a message
20
+ # TestingRecord.logger.info('Information')
21
+ # TestingRecord.logger.debug('Input debug message')
22
+ #
23
+ # By default the logger will output all messages to $stdout, but can be
24
+ # altered to log to a file or another IO location by calling `.log_path=`
25
+ def logger
26
+ @logger ||= Logger.create
27
+ end
28
+
29
+ # This writer method allows you to configure where you want the testingrecord logs to be sent to (Default is $stdout)
30
+ #
31
+ # Example: TestingRecord.log_path = 'testingrecord.log' would save all log messages to `./testingrecord.log`
32
+ def log_path=(logdev)
33
+ logger.reopen(logdev)
34
+ end
35
+ end
10
36
  end
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.4'
4
+ version: '0.5'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luke Hill
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-12-16 00:00:00.000000000 Z
11
+ date: 2025-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: automation_helpers
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: logger
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +58,14 @@ dependencies:
44
58
  requirements:
45
59
  - - "~>"
46
60
  - !ruby/object:Gem::Version
47
- version: 1.81.0
61
+ version: 1.82.0
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
66
  - - "~>"
53
67
  - !ruby/object:Gem::Version
54
- version: 1.81.0
68
+ version: 1.82.0
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rubocop-performance
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -72,16 +86,17 @@ dependencies:
72
86
  requirements:
73
87
  - - "~>"
74
88
  - !ruby/object:Gem::Version
75
- version: 3.7.0
89
+ version: 3.8.0
76
90
  type: :development
77
91
  prerelease: false
78
92
  version_requirements: !ruby/object:Gem::Requirement
79
93
  requirements:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
- version: 3.7.0
83
- description: Use metaprogrammed cache-models to store data you create on-the-fly.
84
- Access and retrieve references to data created from any place inside your tests.
96
+ version: 3.8.0
97
+ description: |-
98
+ Use metaprogrammed cache-models to store data you create on-the-fly. Access and retrieve references to data \
99
+ created from any place inside your tests.
85
100
  email:
86
101
  - lukehill_uk@hotmail.com
87
102
  executables: []
@@ -93,10 +108,12 @@ files:
93
108
  - lib/testing_record.rb
94
109
  - lib/testing_record/dsl.rb
95
110
  - lib/testing_record/dsl/builder.rb
111
+ - lib/testing_record/dsl/builder/filters.rb
96
112
  - lib/testing_record/dsl/builder/helpers.rb
97
113
  - lib/testing_record/dsl/builder/settings.rb
98
114
  - lib/testing_record/dsl/validation.rb
99
115
  - lib/testing_record/dsl/validation/input.rb
116
+ - lib/testing_record/logger.rb
100
117
  - lib/testing_record/model.rb
101
118
  - lib/testing_record/version.rb
102
119
  homepage: https://github.com/site-prism/testingrecord