Class: SymTbl

Inherits:
Object show all
Defined in:
lib/sym_tbl.rb

Defined Under Namespace

Classes: Trigger

Constant Summary collapse

@@sid =
-1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(father_env = nil, default = nil) ⇒ SymTbl

Returns a new instance of SymTbl.



16
17
18
19
20
21
22
23
24
25
# File 'lib/sym_tbl.rb', line 16

def initialize ( father_env=nil, default=nil )
  @sid = (@@sid += 1)
  @local = Hash.new(default)
  if father_env.is_a? Hash
    @father = nil
    merge!(father_env)
  else
    @father = father_env
  end
end

Instance Attribute Details

#fatherObject (readonly)

Returns the value of attribute father.



14
15
16
# File 'lib/sym_tbl.rb', line 14

def father
  @father
end

#localObject (readonly)

Returns the value of attribute local.



14
15
16
# File 'lib/sym_tbl.rb', line 14

def local
  @local
end

#sidObject (readonly)

Returns the value of attribute sid.



14
15
16
# File 'lib/sym_tbl.rb', line 14

def sid
  @sid
end

Instance Method Details

#[](aKey) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sym_tbl.rb', line 31

def [] ( aKey )
  return nil if aKey == ''
  aKey = key_convert(aKey)
  result =
    if @local.has_key? aKey
      @local[aKey]
    elsif @father.nil?
      @local.default
    else
      @father[aKey]
    end
  result = result[] if result.is_a? Trigger
  result
end

#[]=(aKey, aValue) ⇒ Object



46
47
48
49
# File 'lib/sym_tbl.rb', line 46

def []= ( aKey, aValue )
  aKey = key_convert(aKey)
  @local[aKey] = aValue
end

#ancestorsObject



62
63
64
# File 'lib/sym_tbl.rb', line 62

def ancestors
  (@father.is_a?(SymTbl))? [self] + @father.ancestors : [self]
end

#descObject



75
76
77
# File 'lib/sym_tbl.rb', line 75

def desc
  ancestors.map{ |s| s.desc_one }.to_yaml
end

#desc_oneObject



66
67
68
69
70
71
72
73
# File 'lib/sym_tbl.rb', line 66

def desc_one
  if defined? @already_described
    return sid
  else
    @already_described = true
    return { sid => @local }
  end
end

#each(&block) ⇒ Object

FIXME



52
53
54
55
56
57
58
59
60
# File 'lib/sym_tbl.rb', line 52

def each ( &block )
  key_set = Set.new
  blk = lambda do |k,v|
    block[k, v] unless key_set.include? k
    key_set << k
  end
  @local.each(&blk)
  @father.each(&blk) unless @father.nil?
end

#has_key?(key) ⇒ Boolean Also known as: key?, include?, member?

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/sym_tbl.rb', line 91

def has_key?(key)
  key = key_convert(key)
  if @local.has_key?(key)
    true
  else
    if @father.nil?
      false
    else
      @father.has_key?(key)
    end
  end
end

#has_local_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/sym_tbl.rb', line 108

def has_local_key?(key)
  @local.has_key?(key_convert(key))
end

#key_convert(aKey) ⇒ Object



27
28
29
# File 'lib/sym_tbl.rb', line 27

def key_convert ( aKey )
  (aKey.is_a? Symbol)? aKey : aKey.to_s.to_sym
end

#merge(other) ⇒ Object



79
80
81
82
# File 'lib/sym_tbl.rb', line 79

def merge(other)
  symtbl = self.class.new(self)
  symtbl.merge!(other)
end

#merge!(other) ⇒ Object Also known as: update



84
85
86
87
# File 'lib/sym_tbl.rb', line 84

def merge!(other)
  other.each { |k, v| @local[key_convert(k)] = v }
  self
end

#new_childObject



112
113
114
# File 'lib/sym_tbl.rb', line 112

def new_child
  self.class.new(self)
end