Class: Hash

Inherits:
Object
  • Object
show all
Includes:
ForceArray
Defined in:
lib/liquidoc.rb

Instance Method Summary collapse

Methods included from ForceArray

#force_array, #force_array!

Instance Method Details

#argify(options = {}) ⇒ Object



1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
# File 'lib/liquidoc.rb', line 1358

def argify options = {}
  # Converts a hash of key-value pairs to command-line option/argument listings
  # Can be called with optional arguments:
  # template :: Liquid-formatted parsing template string
  #          Accepts:
  #
  #            'hyph'            :: -<key> <value>
  #            'hyphhyph'        :: --<key> <value> (default)
  #            'hyphchar'        :: -<k> <value>
  #            'dump'            :: <key> <value>
  #            'paramequal'      :: <key>=<value>
  #            'valonly'         :: <value>
  # delim    :: Delimiter -- any ASCII characters that separate the arguments
  #
  # For template-based usage, express the variables:
  #    opt (the keyname) as {{opt}}
  #    arg (the value) as {{arg}}
  # EXAMPLES (my_hash = {"key1"=>"val1", "key2"=>"val2"})
  # my_hash.argify                      #=> key1 val1 key2 val2
  # my_hash.argify('hyphhyph')          #=> --key1 val1 --key2 val2
  # my_hash.argify('paramequal')        #=> key1=val1 key2=val2
  # my_hash.argify('-a {{opt}}={{arg}}')#=> -a key1=val1 -a key2=val2
  # my_hash.argify('valonly', '||')     #=> val1||val2
  # my_hash.argify("{{opt}} `{{arg}}`") #=> key1 `val1` key2 `val2`
  raise "InvalidObject" unless self.is_a? Hash
  template = options.fetch(:template, 'hyphhyph')
  if template.contains_liquid?
    tp = template # use the passed Liquid template
  else
    case template # use a preset Liquid template by name
    when "dump"
      tp = "{{opt}} {{arg | quote_wrap: 'single', '\s|,' }}"
    when "hyph"
      tp = "-{{opt}} {{arg | quote_wrap: 'single', '\s|,' }}"
    when "hyphhyph"
      tp = "--{{opt}} {{arg | quote_wrap: 'single', '\s|,' }}"
    when "paramequal"
      tp = "{{opt}}={{arg | quote_wrap: 'single', '\s|,' }}"
    when "valonly"
      tp = "{{arg | quote_wrap: 'single', '\s|,' }}"
    else
      return "Liquid: Unrecognized argify template name: #{template}"
    end
  end
  begin
    tpl = Liquid::Template.parse(tp)
    first = true
    out = ''
    self.each do |k,v|
      # establish datasource
      v = "<Object>" if v.is_a? Hash
      v = v.join(',') if v.is_a? Array
      input = {"opt" => k.to_s, "arg" => v.to_s }
      if first
        dlm = ""
        first = false
      else
        dlm = options.fetch(:delim, ' ')
      end
      out += dlm + tpl.render(input)
    end
  rescue
    raise "Argify template processing failed"
  end
  return out
end

#to_array(op = nil) ⇒ Object



1348
1349
1350
1351
1352
1353
1354
1355
1356
# File 'lib/liquidoc.rb', line 1348

def to_array op=nil
  # Converts a hash of key-value pairs to a flat array based on the first tier
  out = []
  self.each do |k,v|
    v = "<RemovedObject>" if v.is_a? Enumerable and op == "flatten"
    out << {k => v}
  end
  return out
end