activerecord-null 0.1.0 → 0.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: 7bb3c3183d7c9c124c3858a72534649bf8e501f6371942e8a1a06697df1caadf
4
- data.tar.gz: c280cf8d79aba38ded094dd6140f2ff4f7075b295531326f4e23ecb2df7a4524
3
+ metadata.gz: bfe02235c54d325ce319a9c7cf6123c71e3aab539f21788262671f445a344688
4
+ data.tar.gz: b125dada1c0fce080fd37f3a3ccf9525bd61b427c4027c0b3095fcd1ddd85bb7
5
5
  SHA512:
6
- metadata.gz: c861a1f9cb0cc2685e6a611810521539ae1c6e91bff2864607d44f342380d1822bb9cd7f0ed32b9791d6e52dcc7e8c57b7a7ee01371964b6df25c0b8df83b3a5
7
- data.tar.gz: 9aa04f050ad57a08a6005b8ac01b27548644a4098e301452a58d3c8f5d66c4d76efc158b5d6cb1347c36ae7906e2b01c00eaba0add423621c4a54963b9687f5e
6
+ metadata.gz: 0f61caa026a58fe1faee75009734df5e283d62077093c472b191f25497098e8afca8216a33923f70650e2f022e41ec0d6ca29c8273af80ec0b1e205d6913ff69
7
+ data.tar.gz: 73b85e766af51fe0dfd7192d80181100584faa81ecf1a6e76b3d08bb06b7738c54d662976385480044b4119f41ac6218950caf07fbc66f249c086a49001cf4e5
data/CHANGELOG.md CHANGED
@@ -5,6 +5,14 @@ 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.1] - 2024-10-25
9
+
10
+ ### Added
11
+
12
+ - `null?` method to both parent class objects and null objects
13
+ - Null objects now have default nil values for attributes of the mimic model class
14
+ - `Null()` method can now accept a hash of attribute names and values to assign to the null object
15
+
8
16
  ## [0.1.0] - 2024-10-23
9
17
 
10
18
  ### Added
@@ -17,6 +17,18 @@ module ActiveRecord
17
17
  def self.table_name = @mimic_model_class.to_s.tableize
18
18
 
19
19
  def self.primary_key = @mimic_model_class.primary_key
20
+
21
+ def self.define_attribute_methods(attribute_names, value: nil)
22
+ attribute_names.each do |attribute_name|
23
+ unless method_defined?(attribute_name)
24
+ if value.is_a?(Proc)
25
+ define_method(attribute_name, &value)
26
+ else
27
+ define_method(attribute_name) { value }
28
+ end
29
+ end
30
+ end
31
+ end
20
32
  end
21
33
 
22
34
  def mimic_model_class = self.class.mimic_model_class
@@ -34,6 +46,8 @@ module ActiveRecord
34
46
  end
35
47
  end
36
48
 
49
+ def null? = true
50
+
37
51
  def destroyed? = false
38
52
 
39
53
  def new_record? = false
@@ -63,7 +77,7 @@ module ActiveRecord
63
77
  private
64
78
 
65
79
  def define_memoized_association(method, value)
66
- define_singleton_method(method) do
80
+ self.class.define_method(method) do
67
81
  if instance_variable_defined?(:"@#{method}")
68
82
  instance_variable_get(:"@#{method}")
69
83
  else
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module Null
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.1"
6
6
  end
7
7
  end
@@ -15,25 +15,52 @@ module ActiveRecord
15
15
  # Define a Null class for the given class.
16
16
  #
17
17
  # @example
18
- # class Business
18
+ # class Business < ApplicationRecord
19
19
  # Null do
20
20
  # def name = "None"
21
21
  # end
22
22
  # end
23
23
  #
24
24
  # Business.null # => #<Business::Null:0x0000000000000000>
25
+ # Business.null.name # => "None"
25
26
  #
