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 +4 -4
- data/CHANGELOG.md +5 -0
- data/lib/static_association/version.rb +1 -1
- data/lib/static_association.rb +17 -1
- data/spec/static_association_spec.rb +53 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5666e88a0802eea2fe4650de42230959d14a792dd161c50ee81c1abeb681df12
|
|
4
|
+
data.tar.gz: fd5d870372e99f9ba199eef79c0550b673b707d051831ed35197489de6c5e017
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8b6462940e27e5abccfe7938e63efceed99418e8dc3b808ab42926525e914835d9a42b25e49942039c5108ca90b63fbb2adaab23bb4ef9a0c6c13069bbd00949
|
|
7
|
+
data.tar.gz: 4b594996748a63859e1326ab389b23dd17d8ba67e2c3b12b3ed63d4d13b1539be2851c85abd2e7b712e7a5c15bcd9f0753d33871caf79aa53e7bc38c4b6e181e
|
data/CHANGELOG.md
CHANGED
data/lib/static_association.rb
CHANGED
|
@@ -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
|
|
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
|
|