Sha256: 11af162a73d3932c129db5569665bf70338a87987c53249495ba9063f7cd28a1

Contents?: true

Size: 1.91 KB

Versions: 6

Compression:

Stored size: 1.91 KB

Contents

# This is based on Shoulda model builder for Test::Unit.
#
module ModelBuilder
  def self.included(base)
    return unless base.name =~ /^Spec/

    base.class_eval do
      after(:each) do
        if @defined_constants
          @defined_constants.each do |class_name| 
            Object.send(:remove_const, class_name)
          end
        end
      end
    end

    base.extend ClassMethods
  end

  def define_constant(class_name, &block)
    class_name = class_name.to_s.camelize

    klass = Class.new
    Object.const_set(class_name, klass)

    klass.class_eval(&block) if block_given?

    @defined_constants ||= []
    @defined_constants << class_name

    klass
  end

  def define_model(name, columns = {}, &block)
    instance = define_constant(name.to_s.classify, &block).new

    self.class.subject { instance } if self.class.respond_to?(:subject)
    instance
  end

  module ClassMethods
    # This is a macro to run validations of boolean optionals such as :allow_nil
    # and :allow_blank. This macro tests all scenarios. The specs must have a
    # define_and_validate method defined.
    #
    def create_optional_boolean_specs(optional, base, options={})
      base.describe "with #{optional} option" do
        it { should define_and_validate(options.merge(optional => true)).send(optional)            }
        it { should define_and_validate(options.merge(optional => false)).send(optional, false)    }
        it { should_not define_and_validate(options.merge(optional => true)).send(optional, false) }
        it { should_not define_and_validate(options.merge(optional => false)).send(optional)       }
      end
    end

    def create_message_specs(base)
      base.describe "with message option" do
        it { should define_and_validate(:message => 'valid_message').message('valid_message') }
        it { should_not define_and_validate(:message => 'not_valid').message('valid_message') }
      end
    end
  end

end

Version data entries

6 entries across 6 versions & 4 rubygems

Version Path
yeastymobs-remarkable_mongomapper-0.1.1 spec/model_builder.rb
remarkable_mongo-0.1.4 spec/model_builder.rb
remarkable_mongo-0.1.3 spec/model_builder.rb
remarkable_mongo_ign-0.1.2 spec/model_builder.rb
ippy04-remarkable_mongo-0.1.3 spec/model_builder.rb
remarkable_mongo-0.1.2 spec/model_builder.rb