Class: Beaker::Options::OptionsHash

Inherits:
Hash
  • Object
show all
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

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

Examples:

Use this method to return the value for a given key

a['key'] = 'value'
a['key'] == a[:key] == 'value'

Parameters:

  • k (Object)

    The key to find, searches for both k as String and k as Symbol

Returns:

  • (nil, Object)

    Return the Object found at given key, or nil if no Object found



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

Examples:

Use this method to set the value for a key

a['key'] = 'value'

Parameters:

  • k (Object)

    The key to associated with the value, converted to Symbol key

  • v (Object)

    The value to store in the ObjectHash

Returns:

  • (Object)

    Return the Object value just stored



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



181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/beaker/options/options_hash.rb', line 181

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

Examples:

Use this method to set the value for a key

a['key'] = 'value'
a.delete[:key] == 'value'

Parameters:

  • k (Object)

    The key to delete in ObjectHash,

Returns:

  • (Object, nil)

    The Object deleted at value,



101
102
103
# File 'lib/beaker/options/options_hash.rb', line 101

def delete k
  super(k.to_s) || super(k.to_sym)
end

#dumpString

Pretty print the options as JSON

Examples:

base = { :key => { :subkey1 => 'subval', :subkey2 => 'subval' } }
base.dump
#=>  '{
          "key": {
              "subkey1": "subval",
              "subkey2": 2
          }
      }

Returns:

  • (String)

    The description of self



331
332
333
# File 'lib/beaker/options/options_hash.rb', line 331

def dump
  fmt_collection( self, 0, DIV )
end

#fmt_assoc(coll, in_lvl = 0, in_inc = DIV) ⇒ String

Pretty prints an associative collection

Examples:

base = { :key => 'value', :key2 => 'value' }
self.fmt_assoc( base )
#=> '{
         "key": "value",
         "key2": "value"
     }'

Parameters:

  • coll (#each_pair)

    The collection to be printed

  • in_lvl (Integer) (defaults to: 0)

    The level of indentation

  • in_inc (String) (defaults to: DIV)

    The increment to indent

Returns:

  • (String)

    The collection as a pretty JSON object



238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/beaker/options/options_hash.rb', line 238

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

Examples:

self.fmt_value( 4 )
#=> '4'
self.fmt_value( true )
#=> 'true'
self.fmt_value( nil )
#=> 'null'
self.fmt_value( 'string' )
#=> '"string"'

Parameters:

  • value (Object)

    The collection to be printed

Returns:

  • (String)

    The value as a valid JSON primitive



310
311
312
313
314
315
316
# File 'lib/beaker/options/options_hash.rb', line 310

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

Examples:

base = {:key => { :subkey1 => 'subval', :subkey2 => ['subval'] }}
self.fmt_collection( base )
#=> '{
         "key": {
             "subkey": "subval",
             "subkey2": [
                 "subval"
             ]
         }
     }'

Parameters:

  • collection (Enumerable)

    The collection to be printed

  • in_lvl (Integer) (defaults to: 0)

    The level of indentation

  • in_inc (String) (defaults to: DIV)

    The increment to indent

Returns:

  • (String)

    The collection as a pretty JSON object



213
214
215
216
217
218
219
220
221
# File 'lib/beaker/options/options_hash.rb', line 213

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

Examples:

base = [ 'first', 'second' ]
self.fmt_list( base )
#=> '[
         "first",
         "second"
     ]'

Parameters:

  • coll (#each)

    The collection to be printed

  • in_lvl (Integer) (defaults to: 0)

    The level of indentation

  • in_inc (String) (defaults to: DIV)

    The increment to indent

Returns:

  • (String)

    The collection as a pretty JSON object



266
267
268
269
270
271
272
273
274
275
276
# File 'lib/beaker/options/options_hash.rb', line 266

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



281
282
283
284
285
286
287
# File 'lib/beaker/options/options_hash.rb', line 281

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

#get_typeSymbol

Determine the puppet type of the ObjectHash

Default is FOSS

Examples:

Use this method to test if the :type setting is pe

a['type'] = 'pe'
a.get_type == :pe

Returns:

  • (Symbol)

    the type given in the options



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/beaker/options/options_hash.rb', line 63

def get_type
  case self[:type]
  when /pe/
    :pe
  when /foss/
    :foss
  when /aio/
    :aio
  else
    :foss
  end
end

#has_key?(k) ⇒ Boolean

Determine if key is stored in ObjectHash

Examples:

Use this method to set the value for a key

a['key'] = 'value'
a.has_key[:key] == true

Parameters:

  • k (Object)

    The key to find in ObjectHash, searches for both k as String and k as Symbol

Returns:

  • (Boolean)


85
86
87
# File 'lib/beaker/options/options_hash.rb', line 85

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

Examples:

Use this method to test if the :type setting is pe

a['type'] = 'pe'
a.is_pe? == true

Returns:

  • (Boolean)


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

Create new OptionsHash from recursively merged self with an OptionsHash or Hash

Examples:

base = { :key => { :subkey1 => 'subval', :subkey2 => 'subval' } }
hash = { :key => { :subkey1 => 'newval'} }

base.merge(hash)
#=> {:key =>
      {:subkey1 => 'newval',
       :subkey2 => 'subval' }

Parameters:

Returns:



148
149
150
151
152
# File 'lib/beaker/options/options_hash.rb', line 148

def merge hash
  #make a deep copy into an empty hash object
  merged_hash = rmerge(OptionsHash.new, self)
  rmerge(merged_hash, hash)
end

#merge!(hash) ⇒ OptionsHash

Recursively merge self with an OptionsHash or Hash

Examples:

base = { :key => { :subkey1 => 'subval', :subkey2 => 'subval' } }
hash = { :key => { :subkey1 => 'newval'} }

base.merge!(hash)
#=> {:key =>
      {:subkey1 => 'newval',
       :subkey2 => 'subval' }

Parameters:

Returns:



168
169
170
# File 'lib/beaker/options/options_hash.rb', line 168

def merge! hash
  rmerge(self, hash)
end

#rmerge(base, hash) ⇒ OptionsHash

Recursively merge and OptionsHash with an OptionsHash or Hash

Examples:

base = { :key => { :subkey1 => 'subval', :subkey2 => 'subval' } }
hash = { :key => { :subkey1 => 'newval'} }

rmerge(base, hash)
#=>  {:key =>
        {:subkey1 => 'newval',
         :subkey2 => 'subval'}}

Parameters:

Returns:



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/beaker/options/options_hash.rb', line 120

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) || hash[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