Class: Dpl::Env::Env

Inherits:
Object
  • Object
show all
Includes:
Memoize
Defined in:
lib/dpl/helper/env.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Memoize

included

Constructor Details

#initialize(env, args) ⇒ Env

Returns a new instance of Env.



17
18
19
20
21
# File 'lib/dpl/helper/env.rb', line 17

def initialize(env, args)
  @env = env
  @opts = args.last.is_a?(Hash) ? args.pop : {}
  @strs = args.map(&:to_s).map(&:upcase)
end

Instance Attribute Details

#cmdObject (readonly)

opts allows unconventional ENV vars such as GOOGLECLOUDKEYFILE



15
16
17
# File 'lib/dpl/helper/env.rb', line 15

def cmd
  @cmd
end

#env(cmd) ⇒ Object (readonly)

opts allows unconventional ENV vars such as GOOGLECLOUDKEYFILE



15
16
17
# File 'lib/dpl/helper/env.rb', line 15

def env
  @env
end

#optsObject (readonly)

opts allows unconventional ENV vars such as GOOGLECLOUDKEYFILE



15
16
17
# File 'lib/dpl/helper/env.rb', line 15

def opts
  @opts
end

#strsObject (readonly)

opts allows unconventional ENV vars such as GOOGLECLOUDKEYFILE



15
16
17
# File 'lib/dpl/helper/env.rb', line 15

def strs
  @strs
end

Instance Method Details

#description(cmd) ⇒ Object



30
31
32
33
34
35
# File 'lib/dpl/helper/env.rb', line 30

def description(cmd)
  strs = self.strs.map { |str| "#{str}_" }
  strs += self.strs if opts[:allow_skip_underscore]
  strs = strs.size > 1 ? "[#{strs.sort.join('|')}]" : strs.join
  "Options can be given via env vars if prefixed with `#{strs}`. #{example(cmd)}"
end

#example(cmd) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/dpl/helper/env.rb', line 37

def example(cmd)
  return unless opt = cmd.opts.detect { |option| option.secret? }

  env = strs.map { |str| "`#{str}_#{opt.name.upcase}=<#{opt.name}>`" }
  env += strs.map { |str| "`#{str}#{opt.name.upcase}=<#{opt.name}>`" } if opts[:allow_skip_underscore]
  "E.g. the option `--#{opt.name}` can be given as #{sentence(env)}."
end

#sentence(strs) ⇒ Object



45
46
47
48
49
# File 'lib/dpl/helper/env.rb', line 45

def sentence(strs)
  return strs.join if strs.size == 1

  [strs[0..-2].join(', '), strs[-1]].join(' or ')
end