Class: MacSetup::Configuration

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

Constant Summary collapse

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

Instance Method Summary collapse

Constructor Details

#initialize(config_path) ⇒ Configuration

Returns a new instance of Configuration.



9
10
11
12
# File 'lib/mac_setup/configuration.rb', line 9

def initialize(config_path)
  @config_path = config_path
  load_config
end

Instance Method Details

#add(type, value) ⇒ Object



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

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



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

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

#casksObject



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

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

#dotfiles_repoObject



54
55
56
# File 'lib/mac_setup/configuration.rb', line 54

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

#fontsObject



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

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

#git_reposObject



58
59
60
# File 'lib/mac_setup/configuration.rb', line 58

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

#masObject



84
85
86
# File 'lib/mac_setup/configuration.rb', line 84

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

#pluginsObject



42
43
44
# File 'lib/mac_setup/configuration.rb', line 42

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

#quicklookObject



80
81
82
# File 'lib/mac_setup/configuration.rb', line 80

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

#require_value(key) ⇒ Object



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

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


62
63
64
# File 'lib/mac_setup/configuration.rb', line 62

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

#validate!Object

Raises:



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

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

  return if extra_keys.none?

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