Class: Topicz::Repository
- Inherits:
-
Object
- Object
- Topicz::Repository
- Defined in:
- lib/topicz/repository.rb
Instance Attribute Summary collapse
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
- #[](id) ⇒ Object
- #exist_title?(title) ⇒ Boolean
- #find_all(filter = nil) ⇒ Object
-
#initialize(root) ⇒ Repository
constructor
A new instance of Repository.
- #topics ⇒ Object
Constructor Details
#initialize(root) ⇒ 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 |
# File 'lib/topicz/repository.rb', line 9 def initialize(root) @root = root @topics = {} errors = [] Dir.foreach(root) do |path| next if path.start_with?('.') 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
#root ⇒ Object (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
34 35 36 |
# File 'lib/topicz/repository.rb', line 34 def [](id) @topics[id] end |
#exist_title?(title) ⇒ Boolean
38 39 40 |
# File 'lib/topicz/repository.rb', line 38 def exist_title?(title) @topics.values.any? { |t| t.title == title } end |
#find_all(filter = nil) ⇒ Object
46 47 48 |
# File 'lib/topicz/repository.rb', line 46 def find_all(filter = nil) @topics.values.select { |t| t.matches(filter) } end |
#topics ⇒ Object
42 43 44 |
# File 'lib/topicz/repository.rb', line 42 def topics @topics.values end |