Sha256: de8f2312aca2e172c33461bbad492cf3f6bded2c9f8e65a3439ce27157a0de9d
Contents?: true
Size: 1.27 KB
Versions: 2
Compression:
Stored size: 1.27 KB
Contents
require "json" require "active_support/core_ext/hash/indifferent_access" module SerialSpec class ParsedBody attr_reader :raw_body attr_reader :selector def initialize(body) @selector = [] @raw_body = JSON.parse(body) end def self.add_selector(*methuds) methuds.each do |methud| class_eval <<-METHOD def #{methud.to_s}(*args) selector.push([#{methud.to_sym}, args]) self end METHOD end end add_selector :first, :last, :[] def [](*args) selector.push([:[], *args]) self end def first(*args) selector.push([:first]) self end def last(*args) selector.push([:last]) self end def execute copy = selector.clone selector.clear copy.inject(raw_body) do |remainder, method_and_args| begin methud, *args = method_and_args if remainder.kind_of?(Hash) remainder.with_indifferent_access.send methud, *args else remainder.send methud, *args end rescue NoMethodError => ex raise ArgumentError, "could not find '#{methud.inspect}' \nfor:\n '#{remainder.inspect}' \nin\n #{raw_body}" end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
serial-spec-0.2.0 | lib/serial_spec/parsed_body.rb |
serial-spec-0.1.1 | lib/serial_spec/parsed_body.rb |