Class: NeverBounce::CLI::Script::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/never_bounce/cli/script/base.rb

Overview

This class is abstract.

Barebones script base class.

Direct Known Subclasses

Meaningful

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argvArray

Command-line arguments. Default is ARGV.

Returns:

  • (Array)


20
21
22
# File 'lib/never_bounce/cli/script/base.rb', line 20

def argv
  @argv ||= ARGV
end

#envHash

A copy of the environment for value-reading purposes. Default is ENV.to_h.

Returns:

  • (Hash)


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

#stderrIO

Script’s error stream. Default is STDERR.

Returns:

  • (IO)


43
44
45
# File 'lib/never_bounce/cli/script/base.rb', line 43

def stderr
  @stderr ||= STDERR
end

#stdoutIO

Script’s output stream. Default is STDOUT.

Returns:

  • (IO)


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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)

See Also:



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

Returns:

  • (Boolean)


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

#mainInteger

This method is abstract.

Main routine.

Returns:

  • (Integer)

Raises:

  • (NotImplementedError)


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.

Returns:

  • (mixed)

    Result of Kernel.system.



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.

Returns:

  • (true)


55
56
57
# File 'lib/never_bounce/cli/script/base.rb', line 55

def verbose?
  true
end