2 # Copyright (c) 2006-2009 by Craig P Jolicoeur <cpjolicoeur at gmail dot com>
3 # Copyright (C) 2009 Daigo Moriwaki <daigo at debian dot org>
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 # This code was copied from cerberus[1] and modified.
27 # [1] http://rubyforge.org/projects/cerberus
33 def deep_merge!(second)
34 second.each_pair do |k,v|
35 if self[k].is_a?(Hash) && second[k].is_a?(Hash)
36 self[k].deep_merge!(second[k])
44 class HashWithIndifferentAccess < Hash
45 def initialize(constructor = {})
46 if constructor.is_a?(Hash)
55 self[key.to_s] if key.is_a?(Symbol)
58 alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
59 alias_method :regular_update, :update unless method_defined?(:regular_update)
62 regular_writer(convert_key(key), convert_value(value))
65 def update(other_hash)
66 other_hash.each_pair { |key, value| regular_writer(convert_key(key), convert_value(value)) }
70 alias_method :merge!, :update
73 super(convert_key(key))
76 alias_method :include?, :key?
77 alias_method :has_key?, :key?
78 alias_method :member?, :key?
80 def fetch(key, *extras)
81 super(convert_key(key), *extras)
84 def values_at(*indices)
85 indices.collect {|key| self[convert_key(key)]}
89 HashWithIndifferentAccess.new(self)
97 super(convert_key(key))
102 key.kind_of?(Symbol) ? key.to_s : key
104 def convert_value(value)
105 value.is_a?(Hash) ? HashWithIndifferentAccess.new(value) : value
112 FILENAME = 'shogi-server.yaml'
114 def initialize(options = {})
115 @config = HashWithIndifferentAccess.new
117 if options.is_a?(Hash)
118 options[:topdir] ||= $topdir if $topdir
119 options[:topdir] ||= options["topdir"] if options["topdir"]
122 if options[:topdir] && File.exist?(File.join(options[:topdir], FILENAME))
123 merge!(YAML.load(ERB.new(IO.read(File.join(options[:topdir], FILENAME)).result)))
138 def merge!(hash, overwrite = true)
139 return unless hash && !hash.empty?
141 @config.deep_merge!(hash)
143 d = HashWithIndifferentAccess.new(hash)
144 d.deep_merge!(@config)
154 def symbolize_hash(hash)
157 hash[k] = HashWithIndifferentAccess.new(symbolize_hash(v))
162 end # module ShogiServer