Method: GitReflow::Workflow::ClassMethods#command
- Defined in:
- lib/git_reflow/workflow.rb
#command(name, **params) {|a:, b:, c:, ...| ... } ⇒ Object
Creates a singleton method on the included class
This method will take any number of keyword parameters. If @defaults keyword is provided, and the given key(s) in the defaults are not provided as keyword parameters, then it will use the value given in the defaults for that parameter.
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/git_reflow/workflow.rb', line 150 def command(name, **params, &block) params[:flags] ||= {} params[:switches] ||= {} params[:arguments] ||= {} defaults ||= params[:arguments].merge(params[:flags]).merge(params[:switches]) # Ensure flags and switches use kebab-case kebab_case_keys!(params[:flags]) kebab_case_keys!(params[:switches]) # Register the command with the workflow so that we can properly handle # option parsing from the command line self.commands[name] = params self.command_docs[name] = params logger.debug "adding new command '#{name}' with #{defaults.inspect}" self.define_singleton_method(name) do |args = {}| args_with_defaults = {} args.each do |name, value| if "#{value}".length <= 0 && !defaults[name].nil? args_with_defaults[name] = defaults[name] else args_with_defaults[name] = value end end defaults.each do |name, value| if "#{args_with_defaults[name]}".length <= 0 args_with_defaults[name] = value end end logger.debug "callbacks: #{callbacks.inspect}" Array(callbacks[:before][name]).each do |block| logger.debug "(before) callback running for `#{name}` command..." argument_overrides = block.call(**args_with_defaults) || {} args_with_defaults.merge!(argument_overrides) if argument_overrides.is_a?(Hash) end logger.info "Running command `#{name}` with args: #{args_with_defaults.inspect}..." block.call(**args_with_defaults) Array(callbacks[:after][name]).each do |block| logger.debug "(after) callback running for `#{name}` command..." block.call(**args_with_defaults) end end end |