26
- def Null(klass = self, &)
27
+ # class User < ApplicationRecord
28
+ # Null([:name, :team_name] => "Unknown")
29
+ # end
30
+ #
31
+ # User.null.name # => "Unknown"
32
+ # User.null.team_name # => "Unknown"
33
+ #
34
+ # @param inherit [Class] The class from which the Null object inherits attributes
35
+ # @param assignments [Array] The attributes to assign to the null object
36
+ def Null(inherit = self, assignments = {}, &)
37
+ if inherit.is_a?(Hash)
38
+ assignments = inherit
39
+ inherit = self
40
+ end
27
41
  null_class = Class.new do
28
42
  include ::ActiveRecord::Null::Mimic
29
- mimics klass
43
+ mimics inherit
30
44
 
31
45
  include Singleton
32
46
  end
33
- null_class.class_eval(&)
34
- const_set(:Null, null_class)
47
+ null_class.class_eval(&) if block_given?
48
+
49
+ nil_assignments = inherit.attribute_names
50
+ if assignments.any?
51
+ assignments.each do |attributes, value|
52
+ nil_assignments -= attributes
53
+ null_class.define_attribute_methods(attributes, value:)
54
+ end
55
+ end
56
+ null_class.define_attribute_methods(nil_assignments)
57
+ inherit.const_set(:Null, null_class)
58
+
59
+ inherit.define_singleton_method(:null) { null_class.instance }
60
+ end
35
61
 
36
- define_singleton_method(:null) { null_class.instance }
62
+ def self.extended(base)
63
+ base.define_method(:null?) { false }
37
64
  end
38
65
  end
39
66
  end
@@ -15,13 +15,15 @@ end
15
15
 
16
16
  class Post < ApplicationRecord
17
17
  belongs_to :user
18
+
19
+ Null([:description] => -> { "From the callable!" })
18
20
  end
19
21
 
20
22
  class User < ApplicationRecord
21
23
  belongs_to :business
22
24
  has_many :posts
23
25
 
24
- Null do
26
+ Null([:team_name, :other] => "Unknown") do
25
27
  def name = "None"
26
28
  end
27
29
  end
@@ -34,6 +36,10 @@ class ActiveRecord::TestNull < Minitest::Spec
34
36
  end
35
37
 
36
38
  describe "Null" do
39
+ it "is null" do
40
+ expect(User.null.null?).must_equal true
41
+ end
42
+
37
43
  it "is not persisted" do
38
44
  expect(User.null.persisted?).must_equal false
39
45
  end
@@ -82,5 +88,27 @@ class ActiveRecord::TestNull < Minitest::Spec
82
88
  expect(User.null.posts).must_be_kind_of ActiveRecord::Relation
83
89
  expect(User.null.posts.to_a).must_equal []
84
90
  end
91
+
92
+ it "has default nil values for attributes of the mimic model class" do
93
+ expect(Post.null.title).must_be_nil
94
+ end
95
+
96
+ it "creates a Null object without a block" do
97
+ expect(Post.null).must_be_instance_of Post::Null
98
+ end
99
+
100
+ it "assigns the named attributes with the given values" do
101
+ expect(User.null.team_name).must_equal "Unknown"
102
+ end
103
+
104
+ it "assigns callable values to attributes" do
105
+ expect(Post.null.description).must_equal "From the callable!"
106
+ end
107
+ end
108
+
109
+ describe "Parent class object" do
110
+ it "is not null" do
111
+ expect(User.new.null?).must_equal false
112
+ end
85
113
  end
86
114
  end
@@ -5,11 +5,13 @@ ActiveRecord::Schema.define do
5
5
 
6
6
  create_table :users do |t|
7
7
  t.string :name
8
+ t.integer :team_name
8
9
  t.references :business
9
10
  end
10
11
 
11
12
  create_table :posts do |t|
12
13
  t.string :title
14
+ t.string :description
13
15
  t.references :user
14
16
  end
15
17
  end
data/test/test_helper.rb CHANGED
@@ -3,11 +3,10 @@
3
3
  require "simplecov" if ENV["CI"]
4
4
 
5
5
  $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
6
+ require "activerecord/null"
6
7
 
7
8
  require "active_record"
8
9
  ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
9
10
  load "test/support/schema.rb"
10
11
 
11
- require "activerecord/null"
12
-
13
12
  require "minitest/autorun"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-null
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Gay
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-10-23 00:00:00.000000000 Z
11
+ date: 2024-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord