class App::Config::RUBY

Constants

DEFAULT_DECODE
DEFAULT_ENCODE
DEFAULT_MASK
DEFAULT_SUFFIX

Public Class Methods

new( **opts ) click to toggle source
Calls superclass method App::Config::Base::new
# File lib/app/config/ruby.rb, line 17
def initialize( **opts )
  super()
  reload( **opts )
end

Public Instance Methods

load( section ) click to toggle source

load configuration.

# File lib/app/config/ruby.rb, line 40
def load( section )
  raise  ArgumentError, "'#{ section }' is not String."    unless  String === section

  self[section].clear
  load_overlay( section + DEFAULT_SUFFIX )
end
load_overlay( filename ) click to toggle source

load configuration.

# File lib/app/config/ruby.rb, line 48
def load_overlay( filename )
  @paths.each do |path|
    begin
      dirname  =  ::File.expand_path( path )
      if  ::Dir.exist?( dirname )
        ::Dir.glob( "#{ dirname }/#{ filename }" ) do |pathname|
          text  =  ::File.open( pathname ).read    rescue  raise( ArgumentError, "could not load #{ pathname }" )
          hash  =  eval( text )
          self.deeply_merge!( hash )
        end
      end
    rescue => e
      STDERR.puts e.message
    end
  end
end
pretty_text( hash ) click to toggle source
# File lib/app/config/ruby.rb, line 75
def pretty_text( hash )
  hash.pretty_inspect
end
reload( root: nil, path: nil, encode: nil, decode: nil ) click to toggle source

bulk load configuration in search paths.

# File lib/app/config/ruby.rb, line 23
def reload( root: nil, path: nil, encode: nil, decode: nil )
  clear
  @root  =  root || Dir.pwd
  @paths  =  ( path || DEFAULT_PATH ).gsub('$ROOT', @root).split(':')
  @paths  =  @paths.map{|it| !it.nil? && !it.empty? ? it : nil}.compact
  @paths  <<  "."    if @paths.empty?

  @vardir  =  @paths.last
  Dir.mkdir( @vardir )    unless ::Dir.exist?( @vardir )

  @encode  =  DEFAULT_ENCODE.merge( encode || {} )
  @decode  =  DEFAULT_DECODE.merge( decode || {} )

  load_overlay( DEFAULT_MASK )
end
reset( section ) click to toggle source

remove and load configuration.

# File lib/app/config/ruby.rb, line 80
def reset( section )
  raise  ArgumentError, "'#{ section }' is not String."    unless  String === section

  pathname  =  savepathname(section)
  ::FileUtils.remove( pathname )    if ::File.exist?( pathname )
  load( section )
end
save( section ) click to toggle source

save configuration.

# File lib/app/config/ruby.rb, line 66
def save( section )
  raise  ArgumentError, "'#{ section }' is not String."    unless  String === section
  pathname  =  savepathname(section)
  hash  =  { section => self[section] }.deeply_stringify_keys
  ::File.open( pathname, "w" ) do |file|
    file.puts( pretty_text( hash ) )
  end
end
savepathname( section ) click to toggle source
# File lib/app/config/ruby.rb, line 88
def savepathname( section )
  ::File.join( @vardir, section + DEFAULT_SUFFIX )
end