Class: App::Config::RUBY

Inherits:
Hash
  • Object
show all
Defined in:
lib/app/config/ruby.rb

Constant Summary collapse

DEFAULT_MASK =
"*.rb"
DEFAULT_SUFFIX =
".rb"

Instance Method Summary collapse

Methods inherited from Hash

#deeply_dup, #deeply_map, #deeply_map!, #deeply_merge, #deeply_merge!, #deeply_stringify_keys, #deeply_stringify_keys!, #deeply_symbolize_keys, #deeply_symbolize_keys!

Constructor Details

#initialize(**opts) ⇒ RUBY

Returns a new instance of RUBY.



11
12
13
14
15
16
17
# File 'lib/app/config/ruby.rb', line 11

def initialize( **opts )
  super
  self.default_proc  =  proc do |hash, key|
    hash[key]  =  {}
  end
  reload( **opts )
end

Instance Method Details

#load(section) ⇒ Object

load configuration.

Raises:

  • (ArgumentError)


37
38
39
40
41
42
# File 'lib/app/config/ruby.rb', line 37

def load( section )
  raise  ArgumentError, "'#{ section }' is not String."    unless  String === section

  self[section].clear
  load_overlay( section + DEFAULT_SUFFIX )
end

#load_overlay(filename) ⇒ Object

load configuration.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/app/config/ruby.rb', line 45

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

#reload(root: nil, path: nil, encode: nil, decode: nil) ⇒ Object

bulk load configuration in search paths.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/app/config/ruby.rb', line 20

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  =  {}.merge( encode || {} )
  @decode  =  {}.merge( decode || {} )

  load_overlay( DEFAULT_MASK )
end

#reset(section) ⇒ Object

remove and load configuration.

Raises:

  • (ArgumentError)


73
74
75
76
77
78
79
# File 'lib/app/config/ruby.rb', line 73

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) ⇒ Object

save configuration.

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
70
# File 'lib/app/config/ruby.rb', line 63

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( hash.pretty_inspect )
  end
end

#savepathname(section) ⇒ Object



81
82
83
# File 'lib/app/config/ruby.rb', line 81

def savepathname( section )
  ::File.join( @vardir, section + DEFAULT_SUFFIX )
end