Sha256: f56c95c832e03a72540b00eff67c2b86d9e14a1cc0644dce08361dfab1bf3668

Contents?: true

Size: 801 Bytes

Versions: 1

Compression:

Stored size: 801 Bytes

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 ||= []

      for (key, value) in args
        remove(key)
        @opts.push(pb[key, value])
      end
    end

    def remove(key)
      @opts = @opts.keep_if {|a, _| ki[key][a] }
    end

    def +(other)
      raise ArgumentError, "Invalid type provided." unless other.is_a?(Options)
      @opts += other.instance_variable_get(:@opts)
    end

    def to_s
      opts
    end

    def pb
      lambda {|param, arg| "#{param}=#{arg}" }
    end
    private :pb

    def ki
      lambda {|key| lambda {|item| !(/^#{key}=/ === "#{item}") } }
    end
    private :ki
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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