Class: Charyf::Generators::Base

Inherits:
Thor::Group
  • Object
show all
Includes:
Actions, Thor::Actions
Defined in:
lib/charyf/utils/generator/base.rb

Direct Known Subclasses

AppBase, NamedBase

Class Method Summary collapse

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, options = {}) #:nodoc:
  options[:desc]    = "Indicates when to generate #{name.to_s.downcase}" unless options.key?(:desc)
  options[:default] = default_value_for_option(name, options)
  super(name, options)
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, options = {})
  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)
  options = names.last.is_a?(Hash) ? names.pop : {}
  in_base = options.delete(:in) || base_name
  as_hook = options.delete(:as) || generator_name

  names.each do |name|
    unless class_options.key?(name)
      defaults = if options[:type] == :boolean
                   {}
                 elsif [true, false].include?(default_value_for_option(name, options))
                   { banner: "", type: :boolean }
                 elsif default_value_for_option(name, options).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!(options))
    end

    hooks[name] = [ in_base, as_hook ]
    invoke_from_option(name, options, &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