class Sirens::AbstractComponent

Public Class Methods

new(props = Hash[]) click to toggle source

Initializes this component.

Calls superclass method
# File lib/components/abstract_component.rb, line 19
def initialize(props = Hash[])
    super()

    @props = Hash[]
    @child_components = []
    @props = props

    @view = create_view
end
open(props = Hash[]) click to toggle source

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

add_all_components(components) click to toggle source

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
add_component(child_component) click to toggle source

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
child_components() click to toggle source

Returns the child components of this component.

# File lib/components/abstract_component.rb, line 57
def child_components()
    @child_components
end
create_view() click to toggle source

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
default_model() click to toggle source

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
model() click to toggle source

Returns the current model of this component.

# File lib/components/abstract_component.rb, line 71
def model()
    @props[:model]
end
on_component_added(child_component) click to toggle source

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
on_model_changed(new_model:, old_model:) click to toggle source
# File lib/components/abstract_component.rb, line 142
def on_model_changed(new_model:, old_model:)
end
props() click to toggle source

Returns this component props.

# File lib/components/abstract_component.rb, line 42
def props()
    @props
end
remove_component_at(index:) click to toggle source

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
remove_last_component() click to toggle source

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
set_model(new_model) click to toggle source

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
set_props(props) click to toggle source

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
view() click to toggle source

Returns this component View.

# File lib/components/abstract_component.rb, line 89
def view()
    @view
end