Class: SerialSpec::ParsedBody
- Inherits:
-
Object
- Object
- SerialSpec::ParsedBody
- Includes:
- Enumerable
- Defined in:
- lib/serial_spec/parsed_body.rb
Defined Under Namespace
Classes: MissingSelectorError, NotAnEnumerableObject
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#raw_body ⇒ Object
Returns the value of attribute raw_body.
-
#selector ⇒ Object
Returns the value of attribute selector.
Instance Method Summary collapse
- #[](*args) ⇒ Object
- #each(&block) ⇒ Object
- #execute ⇒ Object
- #first(*args) ⇒ Object
-
#initialize(raw_body = nil, options = {}) ⇒ ParsedBody
constructor
A new instance of ParsedBody.
- #last(*args) ⇒ Object
Constructor Details
#initialize(raw_body = nil, options = {}) ⇒ ParsedBody
Returns a new instance of ParsedBody.
16 17 18 19 |
# File 'lib/serial_spec/parsed_body.rb', line 16 def initialize(raw_body=nil, ={}) @selector = [:selector] || [] @raw_body = raw_body end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
14 15 16 |
# File 'lib/serial_spec/parsed_body.rb', line 14 def body @body end |
#raw_body ⇒ Object
Returns the value of attribute raw_body.
12 13 14 |
# File 'lib/serial_spec/parsed_body.rb', line 12 def raw_body @raw_body end |
#selector ⇒ Object
Returns the value of attribute selector.
13 14 15 |
# File 'lib/serial_spec/parsed_body.rb', line 13 def selector @selector end |
Instance Method Details
#[](*args) ⇒ Object
51 52 53 54 55 |
# File 'lib/serial_spec/parsed_body.rb', line 51 def [](*args) clone.tap do |b| b.selector.push([:[], *args]) end end |
#each(&block) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/serial_spec/parsed_body.rb', line 25 def each(&block) current_obj = clone.execute raise NotAnEnumerableObject unless current_obj.kind_of?(Hash) or current_obj.kind_of?(Array) if current_obj.kind_of?(Array) current_obj.each_with_index do |item, i| yield( clone.tap do |b| b.selector.push([:[], i]) end ) end else current_obj.each do |key, value| yield( key, clone.tap do |b| b.selector.push([:[], key]) end ) end end end |
#execute ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/serial_spec/parsed_body.rb', line 69 def execute copy = selector.clone selector.clear current_selector = [] failure = catch(:failed) do return copy.inject(body) do |remainder, method_and_args| current_selector << method_and_args methud, *args = method_and_args if [:first, :last].include?(methud) || args.first.kind_of?(Fixnum) throw(:failed, [:expected_array, remainder, current_selector, method_and_args]) unless remainder.kind_of?(Array) else throw(:failed, [:expected_object, remainder, current_selector, method_and_args]) unless remainder.kind_of?(Hash) end if remainder.kind_of?(Hash) remainder.with_indifferent_access.send methud, *args else remainder.send methud, *args end end end if failure.kind_of?(Array) raise MissingSelectorError, (*failure) end end |
#first(*args) ⇒ Object
57 58 59 60 61 |
# File 'lib/serial_spec/parsed_body.rb', line 57 def first(*args) clone.tap do |b| b.selector.push([:first]) end end |
#last(*args) ⇒ Object
63 64 65 66 67 |
# File 'lib/serial_spec/parsed_body.rb', line 63 def last(*args) clone.tap do |b| b.selector.push([:last]) end end |