Skip to content

Commit 4ad6381

Browse files
committed
Removes string evals in mock_model
1 parent 16dcb56 commit 4ad6381

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

lib/rspec/rails/mocks.rb

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -110,39 +110,40 @@ def self.primary_key; :id; end
110110
end
111111
end
112112
end
113-
m.__send__(:__mock_proxy).instance_eval(<<-CODE, __FILE__, __LINE__)
114-
def @object.is_a?(other)
115-
#{model_class}.ancestors.include?(other)
116-
end unless #{stubs.has_key?(:is_a?)}
117113

118-
def @object.kind_of?(other)
119-
#{model_class}.ancestors.include?(other)
120-
end unless #{stubs.has_key?(:kind_of?)}
114+
msingleton = m.singleton_class
115+
msingleton.__send__(:define_method, :is_a?) do |other|
116+
model_class.ancestors.include?(other)
117+
end unless stubs.has_key?(:is_a?)
121118

122-
def @object.instance_of?(other)
123-
other == #{model_class}
124-
end unless #{stubs.has_key?(:instance_of?)}
119+
msingleton.__send__(:define_method, :kind_of?) do |other|
120+
model_class.ancestors.include?(other)
121+
end unless stubs.has_key?(:kind_of?)
125122

126-
def @object.__model_class_has_column?(method_name)
127-
#{model_class}.respond_to?(:column_names) && #{model_class}.column_names.include?(method_name.to_s)
128-
end
123+
msingleton.__send__(:define_method, :instance_of?) do |other|
124+
other == model_class
125+
end unless stubs.has_key?(:instance_of?)
129126

130-
def @object.respond_to?(method_name, include_private=false)
131-
__model_class_has_column?(method_name) ? true : super
132-
end unless #{stubs.has_key?(:respond_to?)}
127+
msingleton.__send__(:define_method, :__model_class_has_column?) do |method_name|
128+
model_class.respond_to?(:column_names) && model_class.column_names.include?(method_name.to_s)
129+
end
133130

134-
def @object.method_missing(m, *a, &b)
135-
respond_to?(m) ? null_object? ? self : nil : super
136-
end
131+
msingleton.__send__(:define_method, :respond_to?) do |method_name, include_private=false|
132+
__model_class_has_column?(method_name) ? true : super(method_name, include_private)
133+
end unless stubs.has_key?(:respond_to?)
134+
135+
msingleton.__send__(:define_method, :method_missing) do |m, *a, &b|
136+
respond_to?(m) ? null_object? ? self : nil : super(m, *a, &b)
137+
end
137138

138-
def @object.class
139-
#{model_class}
140-
end unless #{stubs.has_key?(:class)}
139+
msingleton.__send__(:define_method, :class) do
140+
model_class
141+
end unless stubs.has_key?(:class)
141142

142-
def @object.to_s
143-
"#{model_class.name}_#{to_param}"
144-
end unless #{stubs.has_key?(:to_s)}
145-
CODE
143+
mock_param = to_param
144+
msingleton.__send__(:define_method, :to_s) do
145+
"#{model_class.name}_#{mock_param}"
146+
end unless stubs.has_key?(:to_s)
146147
yield m if block_given?
147148
end
148149
end

0 commit comments

Comments
 (0)