-
Notifications
You must be signed in to change notification settings - Fork 0
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
Implemented Tramway Form inheritance #44
Implemented Tramway Form inheritance #44
Conversation
lib/tramway/base_form.rb
Outdated
end | ||
end | ||
|
||
def __properties | ||
@properties ||= [] |
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 see that you're using @properties ||= []
several times in the base_form.rb class. You could initializes the @properties class instance variable one time when the class is loaded
class << self
@properties = []
def property(attribute)
(__ancestor_properties + @properties).uniq
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've never seen this approach. Are you sure that initializing class-level variables out of methods is nice? 🙂
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.
It's for sure nicer to have one place of default initialization, and I dont see problems with class level variable.
But I also haven't used this approach much, so can't comment if it have no problems at all
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've tested this approach. For some reason, @properties
is not initialized on class load.
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.
Ah, I see, the variable initialization doesn't pass to child classes. Sorry, then, my bad :)
The initialization still could be isolated with inherited
hook, if you willing to go that path, like this
class Parent
def self.inherited(subclass)
subclass.instance_variable_set(:@things, [])
end
class << self
def add(value)
@things << value
end
end
end
class ChildDecorator < Parent
add :third
add :four
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.
Nice! 🙂 Thanks!!
btw, Linters are v ahuie 😆
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.
btw, Linters are v ahuie
They are right, it should be @properties
, not @@properties
:) @@properties
will give you effects you don't want, like - if you change child properties, the parent properties also will be changed
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.
Maybe I missed something, but looks like we can't use @properties
in our case.
subclass.class_variable_set(:@properties, __ancestor_properties)
return NameError: '@properties' is not allowed as a class variable name
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.
because it should be instance_variable_set
, not class_variable_set
. As a class is actually instance of Class itself, and class variables are @@ ones
@@ -1,5 +1,6 @@ | |||
# frozen_string_literal: true | |||
|
|||
# :reek:ClassVariable { enabled: false } |
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.
@kalashnikovisme this shouldn't be needed
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.
Will fix in the next PR
What's changed basically?
Tramway Form inheritance implemented
What's changed for tramway drivers?
Example
Before
After
Playlist for the code-review (Spotify)
Find more info here