Module Remarkable::DSL::Optionals
In: lib/remarkable/dsl/optionals.rb

This module is responsable for create optional handlers and providing macro configration blocks. Consider the matcher below:

  class AllowValuesForMatcher < Remarkable::ActiveRecord::Base
    arguments :collection => :attributes, :as => :attribute

    optional :message
    optional :in, :splat => true
    optional :allow_nil, :allow_blank, :default => true
  end

This allow the matcher to be called as:

  it { should allow_values_for(:email).in("jose.valim@gmail.com", "jose@another.com").message(:invalid).allow_nil }

It also allow macros to be configured with blocks:

  should_allow_values_for :email do |m|
    m.message :invalid
    m.allow_nil
    m.in "jose.valim@gmail.com"
    m.in "jose@another.com"
  end

Which could be also writen as:

  should_allow_values_for :email do |m|
    m.message = :invalid
    m.allow_nil = true
    m.in = [ "jose.valim@gmail.com", "jose@another.com" ]
  end

The difference between the them are: 1) optional= always require an argument even if :default is given. 2) optional= always overwrite all previous values even if :splat is given.

Blocks can be also given when :block => true is set:

  should_set_session :user_id do |m|
    m.to { @user.id }
  end

I18n

Optionals will be included in description messages if you assign them properly on your locale file. If you have a validate_uniqueness_of matcher with the following on your locale file:

  description: validate uniqueness of {{attributes}}
  optionals:
    scope:
      positive: scoped to {{inspect}}
    case_sensitive:
      positive: case sensitive
      negative: case insensitive

When invoked like below will generate the following messages:

  validate_uniqueness_of :project_id, :scope => :company_id
  #=> "validate uniqueness of project_id scoped to :company_id"

  validate_uniqueness_of :project_id, :scope => :company_id, :case_sensitive => true
  #=> "validate uniqueness of project_id scoped to :company_id and case sensitive"

  validate_uniqueness_of :project_id, :scope => :company_id, :case_sensitive => false
  #=> "validate uniqueness of project_id scoped to :company_id and case insensitive"

Interpolation options

The default interpolation options available are "inspect" and "value". Whenever you use :splat => true, it also adds a new interpolation option called {{sentence}}.

Given the following matcher call:

  validate_uniqueness_of :id, :scope => [ :company_id, :project_id ]

The following yml setting and outputs are:

   scope:
     positive: scoped to {{inspect}}
     # Outputs: "validate uniqueness of project_id scoped to [ :company_id, :project_id ]"

     positive: scoped to {{value}}
     # Outputs: "validate uniqueness of project_id scoped to company_idproject_id"

     positive: scoped to {{value}}
     # Outputs: "validate uniqueness of project_id scoped to company_id and project_id"

Interpolation keys

Three keys are available to be used in I18n files and control how optionals are appended to your description:

  * <tt>positive</tt> - When the optional is given and it evaluates to true (everything but false and nil).
  * <tt>negative</tt> - When the optional is given and it evaluates to false (false or nil).
  * <tt>not_given</tt> - When the optional is not given.

Classes and Modules

Module Remarkable::DSL::Optionals::ClassMethods

Constants

OPTIONAL_KEYS = [ :positive, :negative, :not_given ]

[Validate]