Module: HoTModuLe::ClassMethods

Defined in:
lib/hot_module.rb

Overview

Extends the component class

Instance Method Summary collapse

Instance Method Details

#attribute_binding(matcher, method_name, only: nil) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/hot_module.rb', line 134

def attribute_binding(matcher, method_name, only: nil)
  attribute_bindings << AttributeBinding.new(
    matcher: Regexp.new(matcher),
    method_name: method_name,
    only_for_tag: only
  )
end

#attribute_bindingsObject



132
# File 'lib/hot_module.rb', line 132

def attribute_bindings = @attribute_bindings ||= []

#camelcased(method_symbols) ⇒ Object



64
65
66
67
68
# File 'lib/hot_module.rb', line 64

def camelcased(method_symbols)
  Array(method_symbols).each do |method_symbol|
    alias_method(method_symbol.to_s.gsub(/(?!^)_[a-z0-9]/) { |match| match[1].upcase }, method_symbol)
  end
end

#custom_element(tag_name, html_module = nil, shadow_root: true) ⇒ void

This method returns an undefined value.

Parameters:

  • tag_name (String)
  • html_module (String) (defaults to: nil)

    if not provided, a class method called ‘source_location` must be available with the absolute path of the Ruby file

  • shadow_root (Boolean) (defaults to: true)

    default is true



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/hot_module.rb', line 79

def custom_element(tag_name, html_module = nil, shadow_root: true) # rubocop:disable Metrics/AbcSize
  if html_module.nil? && !respond_to?(:source_location)
    raise HoTModuLe::Error, "You must either supply a file path argument or respond to `source_location'"
  end

  self.tag_name tag_name

  if html_module
    self.html_module html_module
  else
    basepath = File.join(File.dirname(source_location), File.basename(source_location, ".*"))

    self.html_module(html_file_extensions.lazy.filter_map do |ext|
      path = "#{basepath}.#{ext}"
      File.exist?(path) ? path : nil
    end.first)

    raise HoTModuLe::Error, "Cannot find sidecar HTML module for `#{self}'" unless @html_module
  end

  self.shadow_root shadow_root
end

#docNokolexbor::Element

Returns:

  • (Nokolexbor::Element)


120
121
122
123
124
125
# File 'lib/hot_module.rb', line 120

def doc
  @doc ||= begin
    @doc_html = "<#{tag_name}>#{File.read(html_module).strip}</#{tag_name}>"
    Nokolexbor::DocumentFragment.parse(@doc_html).first_element_child
  end
end

#html_file_extensionsObject



70
# File 'lib/hot_module.rb', line 70

def html_file_extensions = %w[module.html mod.html html].freeze

#html_module(value = nil) ⇒ String

Parameters:

  • value (String) (defaults to: nil)

Returns:

  • (String)


113
# File 'lib/hot_module.rb', line 113

def html_module(value = nil) = @html_module ||= value

#line_number_of_node(node) ⇒ Object



127
128
129
130
# File 'lib/hot_module.rb', line 127

def line_number_of_node(node)
  loc = node.source_location
  instance_variable_get(:@doc_html)[0..loc].count("\n") + 1
end

#processed_css_extensionObject



72
# File 'lib/hot_module.rb', line 72

def processed_css_extension = "css-local"

#shadow_root(value = nil) ⇒ Boolean

Parameters:

  • value (Boolean) (defaults to: nil)

Returns:

  • (Boolean)


117
# File 'lib/hot_module.rb', line 117

def shadow_root(value = nil) = @shadow_root ||= value

#tag_name(value = nil) ⇒ String

Parameters:

  • value (String) (defaults to: nil)

Returns:

  • (String)


104
105
106
107
108
109
# File 'lib/hot_module.rb', line 104

def tag_name(value = nil)
  @tag_name ||= begin
    HoTModuLe.register_element self
    value
  end
end