class Storext::Matchers::HaveAttributeMatcher

Public Class Methods

new(name, type = nil) click to toggle source
# File lib/storext/matchers/have_attribute_matcher.rb, line 4
def initialize(name, type = nil)
  @name = name
  @expected_type = type
end

Public Instance Methods

description() click to toggle source
# File lib/storext/matchers/have_attribute_matcher.rb, line 30
def description
  messages = ["have attribute #{@name}"]
  messages << ["of type #{@expected_type}"]
  if @expected_column
    messages << [%Q(in column "#{@expected_column}")]
  end
  if @expected_default
    messages << [%Q(with default "#{@expected_default}")]
  end

  messages.join(" ")
end
failure_message() click to toggle source
# File lib/storext/matchers/have_attribute_matcher.rb, line 43
def failure_message
  "expected #{@klass} to #{description}"
end
in_column(column) click to toggle source
# File lib/storext/matchers/have_attribute_matcher.rb, line 9
def in_column(column)
  @expected_column = column
  self
end
matches?(class_or_record) click to toggle source
# File lib/storext/matchers/have_attribute_matcher.rb, line 19
def matches?(class_or_record)
  @klass = class_or_record.is_a?(ActiveRecord::Base) ?
    class_or_record.class : class_or_record
  definition = @klass.storext_definitions[@name]

  definition.present? &&
    matches_type?(definition[:type]) &&
    matches_column?(definition[:column]) &&
    matches_default?(definition[:opts][:default])
end
with_default(expected_default) click to toggle source
# File lib/storext/matchers/have_attribute_matcher.rb, line 14
def with_default(expected_default)
  @expected_default = expected_default
  self
end

Private Instance Methods

matches_column?(column) click to toggle source
# File lib/storext/matchers/have_attribute_matcher.rb, line 54
def matches_column?(column)
  return true if @expected_column.nil?
  @expected_column == column
end
matches_default?(default) click to toggle source
# File lib/storext/matchers/have_attribute_matcher.rb, line 59
def matches_default?(default)
  return true if @expected_default.nil?
  instance = @klass.new
  instance.send(@name) == @expected_default
end
matches_type?(type) click to toggle source
# File lib/storext/matchers/have_attribute_matcher.rb, line 49
def matches_type?(type)
  return true if @expected_type.nil?
  @expected_type == type
end