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

Refactor section definitions (extract sections) #5

Merged
merged 1 commit into from
Nov 26, 2017
Merged

Conversation

nepalez
Copy link
Member

@nepalez nepalez commented Nov 25, 2017

@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 in Tram::Page#section). Be aware of https://github.com/tram-rb/tram-page/pull/5/files#diff-f279a26eb24bc37345ac55d12c85bbb0R13

I think, you guys should define an appropriate behavior that suites the both commercial projects.

end
Hash[data]
def to_h(except: nil, only: nil, **)
obj = self.class.sections.values.map { |s| s.call(self) }.reduce({}, :merge)
Copy link
Member

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.

Copy link
Member Author

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

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

@nepalez
Copy link
Member Author

nepalez commented Nov 26, 2017

@Envek feel free to take necessary parts of code from here to your PR #4 and throw our unnecessary ;)

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

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.

@nepalez nepalez merged commit ce6a2f6 into master Nov 26, 2017
@nepalez
Copy link
Member Author

nepalez commented Nov 26, 2017

@Envek please add your fixes to your PR
I couldn’t do this untul the night

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants