Sha256: 658903fcb9161e0375da3e61a2e3a82b23a7e2512f23d0b2ac3a58009ef1a771

Contents?: true

Size: 1.22 KB

Versions: 5

Compression:

Stored size: 1.22 KB

Contents

module Departure
  class Option
    attr_reader :name, :value

    # Builds an instance by parsing its name and value out of the given string.
    # Note the string must conform to "--<arg>=<value>" format.
    #
    # @param string [String]
    # @return [Option]
    def self.from_string(string)
      pair = string.split('=')
      name = pair[0][2..-1]
      value = pair[1]

      new(name, value)
    end

    # Constructor
    #
    # @param name [String]
    # @param optional value [String]
    def initialize(name, value = nil)
      @name = name
      @value = value
    end

    # Compares two options
    #
    # @param [Option]
    # @return [Boolean]
    def ==(another_option)
      name == another_option.name
    end
    alias :eql? :==

    # Returns the option's hash
    #
    # @return [Fixnum]
    def hash
      name.hash
    end

    # Returns the option as string following the "--<name>=<value>" format
    #
    # @return [String]
    def to_s
      "--#{name}#{value_as_string}"
    end

    private

    # Returns the value fragment of the option string if any value is specified
    #
    # @return [String]
    def value_as_string
      if value.nil?
        ''
      else
        "=#{value}"
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
departure-4.0.0 lib/departure/option.rb
departure-2.0.1 lib/departure/option.rb
departure-3.0.1 lib/departure/option.rb
departure-3.0.0 lib/departure/option.rb
departure-2.0.0 lib/departure/option.rb