class Sirens::AbstractComponent
Public Class Methods
Initializes this component.
# File lib/components/abstract_component.rb, line 19 def initialize(props = Hash[]) super() @props = Hash[] @child_components = [] @props = props @view = create_view end
Todo: Opens a new window with this component. Currently only works for actual Windows.
# File lib/components/abstract_component.rb, line 8 def self.open(props = Hash[]) self.new(props).tap { |window| window.open } end
Public Instance Methods
Adds the child_component to this component.
# File lib/components/abstract_component.rb, line 109 def add_all_components(components) components.each do |child_component| add_component(child_component) end self end
Adds the child_component to this component.
# File lib/components/abstract_component.rb, line 98 def add_component(child_component) @child_components << child_component on_component_added(child_component) child_component end
Returns the child components of this component.
# File lib/components/abstract_component.rb, line 57 def child_components() @child_components end
Creates the PrimitiveView that this component wraps. This method must be implemented by each subclass.
# File lib/components/abstract_component.rb, line 33 def create_view() raise RuntimeError.new("Class #{self.class.name} must implement a ::create_view() method.") end
Returns a default model if none is given during the initialization of this component.
# File lib/components/abstract_component.rb, line 64 def default_model() nil end
Returns the current model of this component.
# File lib/components/abstract_component.rb, line 71 def model() @props[:model] end
This method is called right after adding a child component. Subclasses may use it to perform further configuration on this component or its children.
# File lib/components/abstract_component.rb, line 139 def on_component_added(child_component) end
# File lib/components/abstract_component.rb, line 142 def on_model_changed(new_model:, old_model:) end
Returns this component props.
# File lib/components/abstract_component.rb, line 42 def props() @props end
Removes the component at the index-th position.
# File lib/components/abstract_component.rb, line 127 def remove_component_at(index:) component = @child_components.delete_at(index) @view.remove_view(component.view) component end
Removes the last child component and returns it.
# File lib/components/abstract_component.rb, line 120 def remove_last_component() remove_component_at(index: @child_components.size - 1) end
Sets the current model of this component.
# File lib/components/abstract_component.rb, line 78 def set_model(new_model) old_model = model @props[:model] = new_model on_model_changed(new_model: new_model, old_model: old_model) end
Applies the given props to the current component.props. The component.props that are not included in given props remain untouched.
# File lib/components/abstract_component.rb, line 50 def set_props(props) @props.merge!(props) end
Returns this component View.
# File lib/components/abstract_component.rb, line 89 def view() @view end