Sha256: feda09a4dfdcc424e1ef501459407ee1ac64c440ca609483ab8d7645784ccdc1

Contents?: true

Size: 1.14 KB

Versions: 56

Compression:

Stored size: 1.14 KB

Contents

# encoding: utf-8
require 'shellwords'
require 'fedux_org_stdlib/core_ext/shellwords/clean'

# Hash
class Hash
  # Convert hash to command line options
  #
  # @example Convert true value
  #
  #   hash = {
  #     opt1: true
  #   }
  #
  #   hash.to_options
  #   # => [ '--opt1']
  #
  # @example Convert false value
  #
  #   hash = {
  #     opt1: false
  #   }
  #
  #   hash.to_options
  #   # => [ '--no-opt1']
  #
  # @example Convert other values
  #
  #   hash = {
  #     opt1: 'string'
  #   }
  #
  #   hash.to_options
  #   # => [ '--opt1', 'string']
  #
  # @example Clean keys
  #
  #   hash = {
  #     '$opt1' => 'string'
  #   }
  #
  #   hash.to_options
  #   # => [ '--opt1', 'string']
  #
  # @example Escape values
  #
  #   hash = {
  #     'opt1' => '$string'
  #   }
  #
  #   hash.to_options
  #   # => [ '--opt1', '\$string']
  def to_options
    each_with_object([]) do |(key, value), a|
      key = Shellwords.clean(key)

      if value.is_a? TrueClass
        a << "--#{key}"
      elsif value.is_a? FalseClass
        a << "--no-#{key}"
      else
        a << "--#{key}"
        a << Shellwords.escape(value)
      end
    end
  end
end

Version data entries

56 entries across 56 versions & 1 rubygems

Version Path
fedux_org-stdlib-0.10.6 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.10.5 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.10.4 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.10.3 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.10.2 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.10.1 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.10.0 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.9.8 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.9.7 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.9.6 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.9.5 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.9.4 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.9.2 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.9.1 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.9.0 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.8.11 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.8.10 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.8.9 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.8.8 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.8.7 lib/fedux_org_stdlib/core_ext/hash/options.rb