Class: PhlexCustomElementGenerator::ComponentGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/phlex_custom_element_generator/component_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name:, tag_name:, namespaces: [], attributes: []) ⇒ ComponentGenerator

Returns a new instance of ComponentGenerator.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/phlex_custom_element_generator/component_generator.rb', line 5

def initialize(class_name:, tag_name:, namespaces: [], attributes: [])
  @class_name = class_name
  @tag_name = tag_name.gsub(/-/, "_")
  @namespaces = namespaces
  @attributes = attributes

  @component_code = <<~RUBY
class #{@class_name} < Phlex::HTML
  register_element :#{@tag_name}

  def initialize(
#{attributes_to_kwargs}
  )
@attributes = attributes.with_defaults({#{attribute_hash.to_s.empty? ? "" : "\n    " + attribute_hash}
})
  end

  def view_template(&)
#{@tag_name}(**@attributes, &)
  end
end
RUBY
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



3
4
5
# File 'lib/phlex_custom_element_generator/component_generator.rb', line 3

def attributes
  @attributes
end

#class_nameObject

Returns the value of attribute class_name.



3
4
5
# File 'lib/phlex_custom_element_generator/component_generator.rb', line 3

def class_name
  @class_name
end

#component_codeObject

Returns the value of attribute component_code.



3
4
5
# File 'lib/phlex_custom_element_generator/component_generator.rb', line 3

def component_code
  @component_code
end

#namespacesObject

Returns the value of attribute namespaces.



3
4
5
# File 'lib/phlex_custom_element_generator/component_generator.rb', line 3

def namespaces
  @namespaces
end

#tag_nameObject

Returns the value of attribute tag_name.



3
4
5
# File 'lib/phlex_custom_element_generator/component_generator.rb', line 3

def tag_name
  @tag_name
end

Instance Method Details

#attribute_hashObject



36
37
38
39
40
# File 'lib/phlex_custom_element_generator/component_generator.rb', line 36

def attribute_hash
  @attributes.length > 0 ?
    @attributes.map { |attr| "#{attr[:attr_name]}: #{attr[:attr_name]}" }.join(",\n      ")
    : ""
end

#attributes_to_kwargsObject



30
31
32
33
34
# File 'lib/phlex_custom_element_generator/component_generator.rb', line 30

def attributes_to_kwargs
  @attributes.length > 0 ?
    @attributes.map { |attr| "#{attr[:attr_name]}: #{(%w[undefined null].include?(attr[:default_value]) ? "nil" : attr[:default_value]) || "nil"}" }.join(",\n    ") + ",\n    **attributes"
    : "**attributes"
end

#createObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/phlex_custom_element_generator/component_generator.rb', line 42

def create
  str = ""

  if namespaces.length <= 0
    str += @component_code
    return str
  end

  namespaces.each.with_index do |namespace, index|
    indent = "  " * index
    str += "#{indent}module #{namespace}\n"
    if index == namespaces.length - 1
      indent = indent + "  "
      str += indent + @component_code
        .split(/\n/)
        .join("\n#{indent}") # Add indentations
        .split(/\n/)
        .map(&:rstrip) # Remove extra spaces in lines that are newlines only.
        .join("\n")
        .chomp
      str += "\n"
    end
  end

  namespaces.each.with_index do |namespace, index|
    indent = "  " * (namespaces.length - 1 - index)
    str += "#{indent}end\n"
  end
  str
end