Class: Scide::Config

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

Overview

Complete scide configuration as an object graph.

Constant Summary collapse

DEFAULT_CONFIG_FILE =

The file from which the configuration is normally loaded. This defaults to $HOME/.scide/config.yml.

File.join File.expand_path('~'), '.scide', 'config.yml'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ Config

Returns an empty configuration.

Arguments

  • file - The file from which to load the configuration. If not given, this defaults to DEFAULT_CONFIG_FILE.



30
31
32
# File 'lib/scide/config.rb', line 30

def initialize file = nil
  @file = file.try(:to_s) || DEFAULT_CONFIG_FILE
end

Instance Attribute Details

#fileObject

The file from which this configuration will be loaded.



13
14
15
# File 'lib/scide/config.rb', line 13

def file
  @file
end

#globalObject (readonly)

The global configuration. Accessible after calling #load!.



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

def global
  @global
end

#projectsObject (readonly)

The project definitions (windows, option overrides, etc). Accessible after calling #load!.



23
24
25
# File 'lib/scide/config.rb', line 23

def projects
  @projects
end

#screenObject (readonly)

GNU Screen options. Accessible after calling #load!.



16
17
18
# File 'lib/scide/config.rb', line 16

def screen
  @screen
end

Instance Method Details

#load!Object

Loads this configuration. This will read from #file and parse the contents as YAML. Configuration elements can then be retrieved with #global, #projects and #screen.

Errors

  • config_not_found - #file does not exist.

  • config_not_readable - #file cannot be read by the user running scide.

  • malformed_config - #file contains malformed YAML.

  • invalid_config - #file contains invalid configuration (see README).

  • unexpected - #file could not be read.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/scide/config.rb', line 44

def load!
  check_config

  begin
    raw_config = load_config
  rescue StandardError => err
    Scide.fail :unexpected, "ERROR: could not read configuration #{@file}"
  end

  begin
    @config = parse_config raw_config
  rescue SyntaxError, ArgumentError => err
    Scide.fail :malformed_config, "ERROR: could not parse configuration #{@file}\n   #{err}"
  end

  invalid_config 'configuration must be a hash' unless @config.kind_of? Hash

  # laziness
  @config = HashWithIndifferentAccess.new @config

  invalid_config 'screen configuration must be a hash' unless @config[:screen].nil? or @config[:screen].kind_of?(Hash)
  invalid_config 'projects configuration must be a hash' unless @config[:projects].nil? or @config[:projects].kind_of?(Hash)

  begin
    @screen = @config[:screen] || HashWithIndifferentAccess.new
    @global = Scide::Global.new @config[:global]
    @projects = (@config[:projects] || {}).inject(HashWithIndifferentAccess.new) do |memo,obj|
      memo[obj[0]] = Scide::Project.new @global, obj[0], obj[1]; memo
    end
  rescue ArgumentError => err
    invalid_config err
  end
end