class Balmora::Variables

Public Class Methods

factory(state) click to toggle source
# File lib/balmora/variables.rb, line 5
def self.factory(state)
  return self.new(state.extension, state.shell, state)
end
new(extension, shell, state) click to toggle source
# File lib/balmora/variables.rb, line 9
def initialize(extension, shell, state)
  @extension = extension
  @shell = shell
  @state = state
end

Public Instance Methods

_inject_array(value, string) click to toggle source
# File lib/balmora/variables.rb, line 63
def _inject_array(value, string)
  result = []

  value.each_with_index() { |item, index|
    if item.instance_of?(::Hash) && item.keys() == [:'extend-variable']
      result += get(item[:'extend-variable'])
    else
      result.push(inject(item, string))
    end
  }

  return result
end
_inject_hash(value, string) click to toggle source
# File lib/balmora/variables.rb, line 77
def _inject_hash(value, string)
  result = {}
  value.each() { |key, item|
    if key == :'include-variable'
      result.merge!(get(item))
    else
      result[key] = inject(item, string)
    end
  }

  return result
end
_inject_string(value) click to toggle source
# File lib/balmora/variables.rb, line 90
def _inject_string(value)
  value = value.gsub('\\\\', '{{__ESCAPED_SLASH__}}')

  if value.match(/(?<!\\)\#\{/)
    value = value.gsub('\\#', '#')
    value = eval('"' + value + '"')
  end

  value = value.gsub(/(?<!\\)\${(.*?)}/) { |string | get(string[2...-1]) }
  value = value.gsub(/(?<!\\)\%{(.*?)}/) { |string |
    command = @shell.expression(string[2...-1])
    @shell.run!([command], verbose: false, message: 'Executing embedded ' +
      'command: ').rstrip()
  }

  value = value.gsub('\\$', '$')
  value = value.gsub('\\%', '%')
  value = value.gsub('{{__ESCAPED_SLASH__}}', '\\')
end
get(name) click to toggle source
# File lib/balmora/variables.rb, line 15
def get(name)
  if !name.instance_of?(::Array)
    name = name.split('.').collect() { |part| part.to_sym() }
  else
    name = name.clone()
  end

  parts = []
  provider = @extension.get(Balmora::Variables, name.shift()).factory(@state)

  result =
    name.
    inject(provider) { |current, part|
      parts.push(part)

      if current.instance_of?(::Hash)
        if !current.has_key?(part)
          raise Error.new("Unknown variable #{parts.join('.')}")
        end

        next current[part]
      end

      if !current.respond_to?(part)
        raise Error.new("Unknown variable #{parts.join('.')}")
      end

      next current.public_send(part)
    }

  return result
end
inject(value, string = true) click to toggle source
# File lib/balmora/variables.rb, line 48
def inject(value, string = true)
  result =
    if value.instance_of?(::Array)
      _inject_array(value, string)
    elsif value.instance_of?(::Hash)
      _inject_hash(value, string)
    elsif value.instance_of?(::String) && string
      _inject_string(value)
    else
      value
    end

  return result
end