Class: Topicz::Commands::RepositoryCommand

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

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil) ⇒ RepositoryCommand

Returns a new instance of RepositoryCommand.



9
10
11
12
# File 'lib/topicz/commands/repository_command.rb', line 9

def initialize(config_file = nil)
  @config = load_config(config_file)
  @repository = load_repository
end

Instance Method Details

#find_exactly_one_topic(filter, strict) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/topicz/commands/repository_command.rb', line 45

def find_exactly_one_topic(filter, strict)
  if strict
    topic = @repository[filter]
    if topic == nil
      raise "No topic found with ID: '#{filter}'"
    end
    topic
  else
    topics = @repository.find_all filter
    if topics.length == 0
      raise "No topics found matching the search filter: '#{filter}'"
    end
    if topics.length > 1
      matches = topics.map { |t| t.title }.join("\n")
      raise "Multiple topics match the search filter: '#{filter}'. Matches:\n#{matches}"
    end
    topics[0]
  end
end

#load_config(config_file) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/topicz/commands/repository_command.rb', line 14

def load_config(config_file)
  file = config_file != nil ? config_file : Topicz::DEFAULT_CONFIG_LOCATION
  unless File.exist? file
    raise "File doesn't exist: #{file}."
  end
  unless File.readable? file
    raise "File isn't readable: #{file}."
  end
  begin
    config = YAML.load_file(file)
  rescue
    raise "Not a valid YAML file: #{file}."
  end
  unless config.has_key?('repository')
    raise "Missing required property 'repository' in configuration file: #{file}."
  end
  config
end

#load_repositoryObject



33
34
35
36
37
38
39
# File 'lib/topicz/commands/repository_command.rb', line 33

def load_repository
  directory = @config['repository']
  unless Dir.exist? directory
    raise "Repository directory doesn't exist: #{directory}."
  end
  Topicz::Repository.new(directory, process_excludes(@config['excludes']))
end

#process_excludes(excludes_config) ⇒ Object



41
42
43
# File 'lib/topicz/commands/repository_command.rb', line 41

def process_excludes(excludes_config)
  [excludes_config].compact.flatten
end