-
Notifications
You must be signed in to change notification settings - Fork 1
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
Refactor section definitions (extract sections) #5
Conversation
end | ||
Hash[data] | ||
def to_h(except: nil, only: nil, **) | ||
obj = self.class.sections.values.map { |s| s.call(self) }.reduce({}, :merge) |
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 think it is somewhat not optimal to calculate all values and then throw out some or most of them.
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.
Ahh! Yes, you are right
It would be better to filter out sections, not calculated values
end | ||
Hash[data] | ||
def to_h(except: nil, only: nil, **) | ||
obj = self.class.sections.values.map { |s| s.call(self) }.reduce({}, :merge) |
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.
Also, it is worth to use merge!
to reduce the number of allocations.
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.
👍
def to_h(except: nil, only: nil, **) | ||
obj = self.class.sections.values.map { |s| s.call(self) }.reduce({}, :merge) | ||
obj = obj.reject { |k, _| Array(except).map(&:to_sym).include? k } if except | ||
obj = obj.select { |k, _| Array(only).map(&:to_sym).include? k } if only |
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.
reject!
and select!
too may help to reduce memory footprint.
@Envek please add your fixes to your PR |
@gzigzigzeo Here I've just the proposed refactoring without adding any new features.
@Envek Now you can prepare you changes by reloading section definitions either implicitly, or explicitly (for example, you could add
option :reload, true.method(:&), optional: true
to the section and check it inTram::Page#section
). Be aware of https://github.com/tram-rb/tram-page/pull/5/files#diff-f279a26eb24bc37345ac55d12c85bbb0R13I think, you guys should define an appropriate behavior that suites the both commercial projects.