Skip to content

Commit

Permalink
pass options to initialize instead of call
Browse files Browse the repository at this point in the history
  • Loading branch information
sammyhenningsson committed Dec 25, 2018
1 parent 854e107 commit 45abdee
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/shaf/command/generate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def self.options(parser, options)

def call
in_project_root do
Generator::Factory.create(*args).call(options)
Generator::Factory.create(*args, **options).call
end
rescue StandardError => e
raise Command::ArgumentError, e.message
Expand Down
9 changes: 5 additions & 4 deletions lib/shaf/generator/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module Shaf
module Generator
class Base
attr_reader :args
attr_reader :args, :options

class << self
def inherited(child)
Expand All @@ -23,11 +23,12 @@ def usage(str = nil, &block)
def options(option_parser, options); end
end

def initialize(*args)
@args = args.dup
def initialize(*args, **options)
@args = args
@options = options
end

def call(options = {}); end
def call; end

def template_dir
File.expand_path('../templates', __FILE__)
Expand Down
2 changes: 1 addition & 1 deletion lib/shaf/generator/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Controller < Base
identifier :controller
usage 'generate controller RESOURCE_NAME [attribute:type] [..]'

def call(options = {})
def call
create_controller
create_integration_spec if options[:specs]
add_link_to_root
Expand Down
2 changes: 1 addition & 1 deletion lib/shaf/generator/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Generator < Shaf::Generator::Base
usage { Factory.usage }

def call
generator = args.empty? ? Empty.new : Factory.create(*args)
generator = args.empty? ? Empty.new(**options) : Factory.create(*args, **options)
(target, content) = generator.call
write_output(target, content)
rescue StandardError => e
Expand Down
7 changes: 4 additions & 3 deletions lib/shaf/generator/migration/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Base
}
]

attr_reader :args
attr_reader :args, :options

class << self
def inherited(child)
Expand All @@ -48,8 +48,9 @@ def usage(str = nil, &block)
end
end

def initialize(*args)
@args = args.dup
def initialize(*args, **options)
@args = args
@options = options
end

def call
Expand Down
8 changes: 4 additions & 4 deletions lib/shaf/generator/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ class Model < Base
identifier :model
usage 'generate model MODEL_NAME [attribute:type] [..]'

def call(options = {})
def call
create_model
create_migration
create_serializer(options)
create_serializer
end

def model_name
Expand Down Expand Up @@ -61,10 +61,10 @@ def create_migration
Migration::Generator.new(*migration_args).call
end

def create_serializer(options)
def create_serializer
serializer_args = %W(serializer #{model_name})
serializer_args += args[1..-1].map { |arg| arg.split(':').first }
Generator::Factory.create(*serializer_args).call(options)
Generator::Factory.create(*serializer_args, **options).call
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/shaf/generator/policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Policy < Base
identifier :policy
usage 'generate policy MODEL_NAME [attribute] [..]'

def call(options = {})
def call
create_policy
end

Expand Down
6 changes: 3 additions & 3 deletions lib/shaf/generator/scaffold.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ class Scaffold < Base
identifier :scaffold
usage 'generate scaffold RESOURCE_NAME [attribute:type] [..]'

def call(options = {})
def call
if name.empty?
raise "Please provide a resource name when using the scaffold generator!"
end

options[:specs] = true if options[:specs].nil?
Generator::Factory.create('model', *args).call(options)
Generator::Factory.create('controller', *controller_args).call(options)
Generator::Factory.create('model', *args, **options).call
Generator::Factory.create('controller', *controller_args, **options).call
end

def name
Expand Down
8 changes: 4 additions & 4 deletions lib/shaf/generator/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ class Serializer < Base
identifier :serializer
usage 'generate serializer MODEL_NAME [attribute] [..]'

def call(options = {})
def call
create_serializer
create_serializer_spec if options[:specs]
create_policy(options)
create_policy
end

def name
Expand Down Expand Up @@ -211,9 +211,9 @@ def opts
}
end

def create_policy(options)
def create_policy
policy_args = ["policy", name, *args[1..-1]]
Generator::Factory.create(*policy_args).call(options)
Generator::Factory.create(*policy_args, **options).call
end
end
end
Expand Down

0 comments on commit 45abdee

Please sign in to comment.