Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added alt_types to configuration #5

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@ Metrics/CyclomaticComplexity:

Metrics/MethodLength:
Enabled: false

Metrics/PerceivedComplexity:
Enabled: false
7 changes: 6 additions & 1 deletion lib/wizard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ def self.generate(model, **args)
concepts -= args[:except] if args.key? :except
concepts = args[:only] if args.key? :only

output = []

actions.each do |action|
concepts.each do |concept|
fetch_generator(concept.to_s).generate(model, action.to_s, args[:context]&.to_s)
file = fetch_generator(concept.to_s).generate(model, action.to_s, args[:context]&.to_s)
output << file if file.length.positive?
end
end

output
end
end
7 changes: 5 additions & 2 deletions lib/wizard/concept_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ def initialize(**args)\
@type = args[:type]
end

def type_dirname = Wizard.configuration.pluralize ? ActiveSupport::Inflector.pluralize(type) : type
def true_type = (Wizard.configuration.alt_types[type.to_sym] || type).to_s

def type_dirname = Wizard.configuration.pluralize ? ActiveSupport::Inflector.pluralize(true_type) : true_type

def generate(model, name, context = nil)
materials = [model, type_dirname, name]
Expand Down Expand Up @@ -38,7 +40,8 @@ def copy(model, name, context)
content["_MODEL_"] = model.camelize
content["::_CONTEXT_"] = context.nil? ? "" : "::#{context.camelize}"
content["_NAME_"] = name.camelize
content["_CONCEPT_"] = type_dirname.camelize while content.include?("_CONCEPT_")
content["_CONCEPT_"] = type_dirname.camelize
content["_CONCEPT_"] = true_type.camelize

content
end
Expand Down
3 changes: 2 additions & 1 deletion lib/wizard/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

module Wizard
class Configuration
attr_accessor :base_directory, :pluralize
attr_accessor :base_directory, :pluralize, :alt_types

def initialize
@base_directory = "app/concepts"
@pluralize = false
@alt_types = {}
end
end
end
14 changes: 14 additions & 0 deletions test/test_wizard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def test_that_it_requires_actions_arg

def test_that_it_generates_files
Wizard.configure do |config|
config.alt_types = {}
config.pluralize = false
end

Expand Down Expand Up @@ -44,4 +45,17 @@ def test_that_it_pluralize_concept_directories

assert File.exist?("test/tmp/concepts/category/operations/create.rb")
end

def test_that_it_uses_alt_types
Wizard.configure do |config|
config.alt_types = {
view: :representable
}
config.pluralize = false
end

Wizard.generate("Application", actions: %w[index], only: %w[view])

assert File.exist?("test/tmp/concepts/application/representable/index.rb")
end
end
2 changes: 2 additions & 0 deletions test/wizard/test_concept_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ def test_that_it_initializes_generator
end

def test_that_it_generates_concept
Wizard.configuration.pluralize = false

generator = Wizard::ConceptGenerator.new type: Wizard::ConceptType::OPERATION

assert_equal "test/tmp/concepts/user/admin/operation/create.rb", generator.generate("User", "create", "Admin")
Expand Down