activerecord-null 0.1.2 → 0.1.3

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: f5da1cf27cad7e88a6c6696874f43ea35670baea4ba390b6d19d2ab9f044d842
4
- data.tar.gz: fd69ad9dfe3b66e186aecb8b6b8411d814fad60ad908d2816e0e9a0ac2ad0a39
3
+ metadata.gz: 61c6b213e7c4c8f5cbeab18700a2eebc46803592fb34b3677ed93da0a6c49c92
4
+ data.tar.gz: 698f74dbdf03af5dbdc7e35111f20a478363c832028258c77cc14fdf0061b8b0
5
5
  SHA512:
6
- metadata.gz: 5c9f845b168b47184197bfc10367fc4a7b93bec34e9f31e01a81a88c64b4a55e7b1d09e911ed08cf19eb562738cc9255f43e43a03d5a832ced57d295e642fcca
7
- data.tar.gz: a7fb4fe100e29d9151d2fefd5696db658171f4c061a0ded77424b7c1d2e947db4fec50c4c5d6cb0132a56463b22bfddc0da98ce064670d02c1713b159b1042bf
6
+ metadata.gz: 3e8fd9f3d1c8c130a997e3c66c6560b82e18b2ff782a2772832de7e4344abb59f0a6ef0efcaae67cb2a47dd221076b1113be2a0f520bc0c267a6ecb137c3b7eb
7
+ data.tar.gz: 9848bd89df1f1cb2ded9dcecd27f478aacbac6ed1586f56ecd7b17446b3755bceec6fbef8b0858b58be7996da11617c1d2f056517c0a55a03aff26a79ee483e5
data/CHANGELOG.md CHANGED
@@ -5,16 +5,24 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
- ## [0.1.2] - 2025-06-17
8
+ ## [0.1.3] - 2025-11-18
9
9
 
10
- ## [0.1.1] - 2024-10-25
10
+ ### Changed
11
11
 
12
- ### Added
12
+ - Use git trailers to track changelog entries. (d296851)
13
+ - Implement [] method to access attributes with string/symbol keys (1dc4f95)
13
14
 
14
- - `null?` method to both parent class objects and null objects
15
- - Null objects now have default nil values for attributes of the mimic model class
16
- - `Null()` method can now accept a hash of attribute names and values to assign to the null object
17
- - `has_query_constraints?` method to null objects
18
- - `respond_to?` method to null objects
19
- - `_read_attribute` method to null objects
20
- - Null class now return false for `composite_primary_key?`
15
+ ### Removed
16
+
17
+ - CodeClimate access on CI runs. (2dbf5bc)
18
+
19
+ ## [0.1.3] - 2025-11-18
20
+
21
+ ### Changed
22
+
23
+ - Use git trailers to track changelog entries. (d296851)
24
+ - Implement [] method to access attributes with string/symbol keys (1dc4f95)
25
+
26
+ ### Removed
27
+
28
+ - CodeClimate access on CI runs. (2dbf5bc)
data/Rakefile CHANGED
@@ -3,7 +3,9 @@
3
3
  require "bundler/gem_tasks"
4
4
  require "minitest/test_task"
5
5
 
6
- Minitest::TestTask.create
6
+ Minitest::TestTask.create do |t|
7
+ t.test_prelude = 'require "test_helper"'
8
+ end
7
9
 
8
10
  task default: :test
9
11
 
@@ -11,4 +13,5 @@ require "reissue/gem"
11
13
 
12
14
  Reissue::Task.create :reissue do |task|
13
15
  task.version_file = "lib/activerecord/null/version.rb"
16
+ task.fragment = :git
14
17
  end
@@ -35,7 +35,13 @@ module ActiveRecord
35
35
 
36
36
  attr_reader :id
37
37
 
38
- def [](*)
38
+ def [](key)
39
+ normalized_key = key.to_sym
40
+ association_names = mimic_model_class.reflect_on_all_associations.map(&:name)
41
+
42
+ return nil if association_names.include?(normalized_key)
43
+
44
+ respond_to?(normalized_key) ? send(normalized_key) : nil
39
45
  end
40
46
 
41
47
  def is_a?(klass)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module Null
5
- VERSION = "0.1.2"
5
+ VERSION = "0.1.3"
6
6
  end
7
7
  end
@@ -40,6 +40,7 @@ module ActiveRecord
40
40
  end
41
41
  null_class = Class.new do
42
42
  include ::ActiveRecord::Null::Mimic
43
+
43
44
  mimics inherit
44
45
 
45
46
  include Singleton
@@ -124,6 +124,68 @@ class ActiveRecord::TestNull < Minitest::Spec
124
124
  it "responds to mimic methods" do
125
125
  expect(Post.null.respond_to?(:description)).must_equal true
126
126
  end
127
+
128
+ describe "bracket access" do
129
+ describe "with attributes" do
130
+ it "accesses attribute with symbol key" do
131
+ expect(User.null[:name]).must_equal "None"
132
+ end
133
+
134
+ it "accesses attribute with string key" do
135
+ expect(User.null["name"]).must_equal "None"
136
+ end
137
+
138
+ it "returns same value for string and symbol keys" do
139
+ expect(User.null[:team_name]).must_equal User.null["team_name"]
140
+ expect(User.null[:team_name]).must_equal "Unknown"
141
+ end
142
+
143
+ it "returns custom attribute values" do
144
+ expect(User.null[:name]).must_equal "None"
145
+ end
146
+
147
+ it "returns static assigned attribute values" do
148
+ expect(User.null[:team_name]).must_equal "Unknown"
149
+ expect(User.null[:other]).must_equal "Unknown"
150
+ end
151
+
152
+ it "returns callable attribute values" do
153
+ expect(Post.null[:description]).must_equal "From the callable!"
154
+ expect(Post.null["description"]).must_equal "From the callable!"
155
+ end
156
+
157
+ it "returns nil for default attributes" do
158
+ expect(User.null[:id]).must_be_nil
159
+ expect(Post.null[:title]).must_be_nil
160
+ end
161
+ end
162
+
163
+ describe "with associations" do
164
+ it "returns nil for belongs_to association" do
165
+ expect(User.null[:business]).must_be_nil
166
+ expect(Post.null[:user]).must_be_nil
167
+ end
168
+
169
+ it "returns nil for has_many association" do
170
+ expect(User.null[:posts]).must_be_nil
171
+ end
172
+
173
+ it "still allows dot notation for associations" do
174
+ expect(User.null.business).must_be_instance_of Business::Null
175
+ expect(User.null.posts).must_be_kind_of ActiveRecord::Relation
176
+ end
177
+ end
178
+
179
+ describe "with non-existent keys" do
180
+ it "returns nil for unknown symbol key" do
181
+ expect(User.null[:nonexistent]).must_be_nil
182
+ end
183
+
184
+ it "returns nil for unknown string key" do
185
+ expect(User.null["nonexistent"]).must_be_nil
186
+ end
187
+ end
188
+ end
127
189
  end
128
190
 
129
191
  describe "Parent class object" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-null
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Gay
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
62
62
  - !ruby/object:Gem::Version
63
63
  version: '0'
64
64
  requirements: []
65
- rubygems_version: 3.6.7
65
+ rubygems_version: 3.7.2
66
66
  specification_version: 4
67
67
  summary: Null Objects for ActiveRecord
68
68
  test_files: []