Module: Ruwi

Defined in:
lib/ruwi.rb,
lib/ruwi/version.rb,
lib/ruwi/cli/command.rb,
lib/ruwi/runtime/app.rb,
lib/ruwi/runtime/dom.rb,
lib/ruwi/runtime/vdom.rb,
lib/ruwi/runtime/utils.rb,
lib/ruwi/cli/command/dev.rb,
lib/ruwi/cli/command/base.rb,
lib/ruwi/cli/command/pack.rb,
lib/ruwi/runtime/template.rb,
lib/ruwi/cli/command/setup.rb,
lib/ruwi/runtime/component.rb,
lib/ruwi/runtime/dispatcher.rb,
lib/ruwi/runtime/dom/events.rb,
lib/ruwi/cli/command/rebuild.rb,
lib/ruwi/runtime/nodes_equal.rb,
lib/ruwi/runtime/utils/props.rb,
lib/ruwi/runtime/utils/arrays.rb,
lib/ruwi/runtime/dom/mount_dom.rb,
lib/ruwi/runtime/dom/patch_dom.rb,
lib/ruwi/runtime/dom/scheduler.rb,
lib/ruwi/runtime/utils/objects.rb,
lib/ruwi/runtime/utils/strings.rb,
lib/ruwi/runtime/dom/attributes.rb,
lib/ruwi/runtime/dom/destroy_dom.rb,
lib/ruwi/runtime/template/parser.rb,
lib/ruwi/runtime/template/build_vdom.rb,
lib/ruwi/runtime/template/build_for_group.rb,
lib/ruwi/runtime/template/build_conditional_group.rb

Defined Under Namespace

Modules: Cli, Dom, NodesEqual, Template, Utils Classes: App, Component, Dispatcher, Vdom

Constant Summary collapse

VERSION =
"0.10.0"

Class Method Summary collapse

Class Method Details

.define_component(template:, state: nil, on_mounted: empty_proc, on_unmounted: empty_proc, methods: {}) ⇒ Class

Define a new component class

Parameters:

  • template (Proc)

    The template function

  • state (Proc, nil) (defaults to: nil)

    The state function

  • methods (Hash) (defaults to: {})

    Additional methods to add to the component

Returns:

  • (Class)

    The new component class



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruwi/runtime/component.rb', line 12

def self.define_component(template:, state: nil, on_mounted: empty_proc, on_unmounted: empty_proc, methods: {})
  Class.new(Component) do
    self.class_variable_set(:@@state, state)
    self.class_variable_set(:@@template, template)
    self.class_variable_set(:@@on_mounted, on_mounted)
    self.class_variable_set(:@@on_unmounted, on_unmounted)

    # Add methods to the component
    methods.each do |method_name, method_proc|
      # Check if method already exists
      if method_defined?(method_name) || private_method_defined?(method_name)
        raise "Method \"#{method_name}()\" already exists in the component."
      end

      # Define the method dynamically
      define_method(method_name, method_proc)
    end
  end
end

.empty_procObject



3
4
5
# File 'lib/ruwi/runtime/component.rb', line 3

def self.empty_proc
  -> { }
end