Class: Charyf::Generators::Base
- Includes:
- Actions, Thor::Actions
- Defined in:
- lib/charyf/utils/generator/base.rb
Class Method Summary collapse
-
.class_option(name, options = {}) ⇒ Object
# Remove a previously added hook.
-
.desc(usage = nil, description = nil, options = {}) ⇒ Object
Tries to get the description from a USAGE file one folder above the command root.
- .desc_file(desc_file = nil) ⇒ Object
-
.hide! ⇒ Object
Convenience method to hide this generator from the available ones when running charyf generator command.
- .hook_for(*names, &block) ⇒ Object
-
.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.
-
.source_root(path = nil) ⇒ Object
Returns the source root for this generator using default_source_root as default.
Methods included from Actions
#add_source, #after_bundle, #environment, #gem, #gem_group, #git, #initialize
Class Method Details
.class_option(name, options = {}) ⇒ Object
# Remove a previously added hook. # # remove_hook_for :orm def self.remove_hook_for(*names)
remove_invocation(*names)
names.each do |name|
hooks.delete(name)
end
end
Make class option aware of Charyf::Generators.options
93 94 95 96 97 |
# File 'lib/charyf/utils/generator/base.rb', line 93 def self.class_option(name, = {}) #:nodoc: [:desc] = "Indicates when to generate #{name.to_s.downcase}" unless .key?(:desc) [:default] = default_value_for_option(name, ) super(name, ) end |
.desc(usage = nil, description = nil, options = {}) ⇒ Object
Tries to get the description from a USAGE file one folder above the command root.
42 43 44 45 46 47 48 |
# File 'lib/charyf/utils/generator/base.rb', line 42 def self.desc(usage = nil, description = nil, = {}) if usage super else @desc ||= ERB.new(File.read(desc_file)).result(binding) if desc_file end end |
.desc_file(desc_file = nil) ⇒ Object
50 51 52 53 54 |
# File 'lib/charyf/utils/generator/base.rb', line 50 def self.desc_file(desc_file = nil) @desc_file = desc_file if desc_file @desc_file if @desc_file && File.exist?(@desc_file) end |
.hide! ⇒ Object
Convenience method to hide this generator from the available ones when running charyf generator command.
115 116 117 |
# File 'lib/charyf/utils/generator/base.rb', line 115 def self.hide! Charyf::Generators.hide_namespace(namespace) end |
.hook_for(*names, &block) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/charyf/utils/generator/base.rb', line 56 def self.hook_for(*names, &block) = names.last.is_a?(Hash) ? names.pop : {} 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: "", type: :boolean } elsif default_value_for_option(name, ).is_a? Array { desc: "#{name.to_s} to be invoked", banner: "NAMES", type: :array } else { desc: "#{name.to_s} 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 |
.inherited(base) ⇒ Object
Cache source root and add lib/generators/base/generator/templates to source paths.
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/charyf/utils/generator/base.rb', line 28 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$/ Charyf::Generators.subclasses << base 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.
108 109 110 111 |
# File 'lib/charyf/utils/generator/base.rb', line 108 def self.namespace(name = nil) return super if name @namespace ||= super.sub(/_generator$/, "").sub(/:generators:/, ":") end |
.source_root(path = nil) ⇒ Object
Returns the source root for this generator using default_source_root as default.
100 101 102 103 |
# File 'lib/charyf/utils/generator/base.rb', line 100 def self.source_root(path = nil) @_source_root = path if path @_source_root ||= default_source_root || super end |