-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify attributes handling. #1370
Conversation
On first read looks great! BTW, that's why I set the things to api private :) |
Up to you B mobile phone
|
Added a commit to avoid storing a lambda calling a proc. How are we feeling on this PR? |
if self.class._attribute_procs[name] | ||
instance_eval(&self.class._attribute_procs[name]) | ||
else | ||
read_attribute_for_serialization(name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- so, I kind of like the polymorphism better.
- but at the end of the day, this is less code and the switch is entirely contained here, which is good
- on the other hand, now there's a data clump of
_attribute_procs
and_attribute_keys
passed around - and
_attribute_keys
sounds a lot like_attributes_keys
, which seems to be a bug attractor to me - but
_attribute_value(name)
is a nicer interface thanattribute_mapping.call(self)
and is more consistent with the reflection value
So, how about
class Attribute
attr_reader :name, :block
def initialize(attr, block = nil)
@name = attr
@block = block
end
def value(instance)
if @block
instance.instance_eval(&block)
else
instance.read_attribute_for_serialization(name)
end
end
end
and we've removed storing the unnecessary lambdas, introduced a smaller change to the serializer interface, and isolated the attribute vs. block switch with a consistent interface to Reflection?
def attribute(attr, options = {}, &block)
key = options.fetch(:key, attr)
_attribute_mappings[key] = Attribute.new(attr, block)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@beauby your thoughts?
failures in CI probably fixed by #1384 |
a7e2fbb
to
8fe3ecf
Compare
@bf4 How about now? |
else | ||
serializer.read_attribute_for_serialization(name) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel better about this l, since it appears to be a pattern we're using all over. However, I still don't have a good sense if we'll want to keep this style, and would like to mark the object as part of the internal api.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Every reference to an Attribute
object is currently marked as @api private
.
end | ||
|
||
autoload :Attribute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer the autoload
below the extend ActiveSupport::Concern
. I believe that's how we're doing it elsewhere. Any particular reason it's here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or should it be auto-loaded at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't feel strongly about this but I followed what was in Associations. However, I didn't put the extend ActiveSupport::Autoload
, which usually goes above the autoload :things
. What's the use of extend ActiveSupport::Autoload
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a different autoload from the one in ruby. I forget what it adds. Autoload is now thread safe in our supported rubies
B mobile phone
On Dec 27, 2015, at 7:19 PM, Lucas Hosseini [email protected] wrote:
In lib/active_model/serializer/attributes.rb:
end
I don't feel strongly about this but I followed what was in Associations. However, I didn't put the extend ActiveSupport::Autoload, which usually goes above the autoload :things. What's the use of extend ActiveSupport::Autoload?autoload :Attribute
—
Reply to this email directly or view it on GitHub.
d39aec2
to
a586a45
Compare
end | ||
|
||
extend ActiveSupport::Autoload | ||
autoload :Attribute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I don't know that referring to this as a mixin is a useful changelog entry, but I'm going to merge it.. just to end the back and forth on it ;)
Simplify attributes handling via a mixin
ref: #1356 (comment)
Rationale: I believe the level of abstraction introduced here adds more complexity than required.