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.11.18 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.11.17 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.11.16 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.11.15 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.11.14 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.11.12 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.11.11 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.11.9 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.11.8 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.11.7 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.11.6 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.11.5 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.11.4 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.11.3 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.11.2 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.11.1 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.11.0 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.10.9 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.10.8 lib/fedux_org_stdlib/core_ext/hash/options.rb
fedux_org-stdlib-0.10.7 lib/fedux_org_stdlib/core_ext/hash/options.rb