Module: Reap::Extensions::Hash

Included in:
Hash
Defined in:
lib/reap/extensions/hash.rb

Instance Method Summary collapse

Instance Method Details

#argumentize(args_field = nil) ⇒ Object Also known as: command_vector

Turn a hash into arguments.

h = { :list => [1,2], :base => "HI" }
h.argumentize #=> [ [], { :list => [1,2], :base => "HI" } ]
h.argumentize(:list) #=> [ [1,2], { :base => "HI" } ]


37
38
39
40
41
42
43
44
45
46
# File 'lib/reap/extensions/hash.rb', line 37

def argumentize(args_field=nil)
  config = dup
  if args_field
    args = [config.delete(args_field)].flatten.compact
  else
    args = []
  end
  args << config
  return args
end

#to_consoleObject

Convert an array into command line parameters. The array is accepted in the format of Ruby method arguments –ie. [arg1, arg2, …, hash]



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/reap/extensions/hash.rb', line 14

def to_console
  flags = collect do |f,v|
    m = f.to_s.size == 1 ? '-' : '--'
    case v
    when Array
      v.collect{ |e| "#{m}#{f}='#{e}'" }.join(' ')
    when true
      "#{m}#{f}"
    when false, nil
      ''
    else
      "#{m}#{f}='#{v}'"
    end
  end
  flags.join(" ")
end

#to_ostructObject



52
53
54
# File 'lib/reap/extensions/hash.rb', line 52

def to_ostruct
  OpenStruct.new(self)
end