activerecord-null 0.1.1 → 0.1.2
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 +4 -4
- data/CHANGELOG.md +6 -6
- data/README.md +20 -0
- data/lib/activerecord/null/mimic.rb +2 -0
- data/lib/activerecord/null/version.rb +1 -1
- data/lib/activerecord/null.rb +10 -0
- data/test/activerecord/test_null.rb +20 -0
- metadata +3 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5da1cf27cad7e88a6c6696874f43ea35670baea4ba390b6d19d2ab9f044d842
|
4
|
+
data.tar.gz: fd69ad9dfe3b66e186aecb8b6b8411d814fad60ad908d2816e0e9a0ac2ad0a39
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c9f845b168b47184197bfc10367fc4a7b93bec34e9f31e01a81a88c64b4a55e7b1d09e911ed08cf19eb562738cc9255f43e43a03d5a832ced57d295e642fcca
|
7
|
+
data.tar.gz: a7fb4fe100e29d9151d2fefd5696db658171f4c061a0ded77424b7c1d2e947db4fec50c4c5d6cb0132a56463b22bfddc0da98ce064670d02c1713b159b1042bf
|
data/CHANGELOG.md
CHANGED
@@ -5,6 +5,8 @@ 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
|
9
|
+
|
8
10
|
## [0.1.1] - 2024-10-25
|
9
11
|
|
10
12
|
### Added
|
@@ -12,9 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
12
14
|
- `null?` method to both parent class objects and null objects
|
13
15
|
- Null objects now have default nil values for attributes of the mimic model class
|
14
16
|
- `Null()` method can now accept a hash of attribute names and values to assign to the null object
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- Initial release
|
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?`
|
data/README.md
CHANGED
@@ -48,6 +48,26 @@ It will even work with associations.
|
|
48
48
|
User.null.posts # => []
|
49
49
|
```
|
50
50
|
|
51
|
+
By default, the null object will have the same attributes as the original model and will return `nil` for all attributes.
|
52
|
+
|
53
|
+
You can override this by passing a hash to the `Null` method where the key is an array of attributes and the value is the value to return for the attribute.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class User < ApplicationRecord
|
57
|
+
Null([:name, :team_name] => "Unknown") do
|
58
|
+
def other_attribute = "Other"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
You may also pass a callable to the hash which will be used to determine the value for the attribute.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
class User < ApplicationRecord
|
67
|
+
Null([:name, :team_name] => -> { "Unknown" })
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
51
71
|
## Development
|
52
72
|
|
53
73
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/activerecord/null.rb
CHANGED
@@ -43,6 +43,16 @@ module ActiveRecord
|
|
43
43
|
mimics inherit
|
44
44
|
|
45
45
|
include Singleton
|
46
|
+
|
47
|
+
class << self
|
48
|
+
def method_missing(method, ...)
|
49
|
+
mimic_model_class.respond_to?(method) ? mimic_model_class.send(method, ...) : super
|
50
|
+
end
|
51
|
+
|
52
|
+
def respond_to_missing?(method, include_private = false)
|
53
|
+
mimic_model_class.respond_to?(method, include_private) || super
|
54
|
+
end
|
55
|
+
end
|
46
56
|
end
|
47
57
|
null_class.class_eval(&) if block_given?
|
48
58
|
|
@@ -35,6 +35,18 @@ class ActiveRecord::TestNull < Minitest::Spec
|
|
35
35
|
end
|
36
36
|
end
|
37
37
|
|
38
|
+
describe ".has_query_constraints?" do
|
39
|
+
it "returns false" do
|
40
|
+
expect(User::Null.has_query_constraints?).must_equal false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe ".composite_primary_key?" do
|
45
|
+
it "returns false" do
|
46
|
+
expect(User::Null.composite_primary_key?).must_equal false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
38
50
|
describe "Null" do
|
39
51
|
it "is null" do
|
40
52
|
expect(User.null.null?).must_equal true
|
@@ -104,6 +116,14 @@ class ActiveRecord::TestNull < Minitest::Spec
|
|
104
116
|
it "assigns callable values to attributes" do
|
105
117
|
expect(Post.null.description).must_equal "From the callable!"
|
106
118
|
end
|
119
|
+
|
120
|
+
it "reads attributes and returns nil" do
|
121
|
+
expect(Post.null._read_attribute(:description)).must_be_nil
|
122
|
+
end
|
123
|
+
|
124
|
+
it "responds to mimic methods" do
|
125
|
+
expect(Post.null.respond_to?(:description)).must_equal true
|
126
|
+
end
|
107
127
|
end
|
108
128
|
|
109
129
|
describe "Parent class object" do
|
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-null
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jim Gay
|
8
|
-
autorequire:
|
9
8
|
bindir: exe
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: activerecord
|
@@ -49,7 +48,6 @@ metadata:
|
|
49
48
|
homepage_uri: https://github.com/SOFware/activerecord-null
|
50
49
|
source_code_uri: https://github.com/SOFware/activerecord-null
|
51
50
|
changelog_uri: https://github.com/SOFware/activerecord-null/blob/main/CHANGELOG.md
|
52
|
-
post_install_message:
|
53
51
|
rdoc_options: []
|
54
52
|
require_paths:
|
55
53
|
- lib
|
@@ -64,8 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
62
|
- !ruby/object:Gem::Version
|
65
63
|
version: '0'
|
66
64
|
requirements: []
|
67
|
-
rubygems_version: 3.
|
68
|
-
signing_key:
|
65
|
+
rubygems_version: 3.6.7
|
69
66
|
specification_version: 4
|
70
67
|
summary: Null Objects for ActiveRecord
|
71
68
|
test_files: []
|