Class: NitroKit::Component

Inherits:
Phlex::HTML
  • Object
show all
Defined in:
app/components/nitro_kit/component.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}, **defaults) ⇒ Component

Returns a new instance of Component.



5
6
7
# File 'app/components/nitro_kit/component.rb', line 5

def initialize(attrs = {}, **defaults)
  @attrs = merge_attrs(attrs, **defaults)
end

Instance Attribute Details

#attrsObject (readonly)

Returns the value of attribute attrs.



9
10
11
# File 'app/components/nitro_kit/component.rb', line 9

def attrs
  @attrs
end

Class Method Details

.builder_method(method_name) ⇒ Object

Class-level helper method for builder style components. When called from erb or templates, we need to wrap the return value in capture so we don’t write to the output buffer immediately. However when called from other components, we don’t.



25
26
27
28
29
30
31
# File 'app/components/nitro_kit/component.rb', line 25

def self.builder_method(method_name)
  warn(
    "[DEPRECATION] builder_method is deprecated. Please migrate to using the builder(&) pattern. See https://github.com/mikker/nitro_kit/issues/35 for details."
  )

  nil
end

.from_template(*args, **attrs, &block) ⇒ Object



11
12
13
# File 'app/components/nitro_kit/component.rb', line 11

def self.from_template(*args, **attrs, &block)
  new(*args, **attrs, &block).tap { |instance| instance.instance_variable_set(:@_nk_from_template, true) }
end

Instance Method Details

#builder(&block) ⇒ Object



15
16
17
# File 'app/components/nitro_kit/component.rb', line 15

def builder(&block)
  @_nk_from_template && !self.is_a?(NitroKit::Component) ? capture(&block) : yield
end