Class: Twin::Config

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

Constant Summary collapse

DEFAULTS =
{
  "global_excludes"           => [".DS_Store"],
  "apex_theme"                => nil,
  "apex_width"                => nil,
  "apex_code_highlight"       => nil,
  "apex_code_highlight_theme" => nil,
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Config

Returns a new instance of Config.



18
19
20
21
22
23
24
25
26
# File 'lib/twin/config.rb', line 18

def initialize(data = {})
  merged = DEFAULTS.merge(data || {})
  @sync_dir                  = ENV["TWIN_SYNC_DIR"] || merged["sync_dir"].to_s
  @global_excludes           = merged["global_excludes"] || []
  @apex_theme                = merged["apex_theme"]
  @apex_width                = merged["apex_width"]
  @apex_code_highlight       = merged["apex_code_highlight"]
  @apex_code_highlight_theme = merged["apex_code_highlight_theme"]
end

Instance Attribute Details

#apex_code_highlightObject

Returns the value of attribute apex_code_highlight.



6
7
8
# File 'lib/twin/config.rb', line 6

def apex_code_highlight
  @apex_code_highlight
end

#apex_code_highlight_themeObject

Returns the value of attribute apex_code_highlight_theme.



6
7
8
# File 'lib/twin/config.rb', line 6

def apex_code_highlight_theme
  @apex_code_highlight_theme
end

#apex_themeObject

Returns the value of attribute apex_theme.



6
7
8
# File 'lib/twin/config.rb', line 6

def apex_theme
  @apex_theme
end

#apex_widthObject

Returns the value of attribute apex_width.



6
7
8
# File 'lib/twin/config.rb', line 6

def apex_width
  @apex_width
end

#global_excludesObject

Returns the value of attribute global_excludes.



6
7
8
# File 'lib/twin/config.rb', line 6

def global_excludes
  @global_excludes
end

#sync_dirObject

Returns the value of attribute sync_dir.



6
7
8
# File 'lib/twin/config.rb', line 6

def sync_dir
  @sync_dir
end

Class Method Details

.loadObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/twin/config.rb', line 28

def self.load
  path = ENV["TWIN_CONFIG"] || File.join(Dir.home, ".config", "twin", "config.yaml")
  data = {}
  if File.exist?(path)
    begin
      data = YAML.safe_load_file(path) || {}
    rescue Psych::SyntaxError => e
      raise "config syntax error in #{path}: #{e.message}"
    end
  end
  new(data)
end

Instance Method Details

#validate!Object



41
42
43
44
# File 'lib/twin/config.rb', line 41

def validate!
  raise "sync_dir not set (add to ~/.config/twin/config.yaml or set TWIN_SYNC_DIR)" if sync_dir.empty?
  raise "sync_dir not found: #{sync_dir}" unless Dir.exist?(sync_dir)
end