Sha256: 871b37f4992c5e420661f94b8de0e6e1e5a0f969b1ce02830442ffd759325279

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

module Trav3
  class Options
    def initialize(**args)
      build(**args)
    end

    def opts
      if @opts.empty?
        ''
      else
        "?#{@opts.join('&')}"
      end
    end

    def build(**args)
      @opts ||= []

      args.each do |(key, value)|
        remove(key)
        @opts.push("#{key}=#{value}")
      end

      self
    end

    def remove(key)
      return_value = nil

      @opts = @opts.keep_if do |item, value|
        if entry_match?(key, item)
          return_value = value
          false
        else
          true
        end
      end

      return_value
    end

    def reset!
      @opts = []
    end

    def +(other)
      raise TypeError, "Options type expected, #{other.class} given" unless other.is_a? Options

      @opts += other.instance_variable_get(:@opts)

      self
    end

    def to_s
      opts
    end

    def to_h
      @opts.map { |item| item.split('=') }.to_h
    end

    private

    def entry_match?(entry, item)
      /^#{entry}=/.match? item.to_s
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
trav3-0.2.1 lib/trav3/options.rb