Class: LocoMotion::ComponentBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/loco_motion/component_builder.rb

Overview

Builder class for creating dynamic component subclasses with custom configurations. This class encapsulates the complex metaprogramming logic needed to create customized component instances without defining new classes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_class, build_kws = {}, &build_block) ⇒ ComponentBuilder

Initialize a new component builder.

Parameters:

  • base_class (Class)

    The base component class to build from.

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

    Keyword arguments to merge into the component config.

  • build_block (Proc)

    Optional block for customizations.



19
20
21
22
23
24
# File 'lib/loco_motion/component_builder.rb', line 19

def initialize(base_class, build_kws = {}, &build_block)
  @base_class = base_class
  @build_kws = build_kws
  @build_block = build_block
  @build_counter = base_class.instance_variable_get(:@build_counter) || 0
end

Instance Attribute Details

#base_classObject (readonly)

Returns the value of attribute base_class.



10
11
12
# File 'lib/loco_motion/component_builder.rb', line 10

def base_class
  @base_class
end

#build_blockObject (readonly)

Returns the value of attribute build_block.



10
11
12
# File 'lib/loco_motion/component_builder.rb', line 10

def build_block
  @build_block
end

#build_kwsObject (readonly)

Returns the value of attribute build_kws.



10
11
12
# File 'lib/loco_motion/component_builder.rb', line 10

def build_kws
  @build_kws
end

Instance Method Details

#buildClass

Build the customized component subclass.

Returns:

  • (Class)

    The dynamically created component subclass.



31
32
33
34
35
36
37
38
39
# File 'lib/loco_motion/component_builder.rb', line 31

def build
  klass = create_subclass
  setup_component_name(klass)
  setup_sidecar_files(klass)
  override_initialize(klass)
  apply_customizations(klass)
  increment_counter
  klass
end