Class: NeverBounce::CLI::Script::Base Abstract
- Inherits:
-
Object
- Object
- NeverBounce::CLI::Script::Base
- Defined in:
- lib/never_bounce/cli/script/base.rb
Overview
This class is abstract.
Barebones script base class.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#argv ⇒ Array
Command-line arguments.
-
#env ⇒ Hash
A copy of the environment for value-reading purposes.
-
#stderr ⇒ IO
Script’s error stream.
-
#stdout ⇒ IO
Script’s output stream.
Class Method Summary collapse
-
.env_value_truthy?(s) ⇒ Boolean
Return
true
if environment variable value is truthy.
Instance Method Summary collapse
- #env_falsey?(k) ⇒ Boolean
-
#env_truthy?(k) ⇒ Boolean
Return
true
if environment variablek
is truthy. -
#main ⇒ Integer
abstract
Main routine.
-
#system(cmd, *args) ⇒ mixed
Run system command, print it if verbose.
-
#verbose? ⇒ true
true
if the script should be verbose.
Instance Attribute Details
#argv ⇒ Array
Command-line arguments. Default is ARGV
.
20 21 22 |
# File 'lib/never_bounce/cli/script/base.rb', line 20 def argv @argv ||= ARGV end |
#env ⇒ Hash
A copy of the environment for value-reading purposes. Default is ENV.to_h
.
27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/never_bounce/cli/script/base.rb', line 27 def env # Ruby's `ENV` is a weird thing. # It's a direct `Object` which acts like `Hash`. # It can't be reliably dup'd cloned, at the same time writes to it are invocation-global. # This implicit read/write nature of `ENV` is a major hassle in tests, since it creates unnecessary side effects we have to tackle with specifically. # Solution for now: # # 1. Since 99% of the time our script isn't interested in *writing* to ENV, this method deals with the READ case as the most widely used one. # 2. If we ever need to write to ENV in order to *create* the environment for a child process or something, we'll find a way to do it with grace. # # Everything above is a comment to `.to_h`, mysteriously present on the next line. @env ||= ENV.to_h end |
#stderr ⇒ IO
Script’s error stream. Default is STDERR
.
43 44 45 |
# File 'lib/never_bounce/cli/script/base.rb', line 43 def stderr @stderr ||= STDERR end |
#stdout ⇒ IO
Script’s output stream. Default is STDOUT
.
49 50 51 |
# File 'lib/never_bounce/cli/script/base.rb', line 49 def stdout @stdout ||= STDOUT end |
Class Method Details
.env_value_truthy?(s) ⇒ Boolean
Return true
if environment variable value is truthy.
# These are truthy.
DEBUG=1
DEBUG=true
DEBUG=y
DEBUG=yes
81 82 83 |
# File 'lib/never_bounce/cli/script/base.rb', line 81 def self.env_value_truthy?(s) ["1", "true", "y", "yes"].include? s.to_s.downcase end |
Instance Method Details
#env_falsey?(k) ⇒ Boolean
62 63 64 |
# File 'lib/never_bounce/cli/script/base.rb', line 62 def env_falsey?(k) !env_truthy?(k) end |
#env_truthy?(k) ⇒ Boolean
Return true
if environment variable k
is truthy.
env_truthy? "WITH_HTTP" # => `true` or `false`
env_truthy? :WITH_HTTP # same as above
70 71 72 |
# File 'lib/never_bounce/cli/script/base.rb', line 70 def env_truthy?(k) self.class.env_value_truthy?(env[k.to_s]) end |
#main ⇒ Integer
This method is abstract.
Main routine.
97 98 99 |
# File 'lib/never_bounce/cli/script/base.rb', line 97 def main raise NotImplementedError, "Redefine `main` in your class: #{self.class}" end |
#system(cmd, *args) ⇒ mixed
Run system command, print it if verbose.
87 88 89 90 |
# File 'lib/never_bounce/cli/script/base.rb', line 87 def system(cmd, *args) puts "### #{cmd} #{args.map(&:shellescape).join(' ')}" if verbose? Kernel.system(cmd, *args) end |
#verbose? ⇒ true
true
if the script should be verbose.
55 56 57 |
# File 'lib/never_bounce/cli/script/base.rb', line 55 def verbose? true end |