Class: Beaker::Options::OptionsHash
- Inherits:
-
Hash
- Object
- Hash
- Beaker::Options::OptionsHash
- Defined in:
- lib/beaker/options/options_hash.rb
Overview
A hash that treats Symbol and String keys interchangeably and recursively merges hashes
Constant Summary collapse
- DIV =
The dividor between elements when OptionsHash is dumped
' '
- EOL =
The end of line when dumping
"\n"
Instance Method Summary collapse
-
#[](k) ⇒ nil, Object
Get value for given key, search for both k as String and k as Symbol, if not present return nil.
-
#[]=(k, v) ⇒ Object
Set Symbol key to Object value.
-
#as_coll(opening, closing, in_lvl, in_inc, &block) ⇒ Object
Helper for formatting collections Computes the indentation level for elements of the collection Yields indentation to block to so the caller can create map of element strings Places delimiters in the correct location Joins everything with correct EOL.
-
#delete(k) ⇒ Object?
Determine key=>value entry in OptionsHash, remove both value at String key and value at Symbol key.
-
#dump ⇒ String
Pretty print the options as JSON.
-
#fmt_assoc(coll, in_lvl = 0, in_inc = DIV) ⇒ String
Pretty prints an associative collection.
-
#fmt_basic(value) ⇒ String
Pretty prints primitive JSON values.
-
#fmt_collection(collection, in_lvl = 0, in_inc = DIV) ⇒ String
Pretty prints a collection.
-
#fmt_list(coll, in_lvl = 0, in_inc = DIV) ⇒ String
Pretty prints a list collection.
-
#fmt_value(value, in_lvl = 0, in_inc = DIV) ⇒ Object
Chooses between collection and primitive formatting.
-
#has_key?(k) ⇒ Boolean
Determine if key is stored in ObjectHash.
-
#is_pe? ⇒ Boolean
Determine if type of ObjectHash is pe, defaults to true.
-
#merge(hash) ⇒ OptionsHash
Recursively merge self with an OptionsHash or Hash.
-
#rmerge(base, hash) ⇒ OptionsHash
Recursively merge and OptionsHash with an OptionsHash or Hash.
Instance Method Details
#[](k) ⇒ nil, Object
Get value for given key, search for both k as String and k as Symbol, if not present return nil
26 27 28 |
# File 'lib/beaker/options/options_hash.rb', line 26 def [] k super(k.to_s) || super(k.to_sym) end |
#[]=(k, v) ⇒ Object
Set Symbol key to Object value
39 40 41 |
# File 'lib/beaker/options/options_hash.rb', line 39 def []=k,v super(k.to_sym, v) end |
#as_coll(opening, closing, in_lvl, in_inc, &block) ⇒ Object
Helper for formatting collections Computes the indentation level for elements of the collection Yields indentation to block to so the caller can create map of element strings Places delimiters in the correct location Joins everything with correct EOL
!@visibility private
139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/beaker/options/options_hash.rb', line 139 def as_coll( opening, closing, in_lvl, in_inc, &block ) delim_indent = in_inc * in_lvl elem_indent = in_inc * (in_lvl + 1) open_brace = opening close_brace = delim_indent + closing fmtd_coll = block.call( elem_indent ) str_coll = fmtd_coll.join( ',' + EOL ) return open_brace + EOL + str_coll + EOL + close_brace end |
#delete(k) ⇒ Object?
Determine key=>value entry in OptionsHash, remove both value at String key and value at Symbol key
deletes both k as String and k as Symbol
nil if no Object deleted
79 80 81 |
# File 'lib/beaker/options/options_hash.rb', line 79 def delete k super(k.to_s) || super(k.to_sym) end |
#dump ⇒ String
Pretty print the options as JSON
289 290 291 |
# File 'lib/beaker/options/options_hash.rb', line 289 def dump fmt_collection( self, 0, DIV ) end |
#fmt_assoc(coll, in_lvl = 0, in_inc = DIV) ⇒ String
Pretty prints an associative collection
196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/beaker/options/options_hash.rb', line 196 def fmt_assoc( coll, in_lvl = 0, in_inc = DIV ) if coll.empty? return '{}' else as_coll '{', '}', in_lvl, in_inc do |elem_indent| coll.map do |key, value| assoc_line = elem_indent + '"' + key.to_s + '"' + ': ' assoc_line += fmt_value( value, in_lvl, in_inc ) end end end end |
#fmt_basic(value) ⇒ String
Pretty prints primitive JSON values
268 269 270 271 272 273 274 |
# File 'lib/beaker/options/options_hash.rb', line 268 def fmt_basic( value ) case value when Numeric, TrueClass, FalseClass then value.to_s when NilClass then "null" else "\"#{value}\"" end end |
#fmt_collection(collection, in_lvl = 0, in_inc = DIV) ⇒ String
Pretty prints a collection
171 172 173 174 175 176 177 178 179 |
# File 'lib/beaker/options/options_hash.rb', line 171 def fmt_collection( collection, in_lvl = 0, in_inc = DIV ) if collection.respond_to? :each_pair string = fmt_assoc( collection, in_lvl, in_inc ) else string = fmt_list( collection, in_lvl, in_inc ) end return string end |
#fmt_list(coll, in_lvl = 0, in_inc = DIV) ⇒ String
Pretty prints a list collection
224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/beaker/options/options_hash.rb', line 224 def fmt_list( coll, in_lvl = 0, in_inc = DIV ) if coll.empty? return '[]' else as_coll '[', ']', in_lvl, in_inc do |indent| coll.map do |el| indent + fmt_value( el, in_lvl, in_inc ) end end end end |
#fmt_value(value, in_lvl = 0, in_inc = DIV) ⇒ Object
Chooses between collection and primitive formatting
!@visibility private
239 240 241 242 243 244 245 |
# File 'lib/beaker/options/options_hash.rb', line 239 def fmt_value( value, in_lvl = 0, in_inc = DIV ) if value.kind_of? Enumerable and not value.is_a? String fmt_collection( value, in_lvl + 1, in_inc ) else fmt_basic( value ) end end |
#has_key?(k) ⇒ Boolean
Determine if key is stored in ObjectHash
63 64 65 |
# File 'lib/beaker/options/options_hash.rb', line 63 def has_key? k super(k.to_s) || super(k.to_sym) end |
#is_pe? ⇒ Boolean
Determine if type of ObjectHash is pe, defaults to true
50 51 52 |
# File 'lib/beaker/options/options_hash.rb', line 50 def is_pe? self[:type] ? self[:type] =~ /pe/ : true end |
#merge(hash) ⇒ OptionsHash
Recursively merge self with an OptionsHash or Hash
126 127 128 |
# File 'lib/beaker/options/options_hash.rb', line 126 def merge hash rmerge(self, hash) end |
#rmerge(base, hash) ⇒ OptionsHash
Recursively merge and OptionsHash with an OptionsHash or Hash
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/beaker/options/options_hash.rb', line 98 def rmerge base, hash return base unless hash.is_a?(Hash) || hash.is_a?(OptionsHash) hash.each do |key, v| if (base[key].is_a?(Hash) || base[key].is_a?(OptionsHash)) && (hash[key].is_a?(Hash) || has[key].is_a?(OptionsHash)) rmerge(base[key], hash[key]) elsif hash[key].is_a?(Hash) base[key] = OptionsHash.new.merge(hash[key]) else base[key]= hash[key] end end base end |