Class: Trav3::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/trav3/options.rb

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Options

Returns a new instance of Options


3
4
5
# File 'lib/trav3/options.rb', line 3

def initialize(**args)
  build(**args)
end

Instance Method Details

#+(other) ⇒ Object

Raises:

  • (TypeError)

45
46
47
48
49
50
51
# File 'lib/trav3/options.rb', line 45

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

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

  self
end

#build(**args) ⇒ Object


15
16
17
18
19
20
21
22
23
24
# File 'lib/trav3/options.rb', line 15

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

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

  self
end

#optsObject


7
8
9
10
11
12
13
# File 'lib/trav3/options.rb', line 7

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

#remove(key) ⇒ Object


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/trav3/options.rb', line 26

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

#reset!Object


41
42
43
# File 'lib/trav3/options.rb', line 41

def reset!
  @opts = []
end

#to_hObject


57
58
59
# File 'lib/trav3/options.rb', line 57

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

#to_sObject


53
54
55
# File 'lib/trav3/options.rb', line 53

def to_s
  opts
end