Class: Serenity::Configuration

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Configuration

Returns a new instance of Configuration.



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

def initialize(filename)
  @filename = filename
  @options = YAML.load_file(filename)
end

Instance Attribute Details

#filenameObject

Returns the value of attribute filename.



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

def filename
  @filename
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#exists?(*args) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/serenity.rb', line 35

def exists?(*args)
  c = options

  missing_option_index = 0

  args.each_with_index do |arg, i|
    if c.is_a?(Hash) && c.has_key?(arg)
      c = c[arg]
    else
      missing_option_index = i
      c = nil
    end
  end

  c      
end

#get(*args) ⇒ Object



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/serenity.rb', line 52

def get(*args)
  c = options

  missing_option_index = 0

  args.each_with_index do |arg, i|
    if c.is_a?(Hash) && c.has_key?(arg)
      c = c[arg]
    else
      missing_option_index = i
      raise "Option not found."
    end
  end

  c
rescue 
  $stdout.puts "The following option was not found in #{filename}:"
  (0..missing_option_index).each do |i|
    $stdout.puts args[i]
  end
  $stdout.puts
  $stdout.puts "Are you sure #{filename} is up to date?"
  $stdout.puts
  raise "Option not found."
end

#to_hashObject



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

def to_hash
  options
end