Class: App::Config::JSON

Inherits:
Hash
  • Object
show all
Includes:
Base
Defined in:
lib/app/config/json.rb

Constant Summary collapse

DEFAULT_MASK =
"*.json"
DEFAULT_SUFFIX =
".json"
DEFAULT_ENCODE =
{ allow_nan: true }
DEFAULT_DECODE =
{ allow_nan: true }

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ JSON

Returns a new instance of JSON.



20
21
22
23
# File 'lib/app/config/json.rb', line 20

def initialize( **opts )
  super()
  reload( **opts )
end

Instance Method Details

#load(section) ⇒ Object

load configuration.

Raises:

  • (ArgumentError)


43
44
45
46
47
48
# File 'lib/app/config/json.rb', line 43

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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/app/config/json.rb', line 51

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 }" )
          json  =  ::ERB.new( text ).result        rescue  raise( ArgumentError, "could not parse by ERB: #{ pathname }" )
          hash  =  ::JSON.load( json, **@decode )
          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.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/app/config/json.rb', line 26

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

remove and load configuration.

Raises:

  • (ArgumentError)


80
81
82
83
84
85
86
# File 'lib/app/config/json.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) ⇒ Object

save configuration.

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
# File 'lib/app/config/json.rb', line 70

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( ::JSON.pretty_generate( hash, **@encode ) )
  end
end

#savepathname(section) ⇒ Object



88
89
90
# File 'lib/app/config/json.rb', line 88

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