lib/remarkable/dsl/optionals.rb in remarkable-3.0.1 vs lib/remarkable/dsl/optionals.rb in remarkable-3.0.2
- old
+ new
@@ -1,17 +1,17 @@
module Remarkable
module DSL
- module Optionals
-
- OPTIONAL_KEYS = [ :positive, :negative, :not_given ]
-
- def self.included(base) #:nodoc:
- base.extend ClassMethods
+ module Optionals
+
+ OPTIONAL_KEYS = [ :positive, :negative, :not_given ]
+
+ def self.included(base) #:nodoc:
+ base.extend ClassMethods
end
-
- module ClassMethods
-
+
+ module ClassMethods
+
protected
# Creates optional handlers for matchers dynamically. The following
# statement:
#
@@ -26,11 +26,11 @@
# end
#
# Options:
#
# * <tt>:default</tt> - The default value for this optional
- # * <tt>:alias</tt> - An alias for this optional
+ # * <tt>:alias</tt> - An alias for this optional
# * <tt>:splat</tt> - Should be true if you expects multiple arguments
#
# Examples:
#
# optional :name, :title
@@ -57,26 +57,26 @@
# #=> "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"
#
- # The interpolation options available are "value" and "inspect". Where
- # the first is the optional value transformed into a string and the
- # second is the inspected value.
- #
- # Three keys are available to be used in I18n files and control how
+ # The interpolation options available are "value" and "inspect". Where
+ # the first is the optional value transformed into a string and the
+ # second is the inspected value.
+ #
+ # 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.
#
def optionals(*names)
options = names.extract_options!
- @matcher_optionals += names
-
- splat = options[:splat] ? '*' : ''
+ @matcher_optionals += names
+
+ splat = options[:splat] ? '*' : ''
default = options[:default] ? "=#{options[:default].inspect}" : ""
names.each do |name|
class_eval <<-END, __FILE__, __LINE__
def #{name}(#{splat}value#{default})
@@ -88,53 +88,53 @@
end
class_eval "alias_method(:#{options[:alias]}, :#{names.last})" if options[:alias]
# Call unique to avoid duplicate optionals.
@matcher_optionals.uniq!
- end
- alias :optional :optionals
-
- # Instead of appending, prepend optionals to the beginning of optionals
- # array. This is important because this decide how the description
- # message is generated.
- #
- def prepend_optionals(*names)
- current_optionals = @matcher_optionals.dup
- @matcher_optionals = []
- optional(*names)
- @matcher_optionals += current_optionals
- @matcher_optionals.uniq!
- end
- alias :prepend_optional :prepend_optionals
-
- end
-
+ end
+ alias :optional :optionals
+
+ # Instead of appending, prepend optionals to the beginning of optionals
+ # array. This is important because this decide how the description
+ # message is generated.
+ #
+ def prepend_optionals(*names)
+ current_optionals = @matcher_optionals.dup
+ @matcher_optionals = []
+ optional(*names)
+ @matcher_optionals += current_optionals
+ @matcher_optionals.uniq!
+ end
+ alias :prepend_optional :prepend_optionals
+
+ end
+
# Overwrites description to support optionals. Check <tt>optional</tt> for
# more information.
#
def description(options={}) #:nodoc:
- message = super(options)
+ message = super(options)
message.strip!
optionals = self.class.matcher_optionals.map do |optional|
scope = matcher_i18n_scope + ".optionals.#{optional}"
- if @options.key?(optional)
+ if @options.key?(optional)
value = @options[optional]
- defaults = [ (value ? :positive : :negative) ]
-
- # If optional is a symbol and it's not any to any of the reserved symbols, search for it also
- defaults.unshift(value) if value.is_a?(Symbol) && !OPTIONAL_KEYS.include?(value)
+ defaults = [ (value ? :positive : :negative) ]
- Remarkable.t defaults.shift, :default => defaults, :raise => true, :scope => scope,
- :inspect => value.inspect, :value => value.to_s
+ # If optional is a symbol and it's not any to any of the reserved symbols, search for it also
+ defaults.unshift(value) if value.is_a?(Symbol) && !OPTIONAL_KEYS.include?(value)
+
+ Remarkable.t defaults.shift, :default => defaults, :raise => true, :scope => scope,
+ :inspect => value.inspect, :value => value.to_s
else
Remarkable.t :not_given, :raise => true, :scope => scope
end rescue nil
end.compact
- message << ' ' << array_to_sentence(optionals)
- message.strip!
+ message << ' ' << array_to_sentence(optionals)
+ message.strip!
message
end
end
end