Class: App::Config::YAML
- Inherits:
-
Hash
- Object
- Hash
- App::Config::YAML
- Includes:
- Base
- Defined in:
- lib/app/config/yaml.rb
Constant Summary collapse
- DEFAULT_MASK =
"*.{yml,yaml}"
- DEFAULT_SUFFIX =
".yaml"
- DEFAULT_ENCODE =
{}
- DEFAULT_DECODE =
{}
Instance Method Summary collapse
-
#initialize(**opts) ⇒ YAML
constructor
A new instance of YAML.
-
#load(section) ⇒ Object
load configuration.
-
#load_overlay(filename) ⇒ Object
load configuration.
- #pretty_text(hash) ⇒ Object
-
#reload(root: nil, path: nil, encode: nil, decode: nil) ⇒ Object
bulk load configuration in search paths.
-
#reset(section) ⇒ Object
remove and load configuration.
-
#save(section) ⇒ Object
save configuration.
- #savepathname(section) ⇒ Object
Constructor Details
#initialize(**opts) ⇒ YAML
Returns a new instance of YAML.
18 19 20 21 |
# File 'lib/app/config/yaml.rb', line 18 def initialize( **opts ) super() reload( **opts ) end |
Instance Method Details
#load(section) ⇒ Object
load configuration.
41 42 43 44 45 46 |
# File 'lib/app/config/yaml.rb', line 41 def load( section ) raise ArgumentError, "'#{ section }' is not String." unless String === section self[section].clear ( section + DEFAULT_SUFFIX ) end |
#load_overlay(filename) ⇒ Object
load configuration.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/app/config/yaml.rb', line 49 def ( filename ) @paths.each do |path| begin dirname = ::File.( path ) if ::Dir.exist?( dirname ) ::Dir.glob( "#{ dirname }/#{ filename }" ) do |pathname| text = ::File.open( pathname ).read rescue raise( ArgumentError, "could not load #{ pathname }" ) yaml = ::ERB.new( text ).result rescue raise( ArgumentError, "could not parse by ERB: #{ pathname }" ) hash = ::YAML.load( yaml, **@decode ) self.deeply_merge!( hash ) end end rescue => e STDERR.puts e. end end end |
#pretty_text(hash) ⇒ Object
77 78 79 |
# File 'lib/app/config/yaml.rb', line 77 def pretty_text( hash ) ::YAML.dump( hash, **@encode ) end |
#reload(root: nil, path: nil, encode: nil, decode: nil) ⇒ Object
bulk load configuration in search paths.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/app/config/yaml.rb', line 24 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 || {} ) ( DEFAULT_MASK ) end |
#reset(section) ⇒ Object
remove and load configuration.
82 83 84 85 86 87 88 |
# File 'lib/app/config/yaml.rb', line 82 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.
68 69 70 71 72 73 74 75 |
# File 'lib/app/config/yaml.rb', line 68 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) ⇒ Object
90 91 92 |
# File 'lib/app/config/yaml.rb', line 90 def savepathname( section ) ::File.join( @vardir, section + DEFAULT_SUFFIX ) end |