Class: Warg::Script::Interpolations

Inherits:
Object
  • Object
show all
Defined in:
lib/warg.rb

Constant Summary collapse

CONTEXT_REGEXP =
/variables:(\w+)/

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Interpolations

Returns a new instance of Interpolations.



2207
2208
2209
2210
# File 'lib/warg.rb', line 2207

def initialize(context)
  @context = context
  @values = {}
end

Instance Method Details

#[](key) ⇒ Object



2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
# File 'lib/warg.rb', line 2220

def [](key)
  if @values.key?(key)
    @values[key]
  elsif key =~ CONTEXT_REGEXP && @context.variables_set_defined?($1)
    variables = @context[$1]
    content = variables.to_h.sort.map { |key, value| %{#{key}="#{value}"} }.join("\n")

    @values[key] = content
  end
end

#[]=(key, value) ⇒ Object



2231
2232
2233
# File 'lib/warg.rb', line 2231

def []=(key, value)
  @values[key] = value
end

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


2212
2213
2214
2215
2216
2217
2218
# File 'lib/warg.rb', line 2212

def key?(key)
  if key =~ CONTEXT_REGEXP
    @context.variables_set_defined?($1)
  else
    @values.key?(key)
  end
end