Module: Common

Defined in:
lib/get/commons/common.rb

Overview

Utility module

Class Method Summary collapse

Class Method Details

.add_module_self_reference(mod) ⇒ Object

Add a new ‘MOD_REF’ constant with the module symbol for a shorter variable name.



115
116
117
118
119
120
121
122
123
# File 'lib/get/commons/common.rb', line 115

def self.add_module_self_reference(mod)
  mod.module_eval("    # module MyModule\n    #   MOD_REF = MyModule\n    # end\n\n    MOD_REF = \#{mod.name}\n  CODE\nend\n", __FILE__, __LINE__ + 1)

.error(message, &block) ⇒ Object

Print an error message and optionally run a block. Stdout becomes stderr, so every print is performed to stderr. This behavior is wanted as this method is called on errors.



27
28
29
# File 'lib/get/commons/common.rb', line 27

def self.error(message, &block)
  Common.print_then_do_and_exit("Error: #{message}", 1, block)
end

.module_instance_attr(mod, name, default_value = nil) ⇒ Object

Add an instance attribute (with a default value) to a module. It is intended to be called in the body of a module definition:

module MyModule
   DEFAULT_VALUE = 1
   Common.module_instance_attr(self, my_variable, DEFAULT_VALUE)
end

produces the code:

module MyModule
   instance_variable_set(:@my_variable, 1)
   def self.my_variable
     instance_variable_get(:@my_variable)
   end

   def self.my_variable=(value)
     instance_variable_set(:@my_variable, value)
   end
end


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/get/commons/common.rb', line 72

def self.module_instance_attr(mod, name, default_value = nil)
  mod.module_eval("    # module MyModule\n    #       instance_variable_set(:@my_variable, 1)\n    #       def self.my_variable\n    #         instance_variable_get(:@my_variable)\n    #       end\n    #\n    #       def self.my_variable=(value)\n    #         instance_variable_set(:@my_variable, value)\n    #       end\n    #    end\n\n    instance_variable_set(:@\#{name}, \#{default_value})\n\n    def self.\#{name}\n      instance_variable_get(:@\#{name})\n    end\n\n    def self.\#{name}=(value)\n      instance_variable_set(:@\#{name}, value)\n    end\n  CODE\nend\n", __FILE__, __LINE__ + 1)

.module_instance_value(mod, name, value) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/get/commons/common.rb', line 97

def self.module_instance_value(mod, name, value)
  mod.module_eval("    # module MyModule\n    #   instance_variable_set(:@my_variable, 1)\n    #   def self.my_variable\n    #     instance_variable_get(:@my_variable)\n    #   end\n    # end\n\n    instance_variable_set(:@\#{name}, \#{value})\n\n    def self.\#{name}\n      instance_variable_get(:@\#{name})\n    end\n  CODE\nend\n", __FILE__, __LINE__ + 1)

Print the given message, execute a block if given, and exit the program with the given exit status. If exit_status is not 0, the stdout is redirected to stderr.



47
48
49
50
51
52
53
# File 'lib/get/commons/common.rb', line 47

def self.print_then_do_and_exit(message, exit_code = 0, action = proc {})
  $stdout = $stderr unless exit_code.zero?

  puts message
  action.call if action.respond_to?('call')
  exit(exit_code)
end

.with_subcommand_exception_handling(parser) ⇒ Object

Subcommand exception handling for Optimist. Generally subcommands do not have a version to print.



33
34
35
36
37
38
39
40
41
42
# File 'lib/get/commons/common.rb', line 33

def self.with_subcommand_exception_handling(parser)
  yield
rescue Optimist::CommandlineError => e
  parser.die(e.message, nil, e.error_code)
rescue Optimist::HelpNeeded
  parser.educate
  exit
rescue Optimist::VersionNeeded
  # Version is not needed in this command
end