Use phlex with dry-initializer #553
|
Hi! I want to use phlex with dry-initializer I tried this: But it doesn't work :( How can I make it work? |
Replies: 1 comment 1 reply
|
It looks like If you're using Dry Initializer, I recommend patching both the Internally, you can use instance variables instead of accessor methods (I think this is best practice anyway), and there’s usually no need to reference component attributes externally. I believe you can patch the methods like this. class BaseComponent < Phlex::HTML
extend Dry::Initializer
def self.option(*args, **kwargs, &block)
super(*args, reader: false, **kwargs, &block)
end
def self.param(*args, **kwargs, &block)
super(*args, reader: false, **kwargs, &block)
end
endThen you can just inherit from class Nav < BaseComponent
option :title
def template
h1 { @title }
end
end |
It looks like
paramis for positional arguments andoptionis for keyword arguments. So you would either need to useoptioninstead ofparamhere, or initialize the component with a positional argumentNav.call("Hello").If you're using Dry Initializer, I recommend patching both the
paramandoptionmethods to havereader: falseby default. Readers are likely to conflict with HTML methods (titleis a popular one you've actually run into in this example).Internally, you can use instance variables instead of accessor methods (I think this is best practice anyway), and there’s usually no need to reference component attributes externally. I believe you can patch the methods like this.