Class: Topicz::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/topicz/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, exclude_filters = []) ⇒ Repository

Returns a new instance of Repository.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/topicz/repository.rb', line 9

def initialize(root, exclude_filters = [])
  @root = root
  @topics = {}
  errors = []
  excludes = exclude_filters.map { | filter | Regexp.new(filter) }
  Dir.foreach(root) do |path|
    next if path.start_with?('.')
    next if excludes.any? { | regexp | path =~ regexp }
    next unless File.directory?(File.join(root, path))
    topic = Topic.new(root, path)
    if @topics.has_key?(topic.id)
      errors << "Error in topic '#{topic.title}': ID '#{topic.id}' is already in use."
    end
    @topics[topic.id] = topic
  end
  @topics.each_value do |topic|
    topic.references.each do |ref|
      unless @topics.has_key?(ref)
        errors << "Error in topic '#{topic.title}': Reference to non-existing topic ID '#{ref}'."
      end
    end
  end
  unless errors.empty?
    raise errors
  end
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



7
8
9
# File 'lib/topicz/repository.rb', line 7

def root
  @root
end

Instance Method Details

#[](id) ⇒ Object



36
37
38
# File 'lib/topicz/repository.rb', line 36

def [](id)
  @topics[id]
end

#exist_title?(title) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/topicz/repository.rb', line 40

def exist_title?(title)
  @topics.values.any? { |t| t.title == title }
end

#find_all(filter = nil) ⇒ Object



48
49
50
# File 'lib/topicz/repository.rb', line 48

def find_all(filter = nil)
  @topics.values.select { |t| t.matches(filter) }
end

#topicsObject



44
45
46
# File 'lib/topicz/repository.rb', line 44

def topics
  @topics.values
end