module BooticClient::Stubbing::Stubber

Public Instance Methods

from_hash(h) click to toggle source
# File lib/bootic_client/stubbing.rb, line 6
def from_hash(h)
  self
end
method_missing(method_name, *args, &block) click to toggle source
# File lib/bootic_client/stubbing.rb, line 32
def method_missing(method_name, *args, &block)
  opts = stringify_keys(args.first)
  if stub = stubs[stub_key(method_name, opts)]
    stub.returns? ? stub.returns : stub
  else
    raise MissingStubError, "No method stubbed for '#{method_name}' with options #{opts.inspect}"
  end
end
respond_to_missing?(method_name, include_private = false) click to toggle source
# File lib/bootic_client/stubbing.rb, line 41
def respond_to_missing?(method_name, include_private = false)
  stubs.keys.any?{|k|
    k.to_s =~ /^#{method_name.to_s}/
  }
end
stub(method_name, opts = {}) click to toggle source
# File lib/bootic_client/stubbing.rb, line 22
def stub(method_name, opts = {})
  key = stub_key(method_name, opts)
  if st = stubs[key]
    st.stub(method_name, opts)
    st
  else
    stubs[key] = Stub.new(method_name, opts)
  end
end
stub_chain(method_path, opts = {}) click to toggle source
# File lib/bootic_client/stubbing.rb, line 10
def stub_chain(method_path, opts = {})
  meths = method_path.split('.')
  c = 0
  opts = stringify_keys(opts)

  meths.reduce(self) do |stub, method_name|
    c += 1
    a = c == meths.size ? opts : {}
    stub.stub(method_name, a)
  end
end

Private Instance Methods

options_key(opts) click to toggle source
# File lib/bootic_client/stubbing.rb, line 52
def options_key(opts)
  # sort keys
  keys = opts.keys.sort
  hash = keys.each_with_object({}) do |key, h|
    value = if opts[key].is_a?(Hash)
      options_key(opts[key])
    else
      opts[key].to_s
    end

    h[key] = value
  end

  hash.inspect
end
stringify_keys(hash) click to toggle source
# File lib/bootic_client/stubbing.rb, line 68
def stringify_keys(hash)
  return hash unless hash.is_a?(Hash)

  hash.each_with_object({}) do |(k, v), h|
    h[k.to_s] = stringify_keys(v)
  end
end
stub_key(method_name, opts) click to toggle source
# File lib/bootic_client/stubbing.rb, line 48
def stub_key(method_name, opts)
  [method_name.to_s, options_key(opts || {})].join('_')
end