Sha256: ef4b7f07b473e83a0ab09123c5a99a0b40a3bf5f1e77ba5d3dc012ea894360c4

Contents?: true

Size: 1.97 KB

Versions: 41

Compression:

Stored size: 1.97 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("Base::").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

41 entries across 41 versions & 1 rubygems

Version Path
coveragebook_components-0.10.0 lib/generators/coco/coco_generator.rb
coveragebook_components-0.9.1 lib/generators/coco/coco_generator.rb
coveragebook_components-0.9.0 lib/generators/coco/coco_generator.rb
coveragebook_components-0.8.9 lib/generators/coco/coco_generator.rb
coveragebook_components-0.8.8 lib/generators/coco/coco_generator.rb
coveragebook_components-0.8.7 lib/generators/coco/coco_generator.rb
coveragebook_components-0.8.6 lib/generators/coco/coco_generator.rb
coveragebook_components-0.8.5 lib/generators/coco/coco_generator.rb
coveragebook_components-0.8.4 lib/generators/coco/coco_generator.rb
coveragebook_components-0.8.3 lib/generators/coco/coco_generator.rb
coveragebook_components-0.8.2 lib/generators/coco/coco_generator.rb
coveragebook_components-0.8.1 lib/generators/coco/coco_generator.rb
coveragebook_components-0.8.0 lib/generators/coco/coco_generator.rb
coveragebook_components-0.8.0.beta.3 lib/generators/coco/coco_generator.rb
coveragebook_components-0.8.0.beta.2 lib/generators/coco/coco_generator.rb
coveragebook_components-0.8.0.beta.1 lib/generators/coco/coco_generator.rb
coveragebook_components-0.7.10 lib/generators/coco/coco_generator.rb
coveragebook_components-0.7.9 lib/generators/coco/coco_generator.rb
coveragebook_components-0.7.8 lib/generators/coco/coco_generator.rb
coveragebook_components-0.7.7 lib/generators/coco/coco_generator.rb