-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce support for
hanami generate part
(#18)
* Introduce support for `hanami generate part` * Cleanup * Ensure to generate specs for base part classes * Point back to `hanami/cli` `main` branch * Introduce `let(:value)` * `#ruby_implicity_keyword_argument?` -> `#ruby_omit_hash_values?`
- Loading branch information
Showing
9 changed files
with
491 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# frozen_string_literal: true | ||
|
||
require "erb" | ||
|
||
module Hanami | ||
module RSpec | ||
module Generators | ||
# @since 2.1.0 | ||
# @api private | ||
class Part | ||
# @since 2.1.0 | ||
# @api private | ||
def initialize(fs:, inflector:) | ||
@fs = fs | ||
@inflector = inflector | ||
end | ||
|
||
# @since 2.1.0 | ||
# @api private | ||
def call(app, slice, name, context: Hanami::CLI::Generators::App::PartContext.new(inflector, app, slice, name)) | ||
if slice | ||
generate_for_slice(slice, context) | ||
else | ||
generate_for_app(context) | ||
end | ||
end | ||
|
||
private | ||
|
||
# @since 2.1.0 | ||
# @api private | ||
def generate_for_slice(slice, context) | ||
generate_base_part_for_app(context) | ||
generate_base_part_for_slice(context, slice) | ||
|
||
fs.write( | ||
"spec/slices/#{slice}/views/parts/#{context.underscored_name}_spec.rb", | ||
t("part_slice_spec.erb", context) | ||
) | ||
end | ||
|
||
# @since 2.1.0 | ||
# @api private | ||
def generate_for_app(context) | ||
generate_base_part_for_app(context) | ||
|
||
fs.write( | ||
"spec/views/parts/#{context.underscored_name}_spec.rb", | ||
t("part_spec.erb", context) | ||
) | ||
end | ||
|
||
# @since 2.1.0 | ||
# @api private | ||
def generate_base_part_for_app(context) | ||
path = fs.join("spec", "views", "part_spec.rb") | ||
return if fs.exist?(path) | ||
|
||
fs.write( | ||
path, | ||
t("part_base_spec.erb", context) | ||
) | ||
end | ||
|
||
# @since 2.1.0 | ||
# @api private | ||
def generate_base_part_for_slice(context, slice) | ||
path = "spec/slices/#{slice}/views/part_spec.rb" | ||
return if fs.exist?(path) | ||
|
||
fs.write( | ||
path, | ||
t("part_slice_base_spec.erb", context) | ||
) | ||
end | ||
|
||
# @since 2.1.0 | ||
# @api private | ||
attr_reader :fs | ||
|
||
# @since 2.1.0 | ||
# @api private | ||
attr_reader :inflector | ||
|
||
# @since 2.1.0 | ||
# @api private | ||
def template(path, context) | ||
require "erb" | ||
|
||
ERB.new( | ||
File.read(__dir__ + "/part/#{path}"), | ||
trim_mode: "-" | ||
).result(context.ctx) | ||
end | ||
|
||
# @since 2.1.0 | ||
# @api private | ||
alias_method :t, :template | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe <%= camelized_app_name %>::Views::Part do | ||
<%- if ruby_omit_hash_values? -%> | ||
subject { described_class.new(value:) } | ||
<%- else -%> | ||
subject { described_class.new(value: value) } | ||
<%- end -%> | ||
let(:value) { double("value") } | ||
|
||
it "works" do | ||
expect(subject).to be_kind_of(described_class) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe <%= camelized_slice_name %>::Views::Part do | ||
<%- if ruby_omit_hash_values? -%> | ||
subject { described_class.new(value:) } | ||
<%- else -%> | ||
subject { described_class.new(value: value) } | ||
<%- end -%> | ||
let(:value) { double("value") } | ||
|
||
it "works" do | ||
expect(subject).to be_kind_of(described_class) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe <%= camelized_slice_name %>::Views::Parts::<%= camelized_name %> do | ||
<%- if ruby_omit_hash_values? -%> | ||
subject { described_class.new(value:) } | ||
<%- else -%> | ||
subject { described_class.new(value: value) } | ||
<%- end -%> | ||
let(:value) { double("<%= underscored_name %>") } | ||
|
||
it "works" do | ||
expect(subject).to be_kind_of(described_class) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe <%= camelized_app_name %>::Views::Parts::<%= camelized_name %> do | ||
<%- if ruby_omit_hash_values? -%> | ||
subject { described_class.new(value:) } | ||
<%- else -%> | ||
subject { described_class.new(value: value) } | ||
<%- end -%> | ||
let(:value) { double("<%= underscored_name %>") } | ||
|
||
it "works" do | ||
expect(subject).to be_kind_of(described_class) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.