File: abstract_generator.rb

package info (click to toggle)
ruby-view-component 4.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 3,832 kB
  • sloc: ruby: 8,385; sh: 166; makefile: 4
file content (56 lines) | stat: -rw-r--r-- 1,315 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true

module ViewComponent
  module AbstractGenerator
    def copy_view_file
      template("component.html.#{engine_name}", destination) unless options["inline"] || options["call"]
    end

    private

    def destination
      File.join(destination_directory, "#{destination_file_name}.html.#{engine_name}")
    end

    def destination_directory
      if sidecar?
        File.join(component_path, class_path, destination_file_name)
      else
        File.join(component_path, class_path)
      end
    end

    def destination_file_name
      "#{file_name}_component"
    end

    def file_name
      @_file_name ||= super.sub(/_component\z/i, "")
    end

    def component_path
      ViewComponent::Base.config.generate.path
    end

    def stimulus_controller
      if stimulus?
        File.join(destination_directory, destination_file_name)
          .sub("#{component_path}/", "")
          .tr("_", "-")
          .gsub("/", "--")
      end
    end

    def sidecar?
      options["sidecar"] || ViewComponent::Base.config.generate.sidecar
    end

    def stimulus?
      options["stimulus"] || ViewComponent::Base.config.generate.stimulus_controller
    end

    def typescript?
      options["typescript"] || ViewComponent::Base.config.generate.typescript
    end
  end
end