Class: Q::Scope

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

Direct Known Subclasses

ToplevelScope

Instance Method Summary collapse

Constructor Details

#initialize(parents = nil) ⇒ Scope

Returns a new instance of Scope.



3
4
5
6
7
8
# File 'lib/q/scope.rb', line 3

def initialize parents = nil
  @map = {}
  @args = []
  @parents = [ parents ].flatten.compact
  @this = nil
end

Instance Method Details

#[](name) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# 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) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# 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

#argsObject



56
57
58
# File 'lib/q/scope.rb', line 56

def args
  @args
end

#args=(args) ⇒ Object



52
53
54
# File 'lib/q/scope.rb', line 52

def args= args
  @args = [args].flatten
end

#has?(name) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/q/scope.rb', line 48

def has_own? name
  @map.has_key? name
end

#inspectObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# 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

#thisObject



68
69
70
71
72
73
74
# File 'lib/q/scope.rb', line 68

def this
  if has_own? '@'
    return @this = @map['@']
  end

  @this
end

#this=(th) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/q/scope.rb', line 60

def this= th
  if has_own? '@'
    return @map['@'] = @this = th
  end

  @this = th
end