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(<<~CODE, __FILE__, __LINE__ + 1)
    # module MyModule
    #   MOD_REF = MyModule
    # end

    MOD_REF = #{mod.name}
  CODE
end

.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(<<~CODE, __FILE__, __LINE__ + 1)
    # 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

    instance_variable_set(:@#{name}, #{default_value})

    def self.#{name}
      instance_variable_get(:@#{name})
    end

    def self.#{name}=(value)
      instance_variable_set(:@#{name}, value)
    end
  CODE
end

.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(<<~CODE, __FILE__, __LINE__ + 1)
    # module MyModule
    #   instance_variable_set(:@my_variable, 1)
    #   def self.my_variable
    #     instance_variable_get(:@my_variable)
    #   end
    # end

    instance_variable_set(:@#{name}, #{value})

    def self.#{name}
      instance_variable_get(:@#{name})
    end
  CODE
end

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