Class: Irelia::Component

Inherits:
ViewComponentContrib::Base
  • Object
show all
Extended by:
Dry::Initializer
Defined in:
app/components/irelia/component.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Component

Returns a new instance of Component.



8
9
10
11
12
13
14
# File 'app/components/irelia/component.rb', line 8

def initialize(**options)
  super

  @component_classes = +""
  add_css_classes(variants)
  self.class.run_after_initialize_hooks(self)
end

Class Method Details

.after_initialize(method_name = nil, &block) ⇒ Object



29
30
31
32
33
34
35
36
# File 'app/components/irelia/component.rb', line 29

def after_initialize(method_name = nil, &block)
  @after_initialize_hooks ||= []
  if method_name
    @after_initialize_hooks << method_name
  elsif block_given?
    @after_initialize_hooks << block
  end
end

.inherited(subclass) ⇒ Object



49
50
51
52
# File 'app/components/irelia/component.rb', line 49

def inherited(subclass)
  super
  subclass.instance_variable_set(:@after_initialize_hooks, @after_initialize_hooks.dup)
end

.run_after_initialize_hooks(instance) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'app/components/irelia/component.rb', line 38

def run_after_initialize_hooks(instance)
  return unless @after_initialize_hooks
  @after_initialize_hooks.each do |hook|
    if hook.is_a?(Symbol)
      instance.send(hook)
    elsif hook.is_a?(Proc)
      instance.instance_eval(&hook)
    end
  end
end

Instance Method Details

#add_css_classes(css_class) ⇒ Object



21
22
23
# File 'app/components/irelia/component.rb', line 21

def add_css_classes(css_class)
  @component_classes += " #{css_class}"
end

#html_optionsObject



16
17
18
19
# File 'app/components/irelia/component.rb', line 16

def html_options
  # Merge the component classes with the classes from the html_options.
  @html_options.merge({ class: "#{@component_classes} #{@html_options[:class]}".strip })
end

#variantsObject



25
26
# File 'app/components/irelia/component.rb', line 25

def variants
end