Class: Dpl::Interpolate::Interpolator

Inherits:
Struct
  • Object
show all
Includes:
Dpl::Interpolate
Defined in:
lib/dpl/helper/interpolate.rb

Constant Summary collapse

MODIFIER =
%i(obfuscate escape quote)
PATTERN =
/%\{(\$?[\w]+)\}/
ENV_VAR =
/^\$[A-Z_]+$/
UPCASE =
/^[A-Z_]+$/
UNKNOWN =
'[unknown variable: %s]'

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#argsObject

Returns the value of attribute args

Returns:

  • (Object)

    the current value of args



75
76
77
# File 'lib/dpl/helper/interpolate.rb', line 75

def args
  @args
end

#objObject

Returns the value of attribute obj

Returns:

  • (Object)

    the current value of obj



75
76
77
# File 'lib/dpl/helper/interpolate.rb', line 75

def obj
  @obj
end

#optsObject

Returns the value of attribute opts

Returns:

  • (Object)

    the current value of opts



75
76
77
# File 'lib/dpl/helper/interpolate.rb', line 75

def opts
  @opts
end

#strObject

Returns the value of attribute str

Returns:

  • (Object)

    the current value of str



75
76
77
# File 'lib/dpl/helper/interpolate.rb', line 75

def str
  @str
end

Instance Method Details

#applyObject



84
85
86
87
88
89
# File 'lib/dpl/helper/interpolate.rb', line 84

def apply
  str = interpolate(self.str.to_s)
  str = obfuscate(str) unless opts[:secure]
  str = str.gsub('  ', ' ') if str.lines.size == 1
  str
end

#interpolate(str) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/dpl/helper/interpolate.rb', line 91

def interpolate(str)
  str = str % args if args.is_a?(Array) && args.any?
  @blacklist_result = false
  str = str.to_s.gsub(PATTERN) do
    @blacklist_result = true
    normalize(lookup($1.to_sym))
  end
  @blacklist_result || (args.is_a?(Array) && args.any? { |arg| arg.is_a?(String) && arg.blacklisted? }) ? str.blacklist : str
end

#lookup(key) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/dpl/helper/interpolate.rb', line 120

def lookup(key)        
  if vars? && !var?(key)
    UNKNOWN % key
  elsif mod = modifier(key)
    key = key.to_s.sub("#{mod}d_", '')
    obj.send(mod, lookup(key))
  elsif key.to_s =~ ENV_VAR
    ENV[key.to_s.sub('$', '')]
  elsif key.to_s =~ UPCASE && obj.class.const_defined?(key)
    obj.class.const_get(key)
  elsif args.is_a?(Hash) && args.key?(key)
    args[key]
  elsif obj.respond_to?(key, true)
    obj.send(key)
  else
    raise KeyError, key
  end
end

#modifier(key) ⇒ Object



139
140
141
# File 'lib/dpl/helper/interpolate.rb', line 139

def modifier(key)
  MODIFIER.detect { |mod| key.to_s.start_with?("#{mod}d_") }
end

#normalize(obj) ⇒ Object



116
117
118
# File 'lib/dpl/helper/interpolate.rb', line 116

def normalize(obj)
  obj.is_a?(Array) ? obj.join(' ') : obj.to_s
end

#obfuscate(str) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/dpl/helper/interpolate.rb', line 101

def obfuscate(str)
  secrets(str).inject(str) do |str, secret|
    secret = secret.dup if secret.frozen?
    secret.blacklist if str.blacklisted?
    str.gsub(secret, super(secret))
  end
end

#secrets(str) ⇒ Object



109
110
111
112
113
114
# File 'lib/dpl/helper/interpolate.rb', line 109

def secrets(str)
  return [] unless str.is_a?(String) && str.blacklisted?
  opts = obj.class.opts.select(&:secret?)
  secrets = opts.map { |opt| obj.opts[opt.name] }.compact
  secrets.select { |secret| str.include?(secret) }
end

#var?(key) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
# File 'lib/dpl/helper/interpolate.rb', line 143

def var?(key)
  vars.include?(key)
end

#varsObject



147
148
149
# File 'lib/dpl/helper/interpolate.rb', line 147

def vars
  opts[:vars]
end

#vars?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/dpl/helper/interpolate.rb', line 151

def vars?
  !!vars
end