Class: RailsHttpLab::Bruno::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_http_lab/bruno/block.rb

Constant Summary collapse

MODES =
%i[kv raw].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, mode:, pairs: nil, raw: nil) ⇒ Block

Returns a new instance of Block.

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
# File 'lib/rails_http_lab/bruno/block.rb', line 8

def initialize(name:, mode:, pairs: nil, raw: nil)
  raise ArgumentError, "invalid mode: #{mode}" unless MODES.include?(mode)
  @name  = name
  @mode  = mode
  @pairs = pairs || []
  @raw   = raw
end

Instance Attribute Details

#modeObject

Returns the value of attribute mode.



6
7
8
# File 'lib/rails_http_lab/bruno/block.rb', line 6

def mode
  @mode
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/rails_http_lab/bruno/block.rb', line 6

def name
  @name
end

#pairsObject

Returns the value of attribute pairs.



6
7
8
# File 'lib/rails_http_lab/bruno/block.rb', line 6

def pairs
  @pairs
end

#rawObject

Returns the value of attribute raw.



6
7
8
# File 'lib/rails_http_lab/bruno/block.rb', line 6

def raw
  @raw
end

Instance Method Details

#[](key) ⇒ Object



19
20
21
22
23
# File 'lib/rails_http_lab/bruno/block.rb', line 19

def [](key)
  return nil unless kv?
  found = pairs.find { |k, _| k == key }
  found && found[1]
end

#[]=(key, value) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/rails_http_lab/bruno/block.rb', line 25

def []=(key, value)
  return unless kv?
  existing = pairs.find { |k, _| k == key }
  if existing
    existing[1] = value
  else
    pairs << [key, value]
  end
end

#kv?Boolean

Returns:

  • (Boolean)


16
# File 'lib/rails_http_lab/bruno/block.rb', line 16

def kv?;  mode == :kv;  end

#raw?Boolean

Returns:

  • (Boolean)


17
# File 'lib/rails_http_lab/bruno/block.rb', line 17

def raw?; mode == :raw; end

#to_hObject



35
36
37
38
39
40
41
# File 'lib/rails_http_lab/bruno/block.rb', line 35

def to_h
  if kv?
    pairs.to_h
  else
    { raw: raw }
  end
end