Class: Fare::LoadConfigurationFile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ LoadConfigurationFile

Returns a new instance of LoadConfigurationFile.



6
7
8
9
10
# File 'lib/fare/load_configuration_file.rb', line 6

def initialize(options)
  @filename = options.fetch(:filename) { Fare.default_config_file }
  @environment = options.fetch(:environment) { Fare.environment }
  verify_file!
end

Instance Attribute Details

#environmentObject (readonly)

Returns the value of attribute environment.



4
5
6
# File 'lib/fare/load_configuration_file.rb', line 4

def environment
  @environment
end

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/fare/load_configuration_file.rb', line 4

def filename
  @filename
end

Instance Method Details

#add_lib_dir_to_load_pathObject



45
46
47
48
49
50
# File 'lib/fare/load_configuration_file.rb', line 45

def add_lib_dir_to_load_path
  lib_dir = File.expand_path("../../lib", filename)
  if File.directory?(lib_dir) && !$LOAD_PATH.include?(lib_dir)
    $LOAD_PATH.unshift(lib_dir)
  end
end

#checksumObject



37
38
39
# File 'lib/fare/load_configuration_file.rb', line 37

def checksum
  Digest::MD5.hexdigest(code)
end

#codeObject



29
30
31
# File 'lib/fare/load_configuration_file.rb', line 29

def code
  @code ||= File.open(filename.to_s, "r:utf-8").read
end

#configurationObject



18
19
20
# File 'lib/fare/load_configuration_file.rb', line 18

def configuration
  @configuration ||= load_configuration
end

#exists?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/fare/load_configuration_file.rb', line 41

def exists?
  File.exist?(filename)
end

#load_configurationObject



22
23
24
25
26
27
# File 'lib/fare/load_configuration_file.rb', line 22

def load_configuration
  add_lib_dir_to_load_path
  dsl = ConfigurationDSL.new
  dsl.instance_eval(code, filename)
  dsl.configuration
end

#lock_filenameObject



33
34
35
# File 'lib/fare/load_configuration_file.rb', line 33

def lock_filename
  File.expand_path("../fare.#{environment}.lock", filename)
end

#verify_file!Object



12
13
14
15
16
# File 'lib/fare/load_configuration_file.rb', line 12

def verify_file!
  unless exists?
    raise ConfigurationNotFoundError, "Configuration file doesn't exist at #{filename}"
  end
end