Sha256: a5ffa0d07816535f65d3d3a3997488c0d1927bef393e7b45aff21e4a45c0c3e9

Contents?: true

Size: 1.98 KB

Versions: 32

Compression:

Stored size: 1.98 KB

Contents

class CocoGenerator < Rails::Generators::NamedBase
  source_root File.expand_path("templates", __dir__)

  argument :attributes, type: :array, default: [], banner: "attribute"

  def create_component_file
    template "component.rb", component_file_path
  end

  def create_template_file
    template "component.html.erb", template_file_path
  end

  def create_css_file
    template "component.css", css_file_path
  end

  def create_js_file
    template "component.js", js_file_path
  end

  def create_preview_file
    template "preview.rb", preview_file_path
  end

  def create_test_file
    template "test.rb", test_file_path
  end

  private

  def component_dir
    File.join("app/components", class_path, file_name)
  end

  def component_file_path
    File.join(component_dir, "component.rb")
  end

  def template_file_path
    File.join(component_dir, "component.html.erb")
  end

  def js_file_path
    File.join(component_dir, "component.js")
  end

  def css_file_path
    File.join(component_dir, "component.css")
  end

  def preview_file_path
    File.join("lookbook/app/previews", class_path_no_coco, "#{file_name}_component_preview.rb")
  end

  def test_file_path
    File.join("test/components", class_path_no_coco, "#{file_name}_component_test.rb")
  end

  def modules_count
    class_namespace_modules.size
  end

  def indent(level)
    level.times.collect { "\t" }.join
  end

  def zeitwerk_class_name
    class_name.split("::").filter { !%w[Elements Blocks Layouts Base].include?(_1) }.join("::")
  end

  def class_path_no_coco
    class_path.drop(1)
  end

  def component_class
    class_name.split("::").last.to_s
  end

  def class_namespace_modules
    parts = class_name.split("::")
    parts.first parts.size - 1
  end

  def component_ref
    parts = class_name.delete_prefix("Shared::").underscore.dasherize.downcase.split("/")
    parts.many? ? [parts.first, parts.last].join("-") : parts.first
  end

  def component_js_name
    component_ref.tr("-", "_").camelize(:lower)
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
coveragebook_components-0.19.8 lib/generators/coco/coco_generator.rb
coveragebook_components-0.19.7 lib/generators/coco/coco_generator.rb
coveragebook_components-0.19.6 lib/generators/coco/coco_generator.rb
coveragebook_components-0.19.5 lib/generators/coco/coco_generator.rb
coveragebook_components-0.19.4 lib/generators/coco/coco_generator.rb
coveragebook_components-0.19.3 lib/generators/coco/coco_generator.rb
coveragebook_components-0.19.2 lib/generators/coco/coco_generator.rb
coveragebook_components-0.19.1 lib/generators/coco/coco_generator.rb
coveragebook_components-0.19.0 lib/generators/coco/coco_generator.rb
coveragebook_components-0.18.8 lib/generators/coco/coco_generator.rb
coveragebook_components-0.18.7 lib/generators/coco/coco_generator.rb
coveragebook_components-0.18.0 lib/generators/coco/coco_generator.rb
coveragebook_components-0.17.7 lib/generators/coco/coco_generator.rb
coveragebook_components-0.17.6 lib/generators/coco/coco_generator.rb
coveragebook_components-0.17.5 lib/generators/coco/coco_generator.rb
coveragebook_components-0.17.4 lib/generators/coco/coco_generator.rb
coveragebook_components-0.17.3 lib/generators/coco/coco_generator.rb
coveragebook_components-0.17.2 lib/generators/coco/coco_generator.rb
coveragebook_components-0.17.1 lib/generators/coco/coco_generator.rb
coveragebook_components-0.17.0 lib/generators/coco/coco_generator.rb