class RSpec::Rabl::AttributeMatcher

Attributes

expected_value_set[R]
opts[R]
rendered_attribute[R]
subject[R]

Public Class Methods

new(rendered_attribute, opts = {}) click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 4
def initialize(rendered_attribute, opts = {})
  @rendered_attribute = rendered_attribute
  @opts = opts
end

Public Instance Methods

description() click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 9
def description
  "render the #{rendered_attribute} attribute"
end
failure_message() click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 13
def failure_message
  if implicit_nil_value?
    "nil value when testing.\n expected #{expected_value.inspect} in #{attribute_path}\n  got #{rendered_value.inspect}\n If you want to test for a nil value, please use `with_value(nil)`"
  else
    "expected #{expected_value.inspect} in #{attribute_path}\n  got #{rendered_value.inspect}"
  end
end
implicit_nil_value?() click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 26
def implicit_nil_value?
  return false if expected_value_set
  rendered_value.nil? || expected_value.nil?
end
matches?(subject) click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 21
def matches?(subject)
  @subject = subject
  attribute_rendered? && (!expected_value_set || rendered_value == expected_value) && !implicit_nil_value?
end
with(model_attribute) click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 31
def with(model_attribute)
  @model_attribute = model_attribute
  self
end
with_value(expected_value) click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 36
def with_value(expected_value)
  @expected_value_set = true
  @expected_value = expected_value
  self
end

Private Instance Methods

attribute_path() click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 45
def attribute_path
  @attribute_path ||= begin
    path = ""
    path << "[\"#{opts[:root]}\"]" if opts[:root]
    path << "[\"#{opts[:object_root]}\"]" if opts[:object_root]
    path << "[\"#{rendered_attribute}\"]"
  end
end
attribute_rendered?() click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 72
def attribute_rendered?
  parsed_object.key?(rendered_attribute.to_s)
end
collection?() click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 84
def collection?
  rendered_object.is_a?(Array)
end
expected_value() click to toggle source

The expected value may be set to nil or false. Retrieving the value from the rendered object will then fail if the attribute is calculated from a node

# File lib/rspec/rabl/attribute_matcher.rb, line 60
def expected_value
  @expected_value ||= @expected_value_set ? @expected_value : get_attribute_from_rendered_object
end
get_attribute_from_rendered_object() click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 76
def get_attribute_from_rendered_object
  if collection?
    rendered_object.first.public_send(model_attribute)
  else
    rendered_object.public_send(model_attribute)
  end
end
model_attribute() click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 54
def model_attribute
  @model_attribute ||= rendered_attribute
end
parse_collection_object() click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 92
def parse_collection_object
  data = parsed
  if opts[:root]
    data = data.fetch(opts[:root]){ raise ::RSpec::Rabl::Error.new("Missing Root #{opts[:root]}") }
  end
  data = data.first
  if opts[:object_root]
    data = data.fetch(opts[:object_root]){ raise ::RSpec::Rabl::Error.new("Missing Object Root #{opts[:object_root]}") }
  end
  data
end
parse_object() click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 104
def parse_object
  if opts[:root]
    parsed.fetch(opts[:root]){ raise ::RSpec::Rabl::Error.new("Missing Object Root") }
  else
    parsed
  end
end
parsed() click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 112
def parsed
  @parsed ||= JSON.parse(subject.render)
end
parsed_object() click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 88
def parsed_object
  collection? ? parse_collection_object : parse_object
end
rendered_object() click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 68
def rendered_object
  subject.object
end
rendered_value() click to toggle source
# File lib/rspec/rabl/attribute_matcher.rb, line 64
def rendered_value
  parsed_object[rendered_attribute.to_s]
end