Sha256: afa64917c278ea3e46ab977b64e22109870b33b020042177c54891332ae80b6a

Contents?: true

Size: 1.18 KB

Versions: 8

Compression:

Stored size: 1.18 KB

Contents

require 'ostruct'

module Reap
module Extensions

  #

  module Hash

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

    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

    # 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" } ]
    #
    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

    alias_method :command_vector, :argumentize

    #

    def to_ostruct
      OpenStruct.new(self)
    end

  end

end
end

class Hash #:nodoc:
  include Reap::Extensions::Hash
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
reap-9.2.0 lib/reap/extensions/hash.rb
reap-9.2.1 lib/reap/extensions/hash.rb
reap-9.3.1 lib/reap/extensions/hash.rb
reap-9.3.3 lib/reap/extensions/hash.rb
reap-9.3.4 lib/reap/extensions/hash.rb
reap-9.3.5 lib/reap/extensions/hash.rb
reap-9.4.0 lib/reap/extensions/hash.rb
reap-9.3.0 lib/reap/extensions/hash.rb