Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A plugin for DataMapper, a prototype version of dm-should

branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

README.rdoc

dm-should

Current Tree

lib
|-- dm-should
|   |-- model.rb
|   |-- property.rb
|   |-- resource.rb
|   |-- spec_class
|   |   |-- be_integer.rb
|   |   |-- be_present.rb
|   |   |-- be_unique.rb
|   |   `-- match.rb
|   |-- spec_class.rb
|   |-- spec_collector.rb
|   |-- specs
|   |   |-- errors.rb
|   |   |-- errors_on_property.rb
|   |   |-- model_specs.rb
|   |   `-- property_specs.rb
|   |-- specs.rb
|   |-- translation.rb
|   `-- version.rb
`-- dm-should.rb

3 directories, 17 files

Current SpecDoc

01: DataMapper::Model (Class methods) with dm-should

property_with_spec method

  • should be defined in order to define specs of models

  • should be used like below:

    class Item1
      include DataMapper::Resource
    
      property :id, Serial
      property_with_spec :name, String do
        should be_present
      end
    end

Available predicates

  • All available predicates are defined on DataMapper::Should::AvailablePredicates module

  • Available predicates means the methods could be used in property_with_spec block

02: DataMapper::Resource (Instance methods) with dm-should

valid? method

  • should return true if a record satisfy its specs and false if it doesn't

  • a record shouldn't be saved when valid? returns false

errors method

  • should return an instance of DataMapper::Should::Errors.

  • Once valid? method executed and return false, record.errors has details about it

  • You can generate error messages by passing a block to translation_keys_each method:

    record.errors.translation_keys_each do |translation_key, assigns|
      your_translation_system.translate(translation_key, assigns)
    end

    The arguments of the block is translation_key and assigns:

    translation_key

    a translation_key is kind of 'warn.be_present' or 'warn.be_unique'. They are combinations of 'doctype.spectype'.

    assigns[:field]

    is now a String like 'Modelname.fieldname'. this is also supposed to be translated

    assigns[:actual]

    is an actual value at the recently validated field

  • Or, You can simply use record.error.error_messages to get translated error message with default translation system

    record.errors.error_messages
    # => ["Item.name was expected to be present, but it wasn't"]

    You can customize the default message set. See DataMapper::Should::Translation.translations.

03: Specs Class and SpecClass of dm-should

What is Specs?

For example, when you have an Item class like this:

class Item
  include DataMapper::Resource
  property :id, Serial
  property_with_spec :name, String do
    should be_present
    should be_unique
    should match(/^A/)
  end
end
  • Item.specs is an instance of DataMapper::Should::ModelSpecs,

    its class is a child class of DataMapper::Should::Specs.

  • Item.specs.on(:name) is an instance of DataMapper::Should::PropertySpecs,

    its class is a child class of DataMapper::Should::Specs.

  • record.errors is an instance of DataMapper::Should::Errors,

    its class is a child class of DataMapper::Should::ModelSpecs.

  • record.errors.on(:name) is an instance of DataMapper::Should::ErrorsOnProperty,

    its class is a child class of DataMapper::Should::PropertySpecs.

  • These DataMapper::Should::Specs's subclasses stores objects of SpecClass

  • You can access one of SpecClass of a Specs instance by [] method

What is SpecClass?

There are 5 subclasses now.

DataMapper::Should::BePresent, DataMapper::Should::BeInteger, DataMapper::Should::BePositiveInteger, DataMapper::Should::BeUnique, DataMapper::Should::Match

  • They have satisfy? method to ensure themselves

    satisfy? method is sended when record.valid? .

04: Details, especially about each SpecClass's

  • TODO: I'll put some infomation here

be_present

property_with_spec :number, Fixnum do
  should be_present
end

when a [String] record.name should be present, record.valid? returns..

  • false if record.name is nil

  • false if record.name is empty string

  • false if record.name only contains invisible string

  • false if record.name only contains invisible string ( including 2 byte space )

  • true if record.name is string that is not empty string.

when a [Integer] record.number should be present, record.valid? returns..

  • false if record.number is nil

  • false if record.number is empty string

  • true if record.number is numeric string

  • true if record.number is a Fixnum

  • true if record.number is an Array

Finished in 0.023154 seconds

28 examples, 0 failures

Something went wrong with that request. Please try again.