Class: Sirens::AbstractComponent

Inherits:
Object
  • Object
show all
Defined in:
lib/components/abstract_component.rb

Direct Known Subclasses

Component, PrimitiveComponent

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(props = Hash[]) ⇒ AbstractComponent

Initializes this component.



19
20
21
22
23
24
25
26
27
# File 'lib/components/abstract_component.rb', line 19

def initialize(props = Hash[])
    super()

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

    @view = create_view
end

Class Method Details

.open(props = Hash[]) ⇒ Object

Todo: Opens a new window with this component. Currently only works for actual Windows.



8
9
10
11
12
# File 'lib/components/abstract_component.rb', line 8

def self.open(props = Hash[])
    self.new(props).tap { |window|
        window.open
    }
end

Instance Method Details

#add_all_components(components) ⇒ Object

Adds the child_component to this component.



109
110
111
112
113
114
115
# 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) ⇒ Object

Adds the child_component to this component.



98
99
100
101
102
103
104
# 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_componentsObject

Returns the child components of this component.



57
58
59
# File 'lib/components/abstract_component.rb', line 57

def child_components()
    @child_components
end

#create_viewObject

Creates the PrimitiveView that this component wraps. This method must be implemented by each subclass.

Raises:

  • (RuntimeError)


33
34
35
# 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_modelObject

Returns a default model if none is given during the initialization of this component.



64
65
66
# File 'lib/components/abstract_component.rb', line 64

def default_model()
    nil
end

#modelObject

Returns the current model of this component.



71
72
73
# File 'lib/components/abstract_component.rb', line 71

def model()
    @props[:model]
end

#on_component_added(child_component) ⇒ Object

This method is called right after adding a child component. Subclasses may use it to perform further configuration on this component or its children.



139
140
# File 'lib/components/abstract_component.rb', line 139

def on_component_added(child_component)
end

#on_model_changed(new_model:, old_model:) ⇒ Object



142
143
# File 'lib/components/abstract_component.rb', line 142

def on_model_changed(new_model:, old_model:)
end

#propsObject

Returns this component props.



42
43
44
# File 'lib/components/abstract_component.rb', line 42

def props()
    @props
end

#remove_component_at(index:) ⇒ Object

Removes the component at the index-th position.



127
128
129
130
131
132
133
# 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_componentObject

Removes the last child component and returns it.



120
121
122
# 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) ⇒ Object

Sets the current model of this component.



78
79
80
81
82
83
84
# 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) ⇒ Object

Applies the given props to the current component.props. The component.props that are not included in given props remain untouched.



50
51
52
# File 'lib/components/abstract_component.rb', line 50

def set_props(props)
    @props.merge!(props)
end

#viewObject

Returns this component View.



89
90
91
# File 'lib/components/abstract_component.rb', line 89

def view()
    @view
end