Class: FlashFlow::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/flash_flow/config.rb

Defined Under Namespace

Classes: AlreadyConfigured, IncompleteConfiguration, NotYetConfigured

Constant Summary collapse

ATTRIBUTES =
[
  :git, :branch_info_file, :log_file, :notifier, :issue_tracker, :lock, :branches, :release, :smtp
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.


24
25
26
# File 'lib/flash_flow/config.rb', line 24

def logger
  @logger
end

Class Method Details

.configurationObject

Raises:

[View source]

26
27
28
29
# File 'lib/flash_flow/config.rb', line 26

def self.configuration
  raise NotYetConfigured unless instance.instance_variable_get(:@configured)
  instance
end

.configure!(config_file) ⇒ Object

Raises:

[View source]

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

def self.configure!(config_file)
  raise AlreadyConfigured if instance.instance_variable_get(:@configured)

  template = ERB.new File.read(config_file)
  yaml = YAML.load template.result(binding)
  config = defaults.merge(symbolize_keys!(yaml))

  missing_attrs = []
  ATTRIBUTES.each do |attr_name|
    missing_attrs << attr_name unless config.has_key?(attr_name)
    instance.instance_variable_set("@#{attr_name}", config[attr_name])
  end

  instance.instance_variable_set(:@logger, get_logger(instance.log_file))

  raise IncompleteConfiguration.new("Missing attributes:\n #{missing_attrs.join("\n ")}") unless missing_attrs.empty?

  instance.instance_variable_set(:@configured, true)
end

.defaultsObject

[View source]

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

def self.defaults
  {
      branch_info_file: 'README.rdoc',
      log_file: 'log/flash_flow.log',
      notifier: nil,
      issue_tracker: nil,
      lock: nil,
      branches: nil,
      release: nil,
      smtp: nil
  }
end

.get_logger(log_file) ⇒ Object

[View source]

51
52
53
54
55
56
57
58
59
60
# File 'lib/flash_flow/config.rb', line 51

def self.get_logger(log_file)
  if log_file.to_s.empty?
    log_file = '/dev/null'
  else
    dir = File.dirname(log_file)
    FileUtils.mkdir_p(dir)
  end
  Logger.new(log_file)

end

.symbolize_keys!(hash) ⇒ Object

[View source]

75
76
77
78
79
80
81
82
83
# File 'lib/flash_flow/config.rb', line 75

def self.symbolize_keys!(hash)
  hash.keys.each do |k|
    unless k.is_a?(Symbol)
      hash[k.to_sym] = hash[k]
      hash.delete(k)
    end
  end
  hash
end