Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Prevent creation of instance methods when `instance_reader = false`, Grammar checks, Conditional statements combined #13401

Merged
merged 1 commit into from

3 participants

@akshay-vishnoi

No description provided.

@dmathieu dmathieu commented on the diff
...ctive_support/core_ext/class/delegating_attributes.rb
((8 lines not shown))
- # Generate the public methods name, name=, and name?
+ # Generate the public methods name, name=, and name?.
@dmathieu Collaborator

An interrogation point is a point. You don't need one after that.

It is not an interrogation statement and ? belongs to method name?.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...ort/test/core_ext/class/delegating_attributes_test.rb
@@ -42,7 +42,7 @@ def test_simple_accessor_declaration_with_instance_reader_false
single_class.superclass_delegating_accessor :no_instance_reader, :instance_reader => false
assert_respond_to single_class, :no_instance_reader
assert_respond_to single_class, :no_instance_reader=
- assert !single_class.public_instance_methods.map(&:to_s).include?("no_instance_reader")
+ assert (single_class.public_instance_methods & [:no_instance_reader, :no_instance_reader?, :_no_instance_reader]).empty?
@dmathieu Collaborator

What is the point of this? It decreases code readability.

Actually I wanted to ensure none of the listed methods should be available to single_class instances. In previous case, test case was checking only for no_instance_reader, but it should check for all.

If I go with the previous case then I will have to write assert statement 3 times, given that map(&:to_s) will be called for all of them. Should I stick with previous version?

@rafaelfranca Owner

You can store the instance methods in a local variable and do three assertions.

Yes I can do this. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...ctive_support/core_ext/class/delegating_attributes.rb
((17 lines not shown))
singleton_class.send(:define_method, "#{name}=") { |value| send("_#{name}=", value) }
- # If an instance_reader is needed, generate methods for name and name= on the
- # class itself, so instances will be able to see them
- define_method(name) { send("_#{name}") } if options[:instance_reader] != false
- define_method("#{name}?") { !!send("#{name}") } if options[:instance_reader] != false
+ # If an instance_reader is needed, generate public instance methods name and name?.
+ if options[:instance_reader] != false
+ define_method(name) { send("_#{name}") }
+ define_method("#{name}?") { send("#{name}").present? }
@dmathieu Collaborator

:+1:

@rafaelfranca Owner

This is cosmetic change so it should not be included in this commit

@rafaelfranca But you had asked to change these kind of changes in Issues. Here same condition is used two times.

@rafaelfranca Owner

ok, but remove the present?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...ctive_support/core_ext/class/delegating_attributes.rb
((14 lines not shown))
singleton_class.send(:define_method, name) { send("_#{name}") }
- singleton_class.send(:define_method, "#{name}?") { !!send("_#{name}") }
+ singleton_class.send(:define_method, "#{name}?") { send("_#{name}").present? }
@rafaelfranca Owner

Please don't use present? here. There is no need for this change

@rafaelfranca But it looks more readable. Am I missing something?

@rafaelfranca Owner

Yes, you are adding an unnecessary dependency for this file.

Okay I will remove it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@akshay-vishnoi

@rafaelfranca Updated. Please have look at it now

@rafaelfranca rafaelfranca merged commit 21fe17a into rails:master
@rafaelfranca

Thank you

@akshay-vishnoi akshay-vishnoi deleted the akshay-vishnoi:refactor branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 19, 2013
  1. @akshay-vishnoi

    Prevent creation of instance methods when `instance_reader = false`, …

    akshay-vishnoi authored
    …Grammar checks, Conditional statements combined
This page is out of date. Refresh to see the latest.
View
17 activesupport/lib/active_support/core_ext/class/delegating_attributes.rb
@@ -4,20 +4,21 @@
class Class
def superclass_delegating_accessor(name, options = {})
# Create private _name and _name= methods that can still be used if the public
- # methods are overridden. This allows
- _superclass_delegating_accessor("_#{name}")
+ # methods are overridden.
+ _superclass_delegating_accessor("_#{name}", options)
- # Generate the public methods name, name=, and name?
+ # Generate the public methods name, name=, and name?.
@dmathieu Collaborator

An interrogation point is a point. You don't need one after that.

It is not an interrogation statement and ? belongs to method name?.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
# These methods dispatch to the private _name, and _name= methods, making them
- # overridable
+ # overridable.
singleton_class.send(:define_method, name) { send("_#{name}") }
singleton_class.send(:define_method, "#{name}?") { !!send("_#{name}") }
singleton_class.send(:define_method, "#{name}=") { |value| send("_#{name}=", value) }
- # If an instance_reader is needed, generate methods for name and name= on the
- # class itself, so instances will be able to see them
- define_method(name) { send("_#{name}") } if options[:instance_reader] != false
- define_method("#{name}?") { !!send("#{name}") } if options[:instance_reader] != false
+ # If an instance_reader is needed, generate public instance methods name and name?.
+ if options[:instance_reader] != false
+ define_method(name) { send("_#{name}") }
+ define_method("#{name}?") { !!send("#{name}") }
+ end
end
private
View
5 activesupport/test/core_ext/class/delegating_attributes_test.rb
@@ -39,10 +39,13 @@ def test_simple_accessor_declaration
end
def test_simple_accessor_declaration_with_instance_reader_false
+ _instance_methods = single_class.public_instance_methods
single_class.superclass_delegating_accessor :no_instance_reader, :instance_reader => false
assert_respond_to single_class, :no_instance_reader
assert_respond_to single_class, :no_instance_reader=
- assert !single_class.public_instance_methods.map(&:to_s).include?("no_instance_reader")
+ assert !_instance_methods.include?(:no_instance_reader)
+ assert !_instance_methods.include?(:no_instance_reader?)
+ assert !_instance_methods.include?(:_no_instance_reader)
end
def test_working_with_simple_attributes
Something went wrong with that request. Please try again.