Skip to content
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

Merged
merged 6 commits into from
Jan 4, 2016
Merged

Conversation

beauby
Copy link
Contributor

@beauby beauby commented Dec 10, 2015

ref: #1356 (comment)

Rationale: I believe the level of abstraction introduced here adds more complexity than required.

@bf4
Copy link
Member

bf4 commented Dec 10, 2015

On first read looks great!

BTW, that's why I set the things to api private :)

@beauby
Copy link
Contributor Author

beauby commented Dec 11, 2015

@bf4 yup, sorry I didn't make it in time for #1356. I'm not sure this PR deserves a changelog though.

@bf4
Copy link
Member

bf4 commented Dec 11, 2015

Up to you

B mobile phone

On Dec 11, 2015, at 4:21 PM, Lucas Hosseini notifications@github.com wrote:

@bf4 yup, sorry I didn't make it in time for #1356. I'm not sure this PR deserves a changelog though.


Reply to this email directly or view it on GitHub.

@beauby
Copy link
Contributor Author

beauby commented Dec 20, 2015

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)
Copy link
Member

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 than attribute_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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@beauby your thoughts?

@bf4
Copy link
Member

bf4 commented Dec 24, 2015

failures in CI probably fixed by #1384

@beauby
Copy link
Contributor Author

beauby commented Dec 27, 2015

@bf4 How about now?

else
serializer.read_attribute_for_serialization(name)
end
end
Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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?

Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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 notifications@github.com wrote:

In lib/active_model/serializer/attributes.rb:

     end
  •    autoload :Attribute
    
    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?


Reply to this email directly or view it on GitHub.

@beauby beauby force-pushed the simplify-attributes branch from d39aec2 to a586a45 Compare December 30, 2015 15:47
end

extend ActiveSupport::Autoload
autoload :Attribute
Copy link
Member

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

bf4 added a commit that referenced this pull request Jan 4, 2016
@bf4 bf4 merged commit b51a432 into rails-api:master Jan 4, 2016
@bf4 bf4 deleted the simplify-attributes branch January 4, 2016 04:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants