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

Methods

Protected Instance methods

optional(*names)

Alias for optionals

Creates optional handlers for matchers dynamically.

Options

  • :default - The default value for this optional
  • :alias - An alias for this optional
  • :splat - Should be true if you expects multiple arguments
  • :block - Tell this optional can also receive blocks

Examples

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

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

[Source]

     # File lib/remarkable/dsl/optionals.rb, line 131
131:           def optionals(*names)
132:             options = names.extract_options!
133: 
134:             @matcher_optionals += names
135:             default = options[:default] ? "=#{options[:default].inspect}" : nil
136: 
137:             block = if options[:block]
138:               @matcher_optionals_block += names 
139:               default ||= "=nil"
140:               ', &block'
141:             else
142:               nil
143:             end
144: 
145:             splat = if options[:splat]
146:               @matcher_optionals_splat += names
147:               '*'
148:             else
149:               nil
150:             end
151: 
152:             names.each do |name|
153:               class_eval "def \#{name}(\#{splat}value\#{default}\#{block})\n@options ||= {}\n\#{\"@options[:\#{name}] ||= []\" if splat}\n@options[:\#{name}] \#{:+ if splat}= \#{\"block ||\" if block} value\nself\nend\ndef \#{name}=(value)\n@options ||= {}\n@options[:\#{name}] = value\nself\nend\n", __FILE__, __LINE__
154:             end
155: 
156:             class_eval %{
157:               alias :#{options[:alias]} :#{names.last}
158:               alias :#{options[:alias]}= :#{names.last}=
159:             } if options[:alias]
160: 
161:             # Call unique to avoid duplicate optionals.

162:             @matcher_optionals.uniq!
163:           end
prepend_optional(*names)

Alias for prepend_optionals

Instead of appending, prepend optionals to the beginning of optionals array. This is important because the optionals declaration order changes how the description message is generated.

[Source]

     # File lib/remarkable/dsl/optionals.rb, line 183
183:           def prepend_optionals(*names)
184:             current_optionals  = @matcher_optionals.dup
185:             @matcher_optionals = []
186:             optional(*names)
187:             @matcher_optionals += current_optionals
188:             @matcher_optionals.uniq!
189:           end

[Validate]