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.



2117
2118
2119
2120
# File 'lib/warg.rb', line 2117

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

Instance Method Details

#[](key) ⇒ Object



2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
# File 'lib/warg.rb', line 2130

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



2141
2142
2143
# File 'lib/warg.rb', line 2141

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

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


2122
2123
2124
2125
2126
2127
2128
# File 'lib/warg.rb', line 2122

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