Sha256: d357cbad614ed43af01eaab279925cbd2dc03e1563b1e34e0a7e81b4572f59f7

Contents?: true

Size: 1.3 KB

Versions: 1

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require "erb"
require "hanami/cli/generators/app/action_context"

module Hanami
  module RSpec
    module Generators
      class Action
        def initialize(fs:, inflector:)
          @fs = fs
          @inflector = inflector
        end

        def call(app, slice, controller, action, context: Hanami::CLI::Generators::App::ActionContext.new(inflector, app, slice, controller, action)) # rubocop:disable Layout/LineLength
          if slice
            fs.write(
              "spec/#{slice}/actions/#{controller_directory(controller)}/#{action}_spec.rb",
              t("action_slice_spec.erb", context)
            )
          else
            fs.write(
              "spec/actions/#{controller_directory(controller)}/#{action}_spec.rb",
              t("action_spec.erb", context)
            )
          end
        end

        private

        attr_reader :fs

        attr_reader :inflector

        # @api private
        # @param controller [Array<String>]
        def controller_directory(controller)
          fs.join(controller)
        end

        def template(path, context)
          require "erb"

          ERB.new(
            File.read(__dir__ + "/action/#{path}")
          ).result(context.ctx)
        end

        alias_method :t, :template
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hanami-rspec-3.11.0.beta3 lib/hanami/rspec/generators/action.rb