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.



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

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



13
14
15
# File 'lib/dpl/helper/env.rb', line 13

def cmd
  @cmd
end

#env(cmd) ⇒ Object (readonly)

opts allows unconventional ENV vars such as GOOGLECLOUDKEYFILE



13
14
15
# File 'lib/dpl/helper/env.rb', line 13

def env
  @env
end

#optsObject (readonly)

opts allows unconventional ENV vars such as GOOGLECLOUDKEYFILE



13
14
15
# File 'lib/dpl/helper/env.rb', line 13

def opts
  @opts
end

#strsObject (readonly)

opts allows unconventional ENV vars such as GOOGLECLOUDKEYFILE



13
14
15
# File 'lib/dpl/helper/env.rb', line 13

def strs
  @strs
end

Instance Method Details

#description(cmd) ⇒ Object



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

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



35
36
37
38
39
40
# File 'lib/dpl/helper/env.rb', line 35

def example(cmd)
  return unless opt = cmd.opts.detect { |opt| opt.secret? }
  env = self.strs.map { |str| "`#{str}_#{opt.name.upcase}=<#{opt.name}>`" }
  env += self.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



42
43
44
45
# File 'lib/dpl/helper/env.rb', line 42

def sentence(strs)
  return strs.join if strs.size == 1
  [strs[0..-2].join(', '), strs[-1]].join(' or ')
end