Class: ThreeScale::Backend::Configuration::Loader

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/3scale/backend/configuration/loader.rb

Constant Summary collapse

Error =
Class.new StandardError
NoConfigFiles =
Class.new Error

Instance Method Summary collapse

Instance Method Details

#add_section(name, *fields) ⇒ Object

Add configuration section with the given fields.

Example

# Define section like this
loader = Loader.new
loader.add_section(:bacons, :type, :amount)

# Configure default values like this
loader.bacons.type   = :chunky
loader.bacons.amount = 'a lot'

# Load the configuration from an array of files
loader.load!(files)

# Use like this
loader.bacons.type # :chunky


28
29
30
# File 'lib/3scale/backend/configuration/loader.rb', line 28

def add_section(name, *fields)
  send("#{name}=", Struct.new(*fields).new)
end

#load!(files) ⇒ Object

Load configuration from a set of files

Raises:



33
34
35
36
37
38
# File 'lib/3scale/backend/configuration/loader.rb', line 33

def load!(files)
  raise NoConfigFiles if !files || files.empty?
  files.each do |path|
    load path if File.readable?(File.expand_path(path))
  end
end