Class: Epi::ConfigurationFile

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Exceptions
Defined in:
lib/epi/configuration_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ConfigurationFile

Returns a new instance of ConfigurationFile.



14
15
16
17
# File 'lib/epi/configuration_file.rb', line 14

def initialize(path)
  @job_descriptions = {}
  @path = Pathname path
end

Instance Attribute Details

#job_descriptionsObject (readonly)

Returns the value of attribute job_descriptions.



10
11
12
# File 'lib/epi/configuration_file.rb', line 10

def job_descriptions
  @job_descriptions
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/epi/configuration_file.rb', line 10

def path
  @path
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/epi/configuration_file.rb', line 35

def changed?
  Digest::MD5.digest(binread) != @last_digest
end

#job(id_and_name, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/epi/configuration_file.rb', line 39

def job(id_and_name, &block)
  raise InvalidConfigurationFile, 'Improper use of "job"' unless
      Hash === id_and_name &&
      id_and_name.count == 1 &&
      Symbol === id_and_name.keys.first &&
      String === id_and_name.values.first &&
      block.respond_to?(:call) &&
      block.respond_to?(:arity) &&
      block.arity >= 1
  id, name = id_and_name.first
  id = id.to_s
  job_description = @job_descriptions[id] ||= JobDescription.new(id)
  job_description.name = name
  job_description.reconfigure &block
end

#loggerObject



19
20
21
# File 'lib/epi/configuration_file.rb', line 19

def logger
  Epi.logger
end

#readObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/epi/configuration_file.rb', line 23

def read
  return unless exist? && changed?
  logger.info "Reading configuration file #{path}"
  data = binread
  begin
    instance_eval data, path.to_s
    @last_digest = Digest::MD5.digest(data)
  rescue => error
    raise InvalidConfigurationFile.new("Unhandled exception of type #{error.class.name}", error)
  end
end