-
Notifications
You must be signed in to change notification settings - Fork 1
Allow to inherit one page from another #4
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,10 @@ class Tram::Page | |
class Section | ||
extend Dry::Initializer | ||
param :name, proc(&:to_sym) | ||
option :method, proc(&:to_s), default: -> { name }, as: :method_name | ||
option :value, proc(&:to_proc), optional: true, as: :block | ||
option :if, proc(&:to_s), optional: true, as: :positive | ||
option :unless, proc(&:to_s), optional: true, as: :negative | ||
option :skip, true.method(:&), optional: true | ||
option :method, proc(&:to_s), default: -> { name } | ||
option :value, proc(&:to_proc), optional: true | ||
option :if, proc(&:to_s), optional: true | ||
option :unless, proc(&:to_s), optional: true | ||
|
||
# @param [Tram::Page] page | ||
# @return [Hash] a part of the section | ||
|
@@ -24,13 +23,20 @@ def call(page) | |
private | ||
|
||
def skip_on?(page) | ||
return true if skip | ||
return true if positive && !page.public_send(positive) | ||
return true if negative && page.public_send(negative) | ||
return true if attributes[:if] && !page.__send__(attributes[:if]) | ||
return true if attributes[:unless] && page.__send__(attributes[:unless]) | ||
end | ||
|
||
def value_at(page) | ||
block ? page.instance_exec(&block) : page.public_send(method_name) | ||
if attributes[:value] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
page.instance_exec(&attributes[:value]) | ||
else | ||
page.public_send(attributes[:method]) | ||
end | ||
end | ||
|
||
def attributes | ||
@attributes ||= self.class.dry_initializer.attributes(self) | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
class InheritedPage < ExamplePage | ||
inherit_section :foo | ||
inherit_section :bar, if: :no_bar | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this DSL breaks the Less Surprise Principle. When I inherit a page from another one, I expect the children to have all the parent's sections (sections is the main responsibility for What I'd like is something like: class InheritedPage < ExamplePage
# section :foo is already here from `ExamplePage`
section :bar, if: :no_bar, reload: true # tells explicitly: "I know that :bar is already here, and yes, I want to reload it"
section :bam
end |
||
section :bam | ||
|
||
def bam | ||
baz.upcase | ||
end | ||
|
||
def no_bar | ||
!bar | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe InheritedPage do | ||
subject { described_class.new("test") } | ||
|
||
it "returns data hash with new and without skipped fields" do | ||
expect(subject.to_h).to eq(foo: "test", bam: "TEST") | ||
end | ||
|
||
it "doesn't change behaviour of the parent class" do | ||
expect(described_class.superclass.new("test").to_h).to \ | ||
eq(bar: "test", foo: "test", baz: "test") | ||
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 think we'd better rename these methods because the original ones can provide problems during debugging
(but I agree to not renaming
value
toblock
)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.
Not to mention using
attributes
adds a bunch of excessive steps in a runtime