Sha256: 47f5b933ae3ab30120efca14e3393a05fb09180706ff6657d240eba7d8c374e6

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

require "erb"
require "dry/files"

module Hanami
  module CLI
    module Generators
      module App
        # @api private
        # @since 2.2.0
        class Component
          # @api private
          # @since 2.2.0
          def initialize(fs:, inflector:)
            @fs = fs
            @inflector = inflector
          end

          # @api private
          # @since 2.2.0
          def call(app, key, slice)
            context = ComponentContext.new(inflector, app, slice, key)

            if slice
              generate_for_slice(context, slice)
            else
              generate_for_app(context)
            end
          end

          private

          attr_reader :fs

          attr_reader :inflector

          def generate_for_slice(context, slice)
            slice_directory = fs.join("slices", slice)
            raise MissingSliceError.new(slice) unless fs.directory?(slice_directory)

            fs.mkdir(directory = fs.join(slice_directory, context.namespaces))
            fs.write(fs.join(directory, "#{context.underscored_name}.rb"), t("slice_component.erb", context))
          end

          def generate_for_app(context)
            fs.mkdir(directory = fs.join("app", context.namespaces))
            fs.write(fs.join(directory, "#{context.underscored_name}.rb"), t("component.erb", context))
          end

          def template(path, context)
            ERB.new(
              File.read(__dir__ + "/component/#{path}")
            ).result(context.ctx)
          end

          alias_method :t, :template
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hanami-cli-2.2.1 lib/hanami/cli/generators/app/component.rb
hanami-cli-2.2.0 lib/hanami/cli/generators/app/component.rb