Skip to content

Commit 14c78e3

Browse files
committed
Remove String#reader, String#writer?, String#valid_method_name? and String#before_type_cast?
1 parent 30f46d4 commit 14c78e3

File tree

4 files changed

+105
-116
lines changed

4 files changed

+105
-116
lines changed

lib/mongoid/attributes/dynamic.rb

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ module Dynamic
2020
def respond_to?(name, include_private = false)
2121
super || (
2222
attributes &&
23-
attributes.has_key?(name.to_s.reader)
23+
attributes.has_key?(__dynamic_reader(name))
2424
)
2525
end
2626

@@ -33,7 +33,7 @@ def respond_to?(name, include_private = false)
3333
#
3434
# @param [ String ] name The name of the field.
3535
def define_dynamic_reader(name)
36-
return unless name.valid_method_name?
36+
return unless valid_method_name?(name)
3737

3838
class_eval do
3939
define_method(name) do
@@ -69,7 +69,7 @@ def define_dynamic_before_type_cast_reader(name)
6969
#
7070
# @param [ String ] name The name of the field.
7171
def define_dynamic_writer(name)
72-
return unless name.valid_method_name?
72+
return unless valid_method_name?(name)
7373

7474
class_eval do
7575
define_method("#{name}=") do |value|
@@ -120,23 +120,53 @@ def inspect_dynamic_fields
120120
#
121121
# @return [ Object ] The result of the method call.
122122
def method_missing(name, *args)
123-
attr = name.to_s
124-
return super unless attributes.has_key?(attr.reader)
125-
if attr.writer?
126-
getter = attr.reader
127-
define_dynamic_writer(getter)
128-
write_attribute(getter, args.first)
129-
elsif attr.before_type_cast?
130-
define_dynamic_before_type_cast_reader(attr.reader)
131-
attribute_will_change!(attr.reader)
132-
read_attribute_before_type_cast(attr.reader)
123+
reader = __dynamic_reader(name)
124+
return super unless attributes.has_key?(reader)
125+
if __dynamic_writer?(name)
126+
define_dynamic_writer(reader)
127+
write_attribute(reader, args.first)
128+
elsif name.to_s.ends_with?('_before_type_cast')
129+
define_dynamic_before_type_cast_reader(reader)
130+
attribute_will_change!(reader)
131+
read_attribute_before_type_cast(reader)
133132
else
134-
getter = attr.reader
135-
define_dynamic_reader(getter)
136-
attribute_will_change!(attr.reader)
137-
read_raw_attribute(getter)
133+
define_dynamic_reader(reader)
134+
attribute_will_change!(reader)
135+
read_raw_attribute(reader)
138136
end
139137
end
138+
139+
private
140+
141+
# Get the string as a getter string.
142+
#
143+
# @example Get the reader/getter
144+
# "model=".reader
145+
#
146+
# @return [ String ] The string stripped of "=".
147+
def __dynamic_reader(method)
148+
method.to_s.delete("=").sub(/_before_type_cast\z/, '')
149+
end
150+
151+
# Is this string a writer?
152+
#
153+
# @example Is the string a setter method?
154+
# "model=".writer?
155+
#
156+
# @return [ true | false ] If the string contains "=".
157+
def __dynamic_writer?(method)
158+
method.to_s.include?("=")
159+
end
160+
161+
# Is this string a valid_method_name?
162+
#
163+
# @example Is the string a valid Ruby identifier for use as a method name
164+
# "model=".valid_method_name?
165+
#
166+
# @return [ true | false ] If the string contains a valid Ruby identifier.
167+
def valid_method_name?(method)
168+
!method.to_s.match?(/[@$"-]/)
169+
end
140170
end
141171
end
142172
end

lib/mongoid/extensions/string.rb

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -86,46 +86,6 @@ def numeric?
8686
(self =~ /\A(?:NaN|-?Infinity)\z/) == 0
8787
end
8888

89-
# Get the string as a getter string.
90-
#
91-
# @example Get the reader/getter
92-
# "model=".reader
93-
#
94-
# @return [ String ] The string stripped of "=".
95-
def reader
96-
delete("=").sub(/\_before\_type\_cast\z/, '')
97-
end
98-
99-
# Is this string a writer?
100-
#
101-
# @example Is the string a setter method?
102-
# "model=".writer?
103-
#
104-
# @return [ true | false ] If the string contains "=".
105-
def writer?
106-
include?("=")
107-
end
108-
109-
# Is this string a valid_method_name?
110-
#
111-
# @example Is the string a valid Ruby identifier for use as a method name
112-
# "model=".valid_method_name?
113-
#
114-
# @return [ true | false ] If the string contains a valid Ruby identifier.
115-
def valid_method_name?
116-
/[@$"-]/ !~ self
117-
end
118-
119-
# Does the string end with _before_type_cast?
120-
#
121-
# @example Is the string a setter method?
122-
# "price_before_type_cast".before_type_cast?
123-
#
124-
# @return [ true | false ] If the string ends with "_before_type_cast"
125-
def before_type_cast?
126-
ends_with?("_before_type_cast")
127-
end
128-
12989
# Is the object not to be converted to bson on criteria creation?
13090
#
13191
# @example Is the object unconvertable?

spec/mongoid/attributes/dynamic_spec.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,4 +150,62 @@
150150

151151
it_behaves_like 'dynamic field'
152152
end
153+
154+
describe "#reader" do
155+
156+
context "when string is a reader" do
157+
158+
it "returns self" do
159+
expect("attribute".reader).to eq("attribute")
160+
end
161+
end
162+
163+
context "when string is a writer" do
164+
165+
it "returns the reader" do
166+
expect("attribute=".reader).to eq("attribute")
167+
end
168+
end
169+
170+
context "when the string is before_type_cast" do
171+
172+
it "returns the reader" do
173+
expect("attribute_before_type_cast".reader).to eq("attribute")
174+
end
175+
end
176+
end
177+
178+
describe "#writer?" do
179+
180+
context "when string is a reader" do
181+
182+
it "returns false" do
183+
expect("attribute".writer?).to be false
184+
end
185+
end
186+
187+
context "when string is a writer" do
188+
189+
it "returns true" do
190+
expect("attribute=".writer?).to be true
191+
end
192+
end
193+
end
194+
195+
describe "#before_type_cast?" do
196+
197+
context "when string is a reader" do
198+
199+
it "returns false" do
200+
expect("attribute".before_type_cast?).to be false
201+
end
202+
end
203+
204+
context "when string is before_type_cast" do
205+
206+
it "returns true" do
207+
expect("attribute_before_type_cast".before_type_cast?).to be true
208+
end
209+
end
210+
end
153211
end

spec/mongoid/extensions/string_spec.rb

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -230,30 +230,6 @@ class Patient
230230
end
231231
end
232232

233-
describe "#reader" do
234-
235-
context "when string is a reader" do
236-
237-
it "returns self" do
238-
expect("attribute".reader).to eq("attribute")
239-
end
240-
end
241-
242-
context "when string is a writer" do
243-
244-
it "returns the reader" do
245-
expect("attribute=".reader).to eq("attribute")
246-
end
247-
end
248-
249-
context "when the string is before_type_cast" do
250-
251-
it "returns the reader" do
252-
expect("attribute_before_type_cast".reader).to eq("attribute")
253-
end
254-
end
255-
end
256-
257233
describe "#numeric?" do
258234

259235
context "when the string is an integer" do
@@ -350,39 +326,4 @@ class Patient
350326
end
351327
end
352328
end
353-
354-
describe "#writer?" do
355-
356-
context "when string is a reader" do
357-
358-
it "returns false" do
359-
expect("attribute".writer?).to be false
360-
end
361-
end
362-
363-
context "when string is a writer" do
364-
365-
it "returns true" do
366-
expect("attribute=".writer?).to be true
367-
end
368-
end
369-
end
370-
371-
describe "#before_type_cast?" do
372-
373-
context "when string is a reader" do
374-
375-
it "returns false" do
376-
expect("attribute".before_type_cast?).to be false
377-
end
378-
end
379-
380-
context "when string is before_type_cast" do
381-
382-
it "returns true" do
383-
expect("attribute_before_type_cast".before_type_cast?).to be true
384-
end
385-
end
386-
end
387-
388329
end

0 commit comments

Comments
 (0)