Sha256: 52bec30ee332a12f659de0b4e220f016b6e03225ec1de31d8d41a80f25ab1e14

Contents?: true

Size: 1.59 KB

Versions: 5

Compression:

Stored size: 1.59 KB

Contents

# frozen_string_literal: true

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 fetch(key)
      @opts.each do |item|
        return item if key.to_s == split.call(item).first
      end

      raise KeyError, "key not found #{key}" unless block_given?

      yield
    end

    def fetch!(key, &block)
      result = fetch(key, &block)
      remove(key)
      result
    end

    def immutable
      old = @opts
      result = yield self
      @opts = old
      result
    end

    def remove(key)
      return_value = nil

      @opts = @opts.delete_if do |item|
        head, tail = split.call item

        return_value = tail if head == key.to_s
      end

      return_value
    end

    def reset!
      @opts = []

      self
    end

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

      update other.instance_variable_get(:@opts)

      self
    end

    def to_s
      opts
    end

    def to_h
      @opts.map(&split).to_h
    end

    private

    def split
      ->(entry) { entry.split('=') }
    end

    def parse(other)
      return other.split('&').map(&split).to_h if other.is_a? String

      other.map(&split).to_h
    end

    def update(other)
      return self unless other

      build(parse(other))
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
trav3-0.3.4 lib/trav3/options.rb
trav3-0.3.3 lib/trav3/options.rb
trav3-0.3.2 lib/trav3/options.rb
trav3-0.3.1 lib/trav3/options.rb
trav3-0.3.0 lib/trav3/options.rb