Class: Talk::Registry
- Inherits:
-
Object
- Object
- Talk::Registry
- Defined in:
- lib/registry.rb
Class Method Summary collapse
- .add(name, namespace, file, line, delimiter = nil) ⇒ Object
- .add_reverse_lookup(split_name, namespace, entry, delimiter) ⇒ Object
- .get_exact_registrations(name, namespace) ⇒ Object
- .get_inexact_registrations(name, namespace) ⇒ Object
- .get_registrations(name, namespace, match_exact = false) ⇒ Object
- .registered?(name, namespace, match_exact = false) ⇒ Boolean
- .render_level(level, at_depth = 1) ⇒ Object
- .reset ⇒ Object
- .to_s ⇒ Object
Class Method Details
.add(name, namespace, file, line, delimiter = nil) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/registry.rb', line 57 def add(name, namespace, file, line, delimiter=nil) if registered?(name, namespace) then old_reg = get_registrations(name, namespace).last Talk::Parser.error(nil, file, line, "Duplicate registration #{name} in #{namespace}; previously defined at #{old_reg}") end @registry ||= {} @registry[namespace] ||= {} split_name = delimiter.nil? ? [*name] : name.to_s.split(delimiter) # create each level of the split name level = @registry[namespace] split_name[0..-2].each do |component| # all but the last component level[component] ||= RegistryEntry.new level = level[component] end level[split_name.last] ||= RegistryEntry.new(name, file, line) level[split_name.last].make_entry(name, file, line) # in case it already existed as a container add_reverse_lookup(split_name, namespace, level[split_name.last], delimiter) end |
.add_reverse_lookup(split_name, namespace, entry, delimiter) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/registry.rb', line 79 def add_reverse_lookup(split_name, namespace, entry, delimiter) @reverse ||= {} @reverse[namespace] ||= {} split_name.reverse.each_prefix do |prefix| name = prefix.reverse.join(delimiter) @reverse[namespace][name] ||= [] @reverse[namespace][name].push entry end end |
.get_exact_registrations(name, namespace) ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/registry.rb', line 99 def get_exact_registrations(name, namespace) level = @registry[namespace] [*name].each do |component| return [] if level.nil? level = level[component] end return [*level] if not level.nil? and level.is_entry? [] end |
.get_inexact_registrations(name, namespace) ⇒ Object
110 111 112 113 |
# File 'lib/registry.rb', line 110 def get_inexact_registrations(name, namespace) return [] if @reverse.nil? or @reverse[namespace].nil? or @reverse[namespace][name].nil? @reverse[namespace][name] end |
.get_registrations(name, namespace, match_exact = false) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/registry.rb', line 89 def get_registrations(name, namespace, match_exact = false) @registry ||= {} exact = get_exact_registrations(name, namespace) return exact unless exact.empty? return [] if match_exact get_inexact_registrations(name, namespace) end |
.registered?(name, namespace, match_exact = false) ⇒ Boolean
115 116 117 |
# File 'lib/registry.rb', line 115 def registered?(name, namespace, match_exact = false) not get_registrations(name, namespace, match_exact).empty? end |
.render_level(level, at_depth = 1) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/registry.rb', line 119 def render_level(level, at_depth=1) indent = " "*at_depth s = "" level.keys.sort.each do |key| value = level[key] s += indent + key s += " (entry from #{File.basename(value.file)}:#{value.line})" if value.is_entry? s += "\n" s += render_level(value, at_depth+1) end s end |
.reset ⇒ Object
133 134 135 136 |
# File 'lib/registry.rb', line 133 def reset @registry = nil @reverse = nil end |
.to_s ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/registry.rb', line 138 def to_s return "Empty registry" if @registry.nil? or @registry.empty? s = "" @registry.keys.sort.each do |namespace| level = @registry[namespace] s += "Namespace #{namespace}:\n" s += render_level(level) end s end |