class NeverBounce::CLI::Script::Base

Barebones script base class. @abstract @see CLI::Feature::BasicInitialize @see CLI::Feature::RequireAttr

Attributes

argv[W]
env[W]
stderr[W]
stdout[W]

Public Class Methods

env_value_truthy?(s) click to toggle source

Return true if environment variable value is truthy.

# These are truthy.
DEBUG=1
DEBUG=true
DEBUG=y
DEBUG=yes
# 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

Public Instance Methods

argv() click to toggle source

Command-line arguments. Default is ARGV. @return [Array]

# File lib/never_bounce/cli/script/base.rb, line 20
def argv
  @argv ||= ARGV
end
env() click to toggle source

A copy of the environment for value-reading purposes. Default is ENV.to_h. @!attribute env @return [Hash]

# 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
env_falsey?(k) click to toggle source

@see env_truthy?

# File lib/never_bounce/cli/script/base.rb, line 62
def env_falsey?(k)
  !env_truthy?(k)
end
env_truthy?(k) click to toggle source

Return true if environment variable k is truthy.

env_truthy? "WITH_HTTP"   # => `true` or `false`
env_truthy? :WITH_HTTP    # same as above
# 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() click to toggle source

Main routine. @abstract @return [Integer]

# File lib/never_bounce/cli/script/base.rb, line 97
def main
  raise NotImplementedError, "Redefine `main` in your class: #{self.class}"
end
stderr() click to toggle source

Script's error stream. Default is STDERR. @return [IO]

# File lib/never_bounce/cli/script/base.rb, line 43
def stderr
  @stderr ||= STDERR
end
stdout() click to toggle source

Script's output stream. Default is STDOUT. @return [IO]

# File lib/never_bounce/cli/script/base.rb, line 49
def stdout
  @stdout ||= STDOUT
end
system(cmd, *args) click to toggle source

Run system command, print it if verbose. @return [mixed] Result of Kernel.system.

# 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?() click to toggle source

true if the script should be verbose. @return [true]

# File lib/never_bounce/cli/script/base.rb, line 55
def verbose?
  true
end