class SerialSpec::ParsedBody

Attributes

body[R]
raw_body[RW]
selector[RW]

Public Class Methods

new(raw_body=nil, options={}) click to toggle source
# File lib/serial_spec/parsed_body.rb, line 16
def initialize(raw_body=nil, options={})
  @selector = options[:selector] || []
  @raw_body = raw_body
end

Public Instance Methods

[](*args) click to toggle source
# File lib/serial_spec/parsed_body.rb, line 51
def [](*args)
  clone.tap do |b|
    b.selector.push([:[], *args])
  end
end
each() { |tap do |b| selector.push([:[], i]) end| ... } click to toggle source
# 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() click to toggle source
# 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, failed_message(*failure)
  end
end
first(*args) click to toggle source
# File lib/serial_spec/parsed_body.rb, line 57
def first(*args)
  clone.tap do |b|
    b.selector.push([:first])
  end
end
last(*args) click to toggle source
# File lib/serial_spec/parsed_body.rb, line 63
def last(*args)
  clone.tap do |b|
    b.selector.push([:last])
  end
end