class Q::Scope

Public Class Methods

new(parents = nil) click to toggle source
# File lib/q/scope.rb, line 3
def initialize parents = nil
  @map = {}
  @args = []
  @parents = [ parents ].flatten.compact
  @this = nil
end

Public Instance Methods

[](name) click to toggle source
# File lib/q/scope.rb, line 10
def [] name
  return @map[name] if has_own? name

  @parents.each do |parent|
    if parent.has? name
      return parent[name]
    end
  end

  return nil
end
[]=(name, value) click to toggle source
# File lib/q/scope.rb, line 22
def []= name, value
  return @map[name] = value if has_own? name

  @parents.each do |parent|
    if parent.has? name
      return parent[name] = value
    end
  end

  @map[name] = value
end
args() click to toggle source
# File lib/q/scope.rb, line 56
def args
  @args
end
args=(args) click to toggle source
# File lib/q/scope.rb, line 52
def args= args
  @args = [args].flatten
end
has?(name) click to toggle source
# File lib/q/scope.rb, line 34
def has? name
  if has_own? name
    return true
  end

  @parents.each do |parent|
    if parent.has? name
      return true
    end
  end

  return false
end
has_own?(name) click to toggle source
# File lib/q/scope.rb, line 48
def has_own? name
  @map.has_key? name
end
inspect() click to toggle source
# File lib/q/scope.rb, line 76
def inspect
  str = "Q::Scope --\n"

  @map.each do |key, value|
    str += "  #{key} => #{value}\n"
  end

  if not @parents.empty?
    str += "\n  parents:\n"

    @parents.each do |parent|
      str += "  #{parent.inspect}\n"
    end
  end

  str
end
this() click to toggle source
# File lib/q/scope.rb, line 68
def this
  if has_own? '@'
    return @this = @map['@']
  end

  @this
end
this=(th) click to toggle source
# File lib/q/scope.rb, line 60
def this= th
  if has_own? '@'
    return @map['@'] = @this = th
  end

  @this = th
end