Class: Tablescript::Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/tablescript/namespace.rb

Overview

Namespace

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = '', parent = nil) ⇒ Namespace

Returns a new instance of Namespace.



25
26
27
28
29
30
# File 'lib/tablescript/namespace.rb', line 25

def initialize(name = '', parent = nil)
  @name = name
  @parent = parent
  @namespaces = {}
  @tables = {}
end

Instance Attribute Details

#namespacesObject (readonly)

Returns the value of attribute namespaces.



23
24
25
# File 'lib/tablescript/namespace.rb', line 23

def namespaces
  @namespaces
end

#parentObject (readonly)

Returns the value of attribute parent.



23
24
25
# File 'lib/tablescript/namespace.rb', line 23

def parent
  @parent
end

#tablesObject (readonly)

Returns the value of attribute tables.



23
24
25
# File 'lib/tablescript/namespace.rb', line 23

def tables
  @tables
end

Instance Method Details

#add(table) ⇒ Object

Raises:



32
33
34
35
# File 'lib/tablescript/namespace.rb', line 32

def add(table)
  raise Exception, "Table #{table.name} already defined" if @tables.key?(table.name)
  @tables[table.name] = table
end

#each_namespace(&blk) ⇒ Object



77
78
79
# File 'lib/tablescript/namespace.rb', line 77

def each_namespace(&blk)
  @namespaces.each_value(&blk)
end

#each_table(&blk) ⇒ Object



81
82
83
# File 'lib/tablescript/namespace.rb', line 81

def each_table(&blk)
  @tables.each_value(&blk)
end

#nameObject



72
73
74
75
# File 'lib/tablescript/namespace.rb', line 72

def name
  return 'global' if @parent.nil?
  @name
end

#namespace(name) ⇒ Object



68
69
70
# File 'lib/tablescript/namespace.rb', line 68

def namespace(name)
  @namespaces[name] ||= Namespace.new(name, self)
end

#resolve(path) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/tablescript/namespace.rb', line 46

def resolve(path)
  parts = path.split('/')
  return table(path) if parts.size == 1
  if parts[0] == @name
    return table(parts[1]) if parts.size == 2
    raise Exception, "Namespace #{parts[1]} not found in #{name} namespace (#{path})" unless @namespaces.key?(parts[1])
    return @namespaces[parts[1]].resolve(parts[1..-1].join('/'))
  else
    raise Exception, "Namespace #{parts[0]} not found in #{name} namespace (#{path})" unless @namespaces.key?(parts[0])
    return @namespaces[parts[0]].resolve(parts[1..-1].join('/'))
  end
end

#resolve?(path) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
# File 'lib/tablescript/namespace.rb', line 37

def resolve?(path)
  begin
    resolve(path)
  rescue Tablescript::Exception
    return false
  end
  true
end

#table(table_name) ⇒ Object

Raises:



59
60
61
62
# File 'lib/tablescript/namespace.rb', line 59

def table(table_name)
  raise Exception, "No such table #{table_name} in #{name}" unless @tables.key?(table_name)
  @tables[table_name]
end

#table?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/tablescript/namespace.rb', line 64

def table?(table_name)
  @tables.key?(table_name)
end