static_association 0.2.0 → 0.3.0

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: 86ead7939a0117761cc96d89b8bead9dd9d6158e6266a8d068c9033b55fa727a
4
- data.tar.gz: b8e8fed19948fa1c38dd7b975c083af75c1e232f89010d197ca661bb48c6a422
3
+ metadata.gz: 5666e88a0802eea2fe4650de42230959d14a792dd161c50ee81c1abeb681df12
4
+ data.tar.gz: fd5d870372e99f9ba199eef79c0550b673b707d051831ed35197489de6c5e017
5
5
  SHA512:
6
- metadata.gz: a524a9345a9b43d5f6c9651e2be95cdc56095fca59a5cf7e97b032e424d122fe1769d365caed9c993ef01b49f6fa77cdcab23b1e841a731392ffe0ebd7707883
7
- data.tar.gz: 5a54cf10f28c4582cd9aa9d353defc0c8fe853014be4c883563f7f7b6bbad0d856a0f5d889ec5ca8f16c04d1f1ebf56fc71506dfdd7d776e6c754bf8c74a5e02
6
+ metadata.gz: 8b6462940e27e5abccfe7938e63efceed99418e8dc3b808ab42926525e914835d9a42b25e49942039c5108ca90b63fbb2adaab23bb4ef9a0c6c13069bbd00949
7
+ data.tar.gz: 4b594996748a63859e1326ab389b23dd17d8ba67e2c3b12b3ed63d4d13b1539be2851c85abd2e7b712e7a5c15bcd9f0753d33871caf79aa53e7bc38c4b6e181e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.3.0 (24 October 2025)
4
+
5
+ - Add `attr_evaluated` to define attributes that are evaluated/computed at runtime (#32)
6
+ - Use including class name for error messages (#31)
7
+
3
8
  ## v0.2.0 (20 June 2025)
4
9
 
5
10
  ### Added
@@ -1,3 +1,3 @@
1
1
  module StaticAssociation
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
@@ -42,7 +42,7 @@ module StaticAssociation
42
42
 
43
43
  def find(id)
44
44
  find_by_id(id) or raise RecordNotFound.new(
45
- "Couldn't find DummyClass with 'id'=#{id}"
45
+ "Couldn't find #{name} with 'id'=#{id}"
46
46
  )
47
47
  end
48
48
 
@@ -82,6 +82,22 @@ module StaticAssociation
82
82
  index[id] = record
83
83
  end
84
84
 
85
+ def attr_evaluated(*names)
86
+ attr_writer(*names)
87
+
88
+ names.each do |name|
89
+ define_method(name) do
90
+ attr_value = instance_variable_get("@#{name}")
91
+
92
+ if attr_value.respond_to?(:call)
93
+ instance_exec(&attr_value)
94
+ else
95
+ attr_value
96
+ end
97
+ end
98
+ end
99
+ end
100
+
85
101
  private
86
102
 
87
103
  def matches_attributes?(record:, attributes:)
@@ -3,7 +3,9 @@ require "static_association"
3
3
 
4
4
  class DummyClass
5
5
  include StaticAssociation
6
+
6
7
  attr_accessor :name
8
+ attr_evaluated :gender
7
9
  end
8
10
 
9
11
  class AssociationClass
@@ -11,6 +13,7 @@ class AssociationClass
11
13
  attr_accessor :dodo_class_id
12
14
 
13
15
  extend StaticAssociation::AssociationHelpers
16
+
14
17
  belongs_to_static :dummy_class
15
18
  belongs_to_static :dodo_class, class_name: "DummyClass"
16
19
  end
@@ -75,6 +78,49 @@ RSpec.describe StaticAssociation do
75
78
  expect { DummyClass.record(id: 1) }.to change(DummyClass, :count).by(1)
76
79
  end
77
80
  end
81
+
82
+ context "when attr_evaluated defined attribute is assigned a lambda" do
83
+ it "evaluates the lambda when the attribute is read" do
84
+ record = DummyClass.record(id: 1) do
85
+ self.gender = -> do
86
+ if name == "Jane"
87
+ :female
88
+ else
89
+ :male
90
+ end
91
+ end
92
+ end
93
+
94
+ expect(record.gender).to eq(:male)
95
+ end
96
+ end
97
+
98
+ context "when attr_evaluated defined attribute is assigned a proc" do
99
+ it "evaluates the proc when the attribute is read" do
100
+ record = DummyClass.record(id: 1) do
101
+ self.name = "Jane"
102
+ self.gender = proc do
103
+ if @name == "Jane"
104
+ :female
105
+ else
106
+ :male
107
+ end
108
+ end
109
+ end
110
+
111
+ expect(record.gender).to eq(:female)
112
+ end
113
+ end
114
+
115
+ context "when attr_evaluated defined attribute is assigned a static value" do
116
+ it "returns the assigned value when the attribute is read" do
117
+ record = DummyClass.record(id: 1) do
118
+ self.gender = "Not disclosed"
119
+ end
120
+
121
+ expect(record.gender).to eq("Not disclosed")
122
+ end
123
+ end
78
124
  end
79
125
 
80
126
  describe ".all" do
@@ -117,6 +163,13 @@ RSpec.describe StaticAssociation do
117
163
  StaticAssociation::RecordNotFound,
118
164
  "Couldn't find DummyClass with 'id'=1"
119
165
  )
166
+
167
+ stub_const("NewDummyClass", Class.new(DummyClass))
168
+ expect { NewDummyClass.find(1) }
169
+ .to raise_error(
170
+ StaticAssociation::RecordNotFound,
171
+ "Couldn't find NewDummyClass with 'id'=1"
172
+ )
120
173
  end
121
174
  end
122
175
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: static_association
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oliver Nightingale