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.



2121
2122
2123
2124
# File 'lib/warg.rb', line 2121

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

Instance Method Details

#[](key) ⇒ Object



2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
# File 'lib/warg.rb', line 2134

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



2145
2146
2147
# File 'lib/warg.rb', line 2145

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


2126
2127
2128
2129
2130
2131
2132
# File 'lib/warg.rb', line 2126

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