Class: Scripted::Configuration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#colorObject

Returns the value of attribute color.



13
14
15
# File 'lib/scripted/configuration.rb', line 13

def color
  @color
end

Instance Method Details

#absolute_config_filesObject



61
62
63
# File 'lib/scripted/configuration.rb', line 61

def absolute_config_files
  config_files.map { |file_name| File.expand_path(file_name) }.uniq
end

#color?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/scripted/configuration.rb', line 35

def color?
  color.nil? ? true : color
end

#config_file(*file_names) ⇒ Object



51
52
53
54
55
# File 'lib/scripted/configuration.rb', line 51

def config_file(*file_names)
  file_names.each do |file_name|
    config_files << file_name
  end
end

#config_filesObject



57
58
59
# File 'lib/scripted/configuration.rb', line 57

def config_files
  @config_files ||= []
end

#evaluate(*args, &block) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/scripted/configuration.rb', line 89

def evaluate(*args, &block)
  instance_eval(*args, &block)
rescue Exception => error
  my_error = ConfigurationSyntaxError.new("#{error.message} (#{error.class})")
  my_error.set_backtrace error.backtrace
  raise my_error
end

#formatter(name, options = {}) ⇒ Object



31
32
33
# File 'lib/scripted/configuration.rb', line 31

def formatter(name, options = {})
  formatters << options.merge(:name => name)
end

#formattersObject



47
48
49
# File 'lib/scripted/configuration.rb', line 47

def formatters
  @formatters ||= []
end

#group(name, &block) ⇒ Object

Defines a group



16
17
18
# File 'lib/scripted/configuration.rb', line 16

def group(name, &block)
  groups[name].define(&block)
end

#groupsObject



65
66
67
# File 'lib/scripted/configuration.rb', line 65

def groups
  @groups ||= Hash.new { |hash, key| hash[key] = Group.new(key) }
end

#load_file(file_name) ⇒ Object



82
83
84
85
86
87
# File 'lib/scripted/configuration.rb', line 82

def load_file(file_name)
  source = File.open(file_name, 'r:utf-8').read
  evaluate source, file_name, 1
rescue Errno::ENOENT => error
  raise_config_file_error(file_name)
end

#load_filesObject



73
74
75
76
77
78
79
80
# File 'lib/scripted/configuration.rb', line 73

def load_files
  absolute_config_files.each do |file_name|
    load_file(file_name)
  end
  if groups.empty?
    raise_config_file_error("scripted.rb")
  end
end

#out(out) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/scripted/configuration.rb', line 39

def out(out)
  if formatters.any?
    formatters.last[:out] = out
  else
    fail NoFormatterForOutput, "You must first specify a formatter to use this option"
  end
end

#parallel(&block) ⇒ Object

Makes a parallel block inside the ‘:default` group.



27
28
29
# File 'lib/scripted/configuration.rb', line 27

def parallel(&block)
  groups[:default].define { parallel(&block) }
end

#raise_config_file_error(file_name) ⇒ Object

Raises:



97
98
99
# File 'lib/scripted/configuration.rb', line 97

def raise_config_file_error(file_name)
  raise ConfigFileNotFound, "No such file -- #{File.expand_path(file_name)}\nEither create a file called 'scripted.rb', or specify another file to load"
end

#run(name, &block) ⇒ Object Also known as: :`

Defines a command in the ‘:default` group.



21
22
23
# File 'lib/scripted/configuration.rb', line 21

def run(name, &block)
  groups[:default].define { run(name, &block) }
end

#with_default_config_file!Object



69
70
71
# File 'lib/scripted/configuration.rb', line 69

def with_default_config_file!
  config_file "scripted.rb" if config_files.empty? && File.exist?("scripted.rb")
end