Class: MacSetup::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/mac_setup/configuration.rb

Constant Summary collapse

InvalidConfigError =
Class.new(StandardError)
DEFAULT_KEYS =
[
  :repo, :plugins, :git_repos, :symlinks, :taps, :brews, :fonts, :casks, :quicklook, :mas, :extra_dotfiles
]

Instance Method Summary collapse

Constructor Details

#initialize(config_path) ⇒ Configuration

Returns a new instance of Configuration.



11
12
13
14
# File 'lib/mac_setup/configuration.rb', line 11

def initialize(config_path)
  @config_path = config_path
  load_config
end

Instance Method Details

#add(type, value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mac_setup/configuration.rb', line 25

def add(type, value)
  add_method = "add_#{type}"

  if respond_to?(add_method, include_private: true)
    send(add_method, value)
  else
    collection = public_send(type)

    case collection
    when Set
      collection << value.to_s
    when Hash
      collection.merge!(value) do |key, oldval, newval|
        raise InvalidConfigError, "#{key} is defined twice!: #{oldval}, #{newval}"
      end
    end
  end
end

#brewsObject



76
77
78
79
80
# File 'lib/mac_setup/configuration.rb', line 76

def brews
  @brews ||= (@config["brews"] || []).each_with_object({}) do |item, merged|
    add_brews(item, merged)
  end
end

#casksObject



86
87
88
# File 'lib/mac_setup/configuration.rb', line 86

def casks
  @casks ||= Set.new(@config["casks"])
end

#dotfiles_repoObject



56
57
58
# File 'lib/mac_setup/configuration.rb', line 56

def dotfiles_repo
  @config.fetch("repo")
end

#extra_dotfilesObject



60
61
62
# File 'lib/mac_setup/configuration.rb', line 60

def extra_dotfiles
  @config.fetch("extra_dotfiles", [])
end

#fontsObject



82
83
84
# File 'lib/mac_setup/configuration.rb', line 82

def fonts
  @fonts ||= Set.new(@config["fonts"])
end

#git_reposObject



64
65
66
# File 'lib/mac_setup/configuration.rb', line 64

def git_repos
  @git_repos ||= @config["git_repos"] || {}
end

#masObject



94
95
96
# File 'lib/mac_setup/configuration.rb', line 94

def mas
  @mas ||= @config["mas"] || {}
end

#pluginsObject



44
45
46
# File 'lib/mac_setup/configuration.rb', line 44

def plugins
  @plugins ||= Set.new(@config["plugins"])
end

#quicklookObject



90
91
92
# File 'lib/mac_setup/configuration.rb', line 90

def quicklook
  @quicklook ||= Set.new(@config["quicklook"])
end

#require_value(key) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/mac_setup/configuration.rb', line 16

def require_value(key)
  value = @config.fetch(key.to_s) do
    raise InvalidConfigError, "Missing config value for #{key}!"
  end

  define_singleton_method(key) { value }
  allowed_keys << key.to_sym
end


68
69
70
# File 'lib/mac_setup/configuration.rb', line 68

def symlinks
  @symlinks ||= @config["symlinks"] || {}
end

#tapsObject



72
73
74
# File 'lib/mac_setup/configuration.rb', line 72

def taps
  @taps ||= (@config["taps"] || []).map { |item| item.split(/\s+/) }.to_set
end

#validate!Object

Raises:



48
49
50
51
52
53
54
# File 'lib/mac_setup/configuration.rb', line 48

def validate!
  extra_keys = @config.keys.map(&:to_sym) - allowed_keys.to_a

  return if extra_keys.none?

  raise InvalidConfigError, "Extra keys in config: #{extra_keys.join(', ')}"
end