Class: Prodder::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/prodder/config.rb

Constant Summary collapse

PathError =
Class.new(StandardError)
LintError =
Class.new(StandardError) do
  attr_reader :errors

  def initialize(errors)
    @errors = errors
    super errors.join "\n"
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_definitions) ⇒ Config

Returns a new instance of Config.



19
20
21
# File 'lib/prodder/config.rb', line 19

def initialize(project_definitions)
  @config = project_definitions
end

Instance Attribute Details

#workspaceObject

Returns the value of attribute workspace.



17
18
19
# File 'lib/prodder/config.rb', line 17

def workspace
  @workspace
end

Class Method Details

.example_contentsObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/prodder/config.rb', line 75

def self.example_contents
  <<-EOF.gsub(/^      /, '')
  blog:
    structure_file: db/structure.sql
    seed_file: db/seeds.sql
    quality_check_file: db/quality_checks.sql
    git:
      origin: [email protected]:your/repo.git
      author: prodder <[email protected]>
    db:
      name: database_name
      host: database.server.example.com
      user: username
      password: password
      tables:
        - posts
        - authors
  EOF
end

Instance Method Details

#assert_in_path(*cmds) ⇒ Object



23
24
25
26
27
28
# File 'lib/prodder/config.rb', line 23

def assert_in_path(*cmds)
  path = ENV['PATH'].split(File::PATH_SEPARATOR)
  cmds.each do |cmd|
    raise PathError.new(cmd) unless path.find { |dir| File.exist? File.join(dir, cmd) }
  end
end

#lintObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/prodder/config.rb', line 30

def lint
  assert_in_path 'pg_dump'
  assert_in_path 'git'

  required = {
    'structure_file'     => [],
    'seed_file'          => [],
    'db'                 => %w[name host user tables],
    'git'                => %w[origin author]
  }

  @config.each_with_object([]) do |(project, defn), errors|
    required.each do |top, inner|
      if !defn.key?(top)
        errors << "Missing required configuration key: #{project}/#{top}"
      else
        errors.concat inner.reject { |key| defn[top].key?(key) }.map { |key|
          "Missing required configuration key: #{project}/#{top}/#{key}"
        }
      end
    end
  end
end

#lint!Object



54
55
56
# File 'lib/prodder/config.rb', line 54

def lint!
  lint.tap { |errors| raise LintError.new(errors) if errors.any? }
end

#projectsObject



58
59
60
# File 'lib/prodder/config.rb', line 58

def projects
  @projects ||= @config.map { |name, defn| Project.new(name, File.join(workspace, name), defn) }
end

#select_projects(names) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/prodder/config.rb', line 62

def select_projects(names)
  return projects if names.empty?

  matches = projects.select { |project| names.include?(project.name) }

  if matches.size != names.size
    unmatched = names - matches.map(&:name)
    yield unmatched if block_given?
  end

  matches
end