Class Topical::TopicTree
In: lib/nukumi2/topictree-extensions.rb
lib/nukumi2/vendor/topical.rb
Parent: Object

Methods

Included Modules

Enumerable

Constants

Contents = Struct.new(:name, :description, :data)

Attributes

data  [R] 

Public Class methods

[Source]

    # File lib/nukumi2/vendor/topical.rb, line 12
12:     def initialize
13:       @data = {}
14:     end

[Source]

     # File lib/nukumi2/vendor/topical.rb, line 146
146:     def self.path2regexp(path, submatch=true)
147:       unless path =~ /\A[-\w\/*]*\Z/ and not path.index '//' \
148:                                      and not path.index '***'
149:         raise ParseError, "Invalid topic: #{path}"
150:       end
151: 
152:       path = path + "/"  unless path[-1] == ?/
153: 
154:       regex = ""
155:       regex << "^"  if path =~ /^\//
156:       regex << path.gsub(/((\*\*?\/)+)/) {
157: 
158:         piece = $1
159: 
160:         if piece =~ /\*\*/
161:           n = (piece.count('/')-piece.scan("**").length).to_s + ","
162:         else
163:           n = piece.count('/').to_s
164:         end
165: 
166:         "([^/]*/){#{n}}"
167:       }
168:       regex << "$"  unless submatch
169: 
170:       Regexp.new(regex)
171:     end

Public Instance methods

[Source]

     # File lib/nukumi2/vendor/topical.rb, line 116
116:     def [](*args)
117:       if args.size == 0
118:         nil
119:       elsif args.size == 1
120:         fetch args.first
121:       else
122:         fetch_unified(*args)
123:       end
124:     end

[Source]

    # File lib/nukumi2/vendor/topical.rb, line 56
56:     def add(path, object)
57:       added = false
58: 
59:       each(path, false) { |content|
60:         content.data << object
61:         added = true
62:       }
63: 
64:       raise "No matching topic found for #{path}."  unless added
65:       object
66:     end

[Source]

     # File lib/nukumi2/vendor/topical.rb, line 173
173:     def check_absolute_path path
174:       unless path =~ %r{\A/([\w_-]+/)*\Z}
175:         raise ParseError, "Invalid absolute topic: #{path}"
176:       end
177:     end

[Source]

    # File lib/nukumi2/vendor/topical.rb, line 16
16:     def clear!
17:       @data = {}
18:     end

[Source]

    # File lib/nukumi2/vendor/topical.rb, line 20
20:     def create(path, name=nil, description=nil, data=[])
21:       path = path + "/"  unless path[-1] == ?/
22: 
23:       begin
24:         check_absolute_path path
25:       rescue ParseError
26:         raise ParseError, "Invalid topic to create: #{path}"
27:       end
28: 
29:       paths = []
30:       path.split("/").each { |piece|
31:         paths << paths.last.to_s + piece + "/"
32:       }
33:       paths << path  if path == "/"
34: 
35:       paths.each { |subpath|
36:         if subpath == paths.last
37:           unless @data[subpath].kind_of?(Contents)
38:             @data[subpath] = Contents.new(name, description, data)
39:           end
40:           @data[subpath].name = name
41:         else
42:           unless @data[subpath].kind_of?(Contents)
43:             @data[subpath] = Contents.new
44:           end
45:         end
46:       }
47:     end

[Source]

    # File lib/nukumi2/vendor/topical.rb, line 68
68:     def delete(object)
69:       @data.each { |k, v|
70:         v.data.delete object  unless v.data.nil?
71:       }
72:     end

[Source]

    # File lib/nukumi2/vendor/topical.rb, line 49
49:     def describe(path, name, description=nil)
50:       each(path, false) { |contents|
51:         contents.name = name
52:         contents.description = description
53:       }
54:     end

[Source]

    # File lib/nukumi2/vendor/topical.rb, line 84
84:     def description(path)
85:       path = path + "/"  unless path[-1] == ?/
86:       check_absolute_path path
87:       if @data[path]
88:         @data[path].description
89:       else
90:         nil
91:       end
92:     end

[Source]

     # File lib/nukumi2/vendor/topical.rb, line 126
126:     def each(path, submatch=true, &block)
127:       each_path(path, submatch) { |key| block.call @data[key] }
128:     end

[Source]

     # File lib/nukumi2/vendor/topical.rb, line 130
130:     def each_path(path, submatch=true, &block)
131:       path = self.class.path2regexp path, submatch
132:       @data.keys.grep(path).each { |key| block.call key }
133:     end

[Source]

     # File lib/nukumi2/vendor/topical.rb, line 94
 94:     def fetch(path)
 95:       result = []
 96:       each(path) { |content|
 97:         result.concat content.data  unless content.data.nil?
 98:       }
 99:       result.uniq
100:     end

[Source]

     # File lib/nukumi2/vendor/topical.rb, line 102
102:     def fetch_unified(path, *selectors)
103:       result = fetch path
104: 
105:       return result  if selectors.empty?
106:   
107:       result.map { |e|
108:         if selectors.all? { |sel| fetch(sel).include? e }
109:           e
110:         else
111:           nil
112:         end
113:       }.compact
114:     end

[Source]

   # File lib/nukumi2/topictree-extensions.rb, line 2
2:   def find_most_specific(path, alternatives)
3:     # Assume the path with least '*' is the most fitting one...
4:     alternatives.find_all { |(topic, view)|
5:       path =~ self.class.path2regexp(topic, false)
6:     }.min { |a, b| a.count('*') <=> b.count('*') }
7:   end

[Source]

    # File lib/nukumi2/vendor/topical.rb, line 74
74:     def name(path)
75:       path = path + "/"  unless path[-1] == ?/
76:       check_absolute_path path
77:       if @data[path]
78:         @data[path].name
79:       else
80:         nil
81:       end
82:     end

[Source]

     # File lib/nukumi2/vendor/topical.rb, line 135
135:     def subtopics(root='/')
136:       r = self.class.path2regexp root, true
137: 
138:       s = []
139:       each_path(root) { |p|
140:         s << p.gsub(r, '').gsub(/\/.*/, '')
141:       }
142:       s.delete ""
143:       s.uniq
144:     end

[Validate]