Class: RailsHttpLab::Bruno::Block
- Inherits:
-
Object
- Object
- RailsHttpLab::Bruno::Block
- Defined in:
- lib/rails_http_lab/bruno/block.rb
Constant Summary collapse
- MODES =
%i[kv raw].freeze
Instance Attribute Summary collapse
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#name ⇒ Object
Returns the value of attribute name.
-
#pairs ⇒ Object
Returns the value of attribute pairs.
-
#raw ⇒ Object
Returns the value of attribute raw.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#initialize(name:, mode:, pairs: nil, raw: nil) ⇒ Block
constructor
A new instance of Block.
- #kv? ⇒ Boolean
- #raw? ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(name:, mode:, pairs: nil, raw: nil) ⇒ Block
Returns a new instance of Block.
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
#mode ⇒ Object
Returns the value of attribute mode.
6 7 8 |
# File 'lib/rails_http_lab/bruno/block.rb', line 6 def mode @mode end |
#name ⇒ Object
Returns the value of attribute name.
6 7 8 |
# File 'lib/rails_http_lab/bruno/block.rb', line 6 def name @name end |
#pairs ⇒ Object
Returns the value of attribute pairs.
6 7 8 |
# File 'lib/rails_http_lab/bruno/block.rb', line 6 def pairs @pairs end |
#raw ⇒ Object
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
16 |
# File 'lib/rails_http_lab/bruno/block.rb', line 16 def kv?; mode == :kv; end |
#raw? ⇒ Boolean
17 |
# File 'lib/rails_http_lab/bruno/block.rb', line 17 def raw?; mode == :raw; end |
#to_h ⇒ Object
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 |