Class: EverydayThorUtil::CommonHelpers

Inherits:
Object
  • Object
show all
Defined in:
lib/everyday_thor_util/common.rb

Class Method Summary collapse

Class Method Details

.call_original_method(args, base, block, method_name, original_method) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/everyday_thor_util/common.rb', line 49

def call_original_method(args, base, block, method_name, original_method)
  begin
    original_method.bind(base).call(*args, &block)
  rescue ArgumentError => e
    base.class.handle_argument_error(base.class.commands[method_name], e, args, original_method.arity)
  end
end

.debug_and_call_original(args, base, env_sym, method_name, option_sym, original_method, &block) ⇒ Object



66
67
68
69
# File 'lib/everyday_thor_util/common.rb', line 66

def debug_and_call_original(args, base, env_sym, method_name, option_sym, original_method, &block)
  print_debug_if_should(option_sym, env_sym, base, original_method, method_name, args)
  call_original_method(args, base, block, method_name, original_method)
end

.define_debug_wrapper(base, env_sym, method_name, option_sym) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/everyday_thor_util/common.rb', line 57

def define_debug_wrapper(base, env_sym, method_name, option_sym)
  base.class_eval {
    original_method = instance_method(method_name)
    define_non_command(method_name) { |*args, &block|
      EverydayThorUtil::CommonHelpers.debug_and_call_original(args, self, env_sym, method_name, option_sym, original_method, &block)
    }
  }
end

.env_val_true(d) ⇒ Object



41
42
43
# File 'lib/everyday_thor_util/common.rb', line 41

def env_val_true(d)
  d == '1' || d == 1 || d == 'true' || d == 't'
end


26
27
28
29
# File 'lib/everyday_thor_util/common.rb', line 26

def print_all_debug(args, method, obj, original_method)
  print_base_debug(method, obj)
  original_method.parameters.each_with_index { |p, i| puts "#{p[1].to_s}: #{args[i]}" }
end


31
32
33
34
35
# File 'lib/everyday_thor_util/common.rb', line 31

def print_base_debug(method, obj)
  puts "command: #{obj.class.basename2} #{method.gsub(/_/, '-').to_s}"
  puts "parent_options: #{obj.parent_options.inspect}"
  puts "options: #{obj.options.inspect}"
end


22
23
24
# File 'lib/everyday_thor_util/common.rb', line 22

def print_debug_if_should(option_sym, env_sym, obj, original_method, method, args)
  print_all_debug(args, method, obj, original_method) if should_debug?(env_sym, obj, option_sym)
end

.should_debug?(env_sym, obj, option_sym) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/everyday_thor_util/common.rb', line 37

def should_debug?(env_sym, obj, option_sym)
  should_use_option_sym?(obj, option_sym) ? obj.options[option_sym.to_sym] : (env_sym && env_val_true(ENV[env_sym.to_s]))
end

.should_use_option_sym?(obj, option_sym) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/everyday_thor_util/common.rb', line 45

def should_use_option_sym?(obj, option_sym)
  option_sym && (obj.options.has_key?(option_sym.to_s) || obj.options.has_key?(option_sym.to_sym))
end