Sha256: 68069f5cff34cee986d8ca3460b86c88ceb1e777ad0f5fc9e0c70d100c3f367d

Contents?: true

Size: 735 Bytes

Versions: 2

Compression:

Stored size: 735 Bytes

Contents

# frozen_string_literal: true

module Reviewer
  class Tool
    # Assembles tool flag settings into a single string or array
    class Flags
      attr_reader :flag_pairs

      def initialize(flag_pairs)
        @flag_pairs = flag_pairs
      end

      def to_s
        to_a.join(' ')
      end

      def to_a
        flags = []
        flag_pairs.each { |key, value| flags << flag(key, value) }
        flags
      end

      private

      def flag(key, value)
        dash = key.to_s.size == 1 ? '-' : '--'

        value = needs_quotes?(value) ? "'#{value}'" : value

        "#{dash}#{key} #{value}".strip
      end

      def needs_quotes?(value)
        value.is_a?(String) && value.include?(' ')
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reviewer-0.1.3 lib/reviewer/tool/flags.rb
reviewer-0.1.2 lib/reviewer/tool/flags.rb