class Shoulda::Matchers::ActiveRecord::SerializeMatcher

@private

Public Class Methods

new(name) click to toggle source
# File lib/shoulda/matchers/active_record/serialize_matcher.rb, line 94
def initialize(name)
  @name = name.to_s
  @options = {}
end

Public Instance Methods

as(type) click to toggle source
# File lib/shoulda/matchers/active_record/serialize_matcher.rb, line 99
def as(type)
  @options[:type] = type
  self
end
as_instance_of(type) click to toggle source
# File lib/shoulda/matchers/active_record/serialize_matcher.rb, line 104
def as_instance_of(type)
  @options[:instance_type] = type
  self
end
description() click to toggle source
# File lib/shoulda/matchers/active_record/serialize_matcher.rb, line 122
def description
  description = "serialize :#{@name}"
  description += " class_name => #{@options[:type]}" if @options.key?(:type)
  description
end
failure_message() click to toggle source
# File lib/shoulda/matchers/active_record/serialize_matcher.rb, line 114
def failure_message
  "Expected #{expectation} (#{@missing})"
end
failure_message_when_negated() click to toggle source
# File lib/shoulda/matchers/active_record/serialize_matcher.rb, line 118
def failure_message_when_negated
  "Did not expect #{expectation}"
end
matches?(subject) click to toggle source
# File lib/shoulda/matchers/active_record/serialize_matcher.rb, line 109
def matches?(subject)
  @subject = subject
  serialization_valid? && type_valid?
end

Protected Instance Methods

attribute_is_serialized?() click to toggle source
# File lib/shoulda/matchers/active_record/serialize_matcher.rb, line 185
def attribute_is_serialized?
  !!serialization_coder
end
class_valid?() click to toggle source
# File lib/shoulda/matchers/active_record/serialize_matcher.rb, line 139
def class_valid?
  if @options[:type]
    klass = serialization_coder
    if klass == @options[:type]
      true
    else
      if klass.respond_to?(:object_class) && klass.object_class == @options[:type]
        true
      else
        @missing = ":#{@name} should be a type of #{@options[:type]}"
        false
      end
    end
  else
    true
  end
end
expectation() click to toggle source
# File lib/shoulda/matchers/active_record/serialize_matcher.rb, line 178
def expectation
  expectation = "#{model_class.name} to serialize the attribute called :#{@name}"
  expectation += " with a type of #{@options[:type]}" if @options[:type]
  expectation += " with an instance of #{@options[:instance_type]}" if @options[:instance_type]
  expectation
end
instance_class_valid?() click to toggle source
# File lib/shoulda/matchers/active_record/serialize_matcher.rb, line 161
def instance_class_valid?
  if @options.key?(:instance_type)
    if serialization_coder.is_a?(@options[:instance_type])
      true
    else
      @missing = ":#{@name} should be an instance of #{@options[:type]}"
      false
    end
  else
    true
  end
end
model() click to toggle source
# File lib/shoulda/matchers/active_record/serialize_matcher.rb, line 193
def model
  @subject.class
end
model_class() click to toggle source
# File lib/shoulda/matchers/active_record/serialize_matcher.rb, line 157
def model_class
  @subject.class
end
serialization_coder() click to toggle source
# File lib/shoulda/matchers/active_record/serialize_matcher.rb, line 189
def serialization_coder
  RailsShim.attribute_serialization_coder_for(model, @name)
end
serialization_valid?() click to toggle source
# File lib/shoulda/matchers/active_record/serialize_matcher.rb, line 130
def serialization_valid?
  if attribute_is_serialized?
    true
  else
    @missing = "no serialized attribute called :#{@name}"
    false
  end
end
type_valid?() click to toggle source
# File lib/shoulda/matchers/active_record/serialize_matcher.rb, line 174
def type_valid?
  class_valid? && instance_class_valid?
end