Small perf, readability refactor to Test::Serializer

This commit is contained in:
Benjamin Fleischer 2015-12-22 22:43:06 -06:00
parent 37a6d2b245
commit ef09c9043f

View File

@ -1,3 +1,4 @@
require 'set'
module ActiveModelSerializers module ActiveModelSerializers
module Test module Test
module Serializer module Serializer
@ -35,15 +36,16 @@ module ActiveModelSerializers
end end
class AssertSerializer class AssertSerializer
EVENT_NAME = 'render.active_model_serializers'
attr_reader :serializers, :message attr_reader :serializers, :message
attr_accessor :response, :expectation attr_accessor :response, :expectation
def initialize def initialize
@serializers = [] @serializers = Set.new
end end
def message=(message) def message=(message)
@message = message || "expecting <#{expectation.inspect}> but rendering with <#{serializers}>" @message = message || "expecting <#{expectation.inspect}> but rendering with <#{serializers.to_a}>"
end end
def matches? def matches?
@ -51,18 +53,12 @@ module ActiveModelSerializers
response.body response.body
case expectation case expectation
when a_serializer? when a_serializer? then matches_class?
matches_class? when Symbol then matches_symbol?
when Symbol when String then matches_string?
matches_symbol? when Regexp then matches_regexp?
when String when NilClass then matches_nil?
matches_string? else fail ArgumentError, 'assert_serializer only accepts a String, Symbol, Regexp, ActiveModel::Serializer, or nil'
when Regexp
matches_regexp?
when NilClass
matches_nil?
else
fail ArgumentError, 'assert_serializer only accepts a String, Symbol, Regexp, ActiveModel::Serializer, or nil'
end end
end end
@ -99,7 +95,7 @@ module ActiveModelSerializers
end end
def matches_nil? def matches_nil?
serializers.blank? serializers.empty?
end end
def a_serializer? def a_serializer?
@ -107,7 +103,7 @@ module ActiveModelSerializers
end end
def event_name def event_name
'render.active_model_serializers' EVENT_NAME
end end
end end