Class: Validacity::Generators::InstallGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Validacity::Generators::InstallGenerator
- Includes:
- Rails::Generators::Actions, Thor::Actions
- Defined in:
- lib/generators/validacity/install_generator.rb
Class Method Summary collapse
-
.add_shebang_option! ⇒ Object
Small macro to add ruby as an option to the generator with proper default value plus an instance helper method called shebang.
-
.banner ⇒ Object
Use Rails default banner.
-
.base_name ⇒ Object
Sets the base_name taking into account the current class namespace.
-
.base_root ⇒ Object
Returns the base root for a common set of generators.
-
.class_option(name, options = {}) ⇒ Object
Make class option aware of Rails::Generators.options and Rails::Generators.aliases.
-
.default_aliases_for_option(name, options) ⇒ Object
Returns default aliases for the option name given doing a lookup in Rails::Generators.aliases.
-
.default_for_option(config, name, options, default) ⇒ Object
Returns default for the option name given doing a lookup in config.
-
.default_generator_root ⇒ Object
:doc:.
-
.default_source_root ⇒ Object
Returns the default source root for a given generator.
-
.default_value_for_option(name, options) ⇒ Object
Returns the default value for the option name given doing a lookup in Rails::Generators.options.
-
.desc(description = nil) ⇒ Object
Tries to get the description from a USAGE file one folder above the source root otherwise uses a default description.
-
.generator_name ⇒ Object
Removes the namespaces and get the generator name.
-
.hide! ⇒ Object
Convenience method to hide this generator from the available ones when running rails generator command.
-
.hook_for(*names, &block) ⇒ Object
Invoke a generator based on the value supplied by the user to the given option named “name”.
-
.hooks ⇒ Object
Keep hooks configuration that are used on prepare_for_invocation.
-
.inherited(base) ⇒ Object
Cache source root and add lib/generators/base/generator/templates to source paths.
-
.namespace(name = nil) ⇒ Object
Convenience method to get the namespace from the class name.
-
.prepare_for_invocation(name, value) ⇒ Object
Prepare class invocation to search on Rails namespace if a previous added hook is being used.
-
.remove_hook_for(*names) ⇒ Object
Remove a previously added hook.
-
.source_root(path = nil) ⇒ Object
Returns the source root for this generator using default_source_root as default.
-
.usage_path ⇒ Object
:doc:.
Instance Method Summary collapse
Class Method Details
.add_shebang_option! ⇒ Object
Small macro to add ruby as an option to the generator with proper default value plus an instance helper method called shebang.
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
# File 'lib/generators/validacity/install_generator.rb', line 399 def self.add_shebang_option! # :doc: class_option :ruby, type: :string, aliases: "-r", default: Thor::Util.ruby_command, desc: "Path to the Ruby binary of your choice", banner: "PATH" no_tasks { define_method :shebang do @shebang ||= begin command = if [:ruby] == Thor::Util.ruby_command "/usr/bin/env #{File.basename(Thor::Util.ruby_command)}" else [:ruby] end "#!#{command}" end end } end |
.banner ⇒ Object
Use Rails default banner.
328 329 330 |
# File 'lib/generators/validacity/install_generator.rb', line 328 def self. # :doc: "rails generate #{namespace.sub(/^rails:/, '')} #{arguments.map(&:usage).join(' ')} [options]".gsub(/\s+/, " ") end |
.base_name ⇒ Object
Sets the base_name taking into account the current class namespace.
333 334 335 336 337 338 339 |
# File 'lib/generators/validacity/install_generator.rb', line 333 def self.base_name # :doc: @base_name ||= begin if base = name.to_s.split("::").first base.underscore end end end |
.base_root ⇒ Object
Returns the base root for a common set of generators. This is used to dynamically guess the default source root.
236 237 238 |
# File 'lib/generators/validacity/install_generator.rb', line 236 def self.base_root __dir__ end |
.class_option(name, options = {}) ⇒ Object
Make class option aware of Rails::Generators.options and Rails::Generators.aliases.
217 218 219 220 221 222 |
# File 'lib/generators/validacity/install_generator.rb', line 217 def self.class_option(name, = {}) #:nodoc: [:desc] = "Indicates when to generate #{name.to_s.humanize.downcase}" unless .key?(:desc) [:aliases] = default_aliases_for_option(name, ) [:default] = default_value_for_option(name, ) super(name, ) end |
.default_aliases_for_option(name, options) ⇒ Object
Returns default aliases for the option name given doing a lookup in Rails::Generators.aliases.
360 361 362 |
# File 'lib/generators/validacity/install_generator.rb', line 360 def self.default_aliases_for_option(name, ) # :doc: default_for_option(Rails::Generators.aliases, name, , [:aliases]) end |
.default_for_option(config, name, options, default) ⇒ Object
Returns default for the option name given doing a lookup in config.
365 366 367 368 369 370 371 372 373 374 375 |
# File 'lib/generators/validacity/install_generator.rb', line 365 def self.default_for_option(config, name, , default) # :doc: if generator_name && (c = config[generator_name.to_sym]) && c.key?(name) c[name] elsif base_name && (c = config[base_name.to_sym]) && c.key?(name) c[name] elsif config[:rails].key?(name) config[:rails][name] else default end end |
.default_generator_root ⇒ Object
:doc:
425 426 427 428 |
# File 'lib/generators/validacity/install_generator.rb', line 425 def self.default_generator_root # :doc: path = File.(File.join(base_name, generator_name), base_root) path if File.exist?(path) end |
.default_source_root ⇒ Object
Returns the default source root for a given generator. This is used internally by rails to set its generators source root. If you want to customize your source root, you should use source_root.
227 228 229 230 231 232 |
# File 'lib/generators/validacity/install_generator.rb', line 227 def self.default_source_root return unless base_name && generator_name return unless default_generator_root path = File.join(default_generator_root, "templates") path if File.exist?(path) end |
.default_value_for_option(name, options) ⇒ Object
Returns the default value for the option name given doing a lookup in Rails::Generators.options.
354 355 356 |
# File 'lib/generators/validacity/install_generator.rb', line 354 def self.default_value_for_option(name, ) # :doc: default_for_option(Rails::Generators., name, , [:default]) end |
.desc(description = nil) ⇒ Object
Tries to get the description from a USAGE file one folder above the source root otherwise uses a default description.
49 50 51 52 53 54 55 56 57 |
# File 'lib/generators/validacity/install_generator.rb', line 49 def self.desc(description = nil) return super if description @desc ||= if usage_path ERB.new(File.read(usage_path)).result(binding) else "Description:\n Create #{base_name.humanize.downcase} files for #{generator_name} generator." end end |
.generator_name ⇒ Object
Removes the namespaces and get the generator name. For example, Rails::Generators::ModelGenerator will return “model” as generator name.
343 344 345 346 347 348 349 350 |
# File 'lib/generators/validacity/install_generator.rb', line 343 def self.generator_name # :doc: @generator_name ||= begin if generator = name.to_s.split("::").last generator.sub!(/Generator$/, "") generator.underscore end end end |
.hide! ⇒ Object
Convenience method to hide this generator from the available ones when running rails generator command.
69 70 71 |
# File 'lib/generators/validacity/install_generator.rb', line 69 def self.hide! Rails::Generators.hide_namespace(namespace) end |
.hook_for(*names, &block) ⇒ Object
Invoke a generator based on the value supplied by the user to the given option named “name”. A class option is created when this method is invoked and you can set a hash to customize it.
Examples
module Rails::Generators
class ControllerGenerator < Base
hook_for :test_framework, aliases: "-t"
end
end
The example above will create a test framework option and will invoke a generator based on the user supplied value.
For example, if the user invoke the controller generator as:
rails generate controller Account --test-framework=test_unit
The controller generator will then try to invoke the following generators:
"rails:test_unit", "test_unit:controller", "test_unit"
Notice that “rails:generators:test_unit” could be loaded as well, what Rails looks for is the first and last parts of the namespace. This is what allows any test framework to hook into Rails as long as it provides any of the hooks above.
Options
The first and last part used to find the generator to be invoked are guessed based on class invokes hook_for, as noticed in the example above. This can be customized with two options: :in and :as.
Let’s suppose you are creating a generator that needs to invoke the controller generator from test unit. Your first attempt is:
class AwesomeGenerator < Rails::Generators::Base
hook_for :test_framework
end
The lookup in this case for test_unit as input is:
"test_unit:awesome", "test_unit"
Which is not the desired lookup. You can change it by providing the :as option:
class AwesomeGenerator < Rails::Generators::Base
hook_for :test_framework, as: :controller
end
And now it will look up at:
"test_unit:controller", "test_unit"
Similarly, if you want it to also look up in the rails namespace, you just need to provide the :in value:
class AwesomeGenerator < Rails::Generators::Base
hook_for :test_framework, in: :rails, as: :controller
end
And the lookup is exactly the same as previously:
"rails:test_unit", "test_unit:controller", "test_unit"
Switches
All hooks come with switches for user interface. If you do not want to use any test framework, you can do:
rails generate controller Account --skip-test-framework
Or similarly:
rails generate controller Account --no-test-framework
Boolean hooks
In some cases, you may want to provide a boolean hook. For example, webrat developers might want to have webrat available on controller generator. This can be achieved as:
Rails::Generators::ControllerGenerator.hook_for :webrat, type: :boolean
Then, if you want webrat to be invoked, just supply:
rails generate controller Account --webrat
The hooks lookup is similar as above:
"rails:generators:webrat", "webrat:generators:controller", "webrat"
Custom invocations
You can also supply a block to hook_for to customize how the hook is going to be invoked. The block receives two arguments, an instance of the current class and the class to be invoked.
For example, in the resource generator, the controller should be invoked with a pluralized class name. But by default it is invoked with the same name as the resource generator, which is singular. To change this, we can give a block to customize how the controller can be invoked.
hook_for :resource_controller do |instance, controller|
instance.invoke controller, [ instance.name.pluralize ]
end
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/generators/validacity/install_generator.rb', line 182 def self.hook_for(*names, &block) = names. in_base = .delete(:in) || base_name as_hook = .delete(:as) || generator_name names.each do |name| unless .key?(name) defaults = if [:type] == :boolean {} elsif [true, false].include?(default_value_for_option(name, )) { banner: "" } else { desc: "#{name.to_s.humanize} to be invoked", banner: "NAME" } end class_option(name, defaults.merge!()) end hooks[name] = [ in_base, as_hook ] invoke_from_option(name, , &block) end end |
.hooks ⇒ Object
Keep hooks configuration that are used on prepare_for_invocation.
378 379 380 |
# File 'lib/generators/validacity/install_generator.rb', line 378 def self.hooks #:nodoc: @hooks ||= from_superclass(:hooks, {}) end |
.inherited(base) ⇒ Object
Cache source root and add lib/generators/base/generator/templates to source paths.
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/generators/validacity/install_generator.rb', line 242 def self.inherited(base) #:nodoc: super # Invoke source_root so the default_source_root is set. base.source_root if base.name && base.name !~ /Base$/ Rails::Generators.subclasses << base Rails::Generators.templates_path.each do |path| if base.name.include?("::") base.source_paths << File.join(path, base.base_name, base.generator_name) else base.source_paths << File.join(path, base.generator_name) end end end end |
.namespace(name = nil) ⇒ Object
Convenience method to get the namespace from the class name. It’s the same as Thor default except that the Generator at the end of the class is removed.
62 63 64 65 |
# File 'lib/generators/validacity/install_generator.rb', line 62 def self.namespace(name = nil) return super if name @namespace ||= super.sub(/_generator$/, "").sub(/:generators:/, ":") end |
.prepare_for_invocation(name, value) ⇒ Object
Prepare class invocation to search on Rails namespace if a previous added hook is being used.
384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/generators/validacity/install_generator.rb', line 384 def self.prepare_for_invocation(name, value) #:nodoc: return super unless value.is_a?(String) || value.is_a?(Symbol) if value && constants = hooks[name] value = name if TrueClass === value Rails::Generators.find_by_namespace(value, *constants) elsif klass = Rails::Generators.find_by_namespace(value) klass else super end end |
.remove_hook_for(*names) ⇒ Object
Remove a previously added hook.
remove_hook_for :orm
208 209 210 211 212 213 214 |
# File 'lib/generators/validacity/install_generator.rb', line 208 def self.remove_hook_for(*names) remove_invocation(*names) names.each do |name| hooks.delete(name) end end |
.source_root(path = nil) ⇒ Object
Returns the source root for this generator using default_source_root as default.
42 43 44 45 |
# File 'lib/generators/validacity/install_generator.rb', line 42 def self.source_root(path = nil) @_source_root = path if path @_source_root ||= default_source_root end |
.usage_path ⇒ Object
:doc:
417 418 419 420 421 422 423 |
# File 'lib/generators/validacity/install_generator.rb', line 417 def self.usage_path # :doc: paths = [ source_root && File.("../USAGE", source_root), default_generator_root && File.join(default_generator_root, "USAGE") ] paths.compact.detect { |path| File.exist? path } end |
Instance Method Details
#copy_application_policy ⇒ Object
26 27 28 29 30 |
# File 'lib/generators/validacity/install_generator.rb', line 26 def copy_application_policy validator_file = File.join("app/validators", class_path, "application_validator.rb") template "application_validator.rb", validator_file end |
#create_initializer_file ⇒ Object
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/generators/validacity/install_generator.rb', line 15 def create_initializer_file content = <<-'RUBY'.gsub(/^\ {10}/, "") require_dependency "validacity" Validacity.configure do |config| config.search_paths "#{__dir__}/../app/validators/**/*_validator.rb" end RUBY create_file "config/initializers/validacity_initializer.rb", content end |