Class: Perfer::Configuration

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

Constant Summary collapse

DEFAULTS =
{
  :minimal_time => 1.0,
  :measurements => 10,
  :verbose => false,
}.freeze
PROPERTIES =
DEFAULTS.keys

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/perfer/configuration.rb', line 13

def initialize
  @config_file = DIR/'config.yml'

  DEFAULTS.each_pair { |property, value|
    instance_variable_set(:"@#{property}", value)
  }

  if @config_file.exist? and !@config_file.empty?
    YAML.load_file(@config_file).each_pair { |property, value|
      property = property.to_sym
      if PROPERTIES.include? property
        instance_variable_set(:"@#{property}", value)
      else
        warn "Unknown property in configuration file: #{property}"
      end
    }
  end
end

Instance Method Details

#to_hashObject



36
37
38
39
40
# File 'lib/perfer/configuration.rb', line 36

def to_hash
  PROPERTIES.each_with_object({}) { |property, h|
    h[property] = instance_variable_get(:"@#{property}")
  }
end

#write_defaultsObject



32
33
34
# File 'lib/perfer/configuration.rb', line 32

def write_defaults
  @config_file.write YAML.dump DEFAULTS
end