Class: Hash

Inherits:
Object show all
Defined in:
lib/detroit/core_ext/to_console.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" } ]


84
85
86
87
88
89
90
91
92
93
# File 'lib/detroit/core_ext/to_console.rb', line 84

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_argvObject

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/detroit/core_ext/to_console.rb', line 60

def to_argv
  flags = []
  each do |f,v|
    m = f.to_s.size == 1 ? '-' : '--'
    case v
    when Array
      v.each{ |e| flags << "#{m}#{f}='#{e}'" }
    when true
      flags << "#{m}#{f}"
    when false, nil
      # nothing
    else
      flags << "#{m}#{f}='#{v}'"
    end
  end
  flags
end

#to_consoleObject

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



53
54
55
# File 'lib/detroit/core_ext/to_console.rb', line 53

def to_console
  to_argv.join(' ')
end