carlosbrando-remarkable 2.2.9 → 2.2.10

Sign up to get free protection for your applications and to get access to all the features.
data/lib/remarkable.rb CHANGED
@@ -2,7 +2,7 @@ $:.unshift(File.dirname(__FILE__)) unless
2
2
  $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
3
 
4
4
  module Remarkable
5
- VERSION = '2.2.9'
5
+ VERSION = '2.2.10'
6
6
  end
7
7
 
8
8
  if ENV['RAILS_ENV'] == 'test' && defined?(Spec)
@@ -297,11 +297,13 @@ Options:
297
297
  * :in - A synonym(or alias) for :within.
298
298
  * :allow_nil - when supplied, validates if it allows nil or not.
299
299
  * :allow_blank - when supplied, validates if it allows blank or not.
300
- * :short_message - value the test expects to find in errors.on(:attribute).
300
+ * :too_short - value the test expects to find in errors.on(:attribute) when attribute is too short.
301
301
  Regexp, string or symbol. Default = I18n.translate('activerecord.errors.messages.too_short') % range.first
302
- * :long_message - value the test expects to find in errors.on(:attribute).
302
+ * :too_long - value the test expects to find in errors.on(:attribute) when attribute is too long.
303
303
  Regexp, string or symbol. Default = I18n.translate('activerecord.errors.messages.too_long') % range.last
304
- * :message - value the test expects to find in errors.on(:attribute) only when :minimum, :maximum or :is is given.
304
+ * :wrong_length - value the test expects to find in errors.on(:attribute) when attribute is the wrong length.
305
+ Regexp, string or symbol. Default = I18n.translate('activerecord.errors.messages.wrong_length') % range.last
306
+ * :message - value the test expects to find in errors.on(:attribute). When supplied overwrites :too_short, :too_long and :wrong_length.
305
307
  Regexp, string or symbol. Default = I18n.translate('activerecord.errors.messages.wrong_length') % value
306
308
 
307
309
  <pre><code> should_validate_length_of :password, :within => 6..20
@@ -5,21 +5,19 @@ module Remarkable # :nodoc:
5
5
  include Remarkable::ActiveRecord::Helpers
6
6
 
7
7
  arguments :behavior, :range, :attributes
8
- optional :minimum, :maximum, :short_message, :long_message
8
+ optional :message, :minimum, :maximum, :too_short, :too_long, :wrong_length
9
9
 
10
10
  assertions :less_than_min_length?, :exactly_min_length?, :allow_nil?,
11
11
  :more_than_max_length?, :exactly_max_length?, :allow_blank?
12
12
 
13
- # If message is supplied, reassign it properly to :short_message
14
- # and :long_message. This is ActiveRecord default behavior when
15
- # the validation is :maximum, :minimum or :is.
16
- #
17
- def message(message)
18
- if [:is, :minimum, :maximum].include? @behavior
19
- short_message(message)
20
- long_message(message)
21
- end
22
- self
13
+ def short_message(message)
14
+ warn "[DEPRECATION] :short_message is deprecated for validate_length_of, please use :too_short instead"
15
+ too_short(message)
16
+ end
17
+
18
+ def long_message(message)
19
+ warn "[DEPRECATION] :long_message is deprecated for validate_length_of, please use :too_long instead"
20
+ too_long(message)
23
21
  end
24
22
 
25
23
  def within(range)
@@ -51,35 +49,26 @@ module Remarkable # :nodoc:
51
49
  private
52
50
 
53
51
  def default_options
54
- if @behavior == :is
55
- { :short_message => :wrong_length, :long_message => :wrong_length }
56
- else
57
- { :short_message => :too_short, :long_message => :too_long }
58
- end
52
+ { :too_long => :too_long, :too_short => :too_short, :wrong_length => :wrong_length }
59
53
  end
60
54
 
61
- # Reassign messages properly
62
- def after_initialize
55
+ def before_assert
63
56
  # Set the values, for example:
64
57
  # send(:within, 0..10)
65
58
  send(@behavior, @range)
66
-
67
- message(@options.delete(:message)) if @options[:message]
68
- long_message(@options[:long_message])
69
- short_message(@options[:short_message])
70
59
  end
71
60
 
72
61
  def allow_nil?
73
- super(:short_message)
62
+ super(default_message_for(:too_short))
74
63
  end
75
64
 
76
65
  def allow_blank?
77
- super(:short_message)
66
+ super(default_message_for(:too_short))
78
67
  end
79
68
 
80
69
  def less_than_min_length?
81
70
  return true if @behavior == :maximum || @options[:minimum] <= 1
82
- return true if bad?(value_for_length(@options[:minimum] - 1), :short_message)
71
+ return true if bad?(value_for_length(@options[:minimum] - 1), default_message_for(:too_short))
83
72
 
84
73
  @missing = "allow #{@attribute} to be less than #{@options[:minimum]} chars long"
85
74
  return false
@@ -87,7 +76,7 @@ module Remarkable # :nodoc:
87
76
 
88
77
  def exactly_min_length?
89
78
  return true if @behavior == :maximum || @options[:minimum] <= 0
90
- return true if good?(value_for_length(@options[:minimum]), :short_message)
79
+ return true if good?(value_for_length(@options[:minimum]), default_message_for(:too_short))
91
80
 
92
81
  @missing = "not allow #{@attribute} to be exactly #{@options[:minimum]} chars long"
93
82
  return false
@@ -95,7 +84,7 @@ module Remarkable # :nodoc:
95
84
 
96
85
  def more_than_max_length?
97
86
  return true if @behavior == :minimum
98
- return true if bad?(value_for_length(@options[:maximum] + 1), :long_message)
87
+ return true if bad?(value_for_length(@options[:maximum] + 1), default_message_for(:too_long))
99
88
 
100
89
  @missing = "allow #{@attribute} to be more than #{@options[:maximum]} chars long"
101
90
  return false
@@ -103,7 +92,7 @@ module Remarkable # :nodoc:
103
92
 
104
93
  def exactly_max_length?
105
94
  return true if @behavior == :minimum || @options[:minimum] == @options[:maximum]
106
- return true if good?(value_for_length(@options[:maximum]), :long_message)
95
+ return true if good?(value_for_length(@options[:maximum]), default_message_for(:too_long))
107
96
 
108
97
  @missing = "not allow #{@attribute} to be exactly #{@options[:maximum]} chars long"
109
98
  return false
@@ -130,6 +119,16 @@ module Remarkable # :nodoc:
130
119
  def value_for_length(value)
131
120
  "x" * value
132
121
  end
122
+
123
+ # Returns the default message for the validation type.
124
+ # If user supplied :message, it will return it. Otherwise it will return
125
+ # wrong_length on :is validation and :too_short or :too_long in the other
126
+ # types.
127
+ #
128
+ def default_message_for(validation_type)
129
+ return :message if @options[:message]
130
+ @options.key?(:is) ? :wrong_length : validation_type
131
+ end
133
132
  end
134
133
 
135
134
  # Validates the length of the given attributes. You have also to supply
@@ -150,11 +149,13 @@ module Remarkable # :nodoc:
150
149
  # * <tt>:in</tt> - A synonym(or alias) for :within.
151
150
  # * <tt>:allow_nil</tt> - when supplied, validates if it allows nil or not.
152
151
  # * <tt>:allow_blank</tt> - when supplied, validates if it allows blank or not.
153
- # * <tt>:short_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
152
+ # * <tt>:too_short</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt> when attribute is too short.
154
153
  # Regexp, string or symbol. Default = <tt>I18n.translate('activerecord.errors.messages.too_short') % range.first</tt>
155
- # * <tt>:long_message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>.
154
+ # * <tt>:too_long</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt> when attribute is too long.
156
155
  # Regexp, string or symbol. Default = <tt>I18n.translate('activerecord.errors.messages.too_long') % range.last</tt>
157
- # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt> only when :minimum, :maximum or :is is given.
156
+ # * <tt>:wrong_length</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt> when attribute is the wrong length.
157
+ # Regexp, string or symbol. Default = <tt>I18n.translate('activerecord.errors.messages.wrong_length') % range.last</tt>
158
+ # * <tt>:message</tt> - value the test expects to find in <tt>errors.on(:attribute)</tt>. When supplied overwrites :too_short, :too_long and :wrong_length.
158
159
  # Regexp, string or symbol. Default = <tt>I18n.translate('activerecord.errors.messages.wrong_length') % value</tt>
159
160
  #
160
161
  # Example:
@@ -62,14 +62,26 @@ module Remarkable # :nodoc:
62
62
  assert_bad_or_good_if_key(:only_integer, valid_value_for_test.to_f, message, default_message_for(:number))
63
63
  end
64
64
 
65
+ # In ActiveRecord, when we supply :even, does not matter the value, it
66
+ # considers that should odd values should be accepted.
67
+ #
65
68
  def allow_even?
66
- message = "allow even values for #{@attribute}"
67
- assert_bad_or_good_if_key(:even, valid_value_for_test + 1, message, default_message_for(:even))
69
+ return true unless @options[:even]
70
+ return true if bad?(even_valid_value_for_test + 1, default_message_for(:even))
71
+
72
+ message = "allow non-even values for #{@attribute}"
73
+ return false
68
74
  end
69
75
 
76
+ # In ActiveRecord, when we supply :odd, does not matter the value, it
77
+ # considers that should odd values should be accepted.
78
+ #
70
79
  def allow_odd?
71
- message = "allow odd values for #{@attribute}"
72
- assert_bad_or_good_if_key(:odd, even_valid_value_for_test, message, default_message_for(:odd))
80
+ return true unless @options[:odd]
81
+ return true if bad?(even_valid_value_for_test, default_message_for(:odd))
82
+
83
+ message = "allow non-odd values for #{@attribute}"
84
+ return false
73
85
  end
74
86
 
75
87
  def equal_to?(key, add = 0)
@@ -8,20 +8,20 @@ Macro that creates a test asserting that the controller assigned to each of the
8
8
 
9
9
  Options:
10
10
 
11
- * :class - The expected class of the instance variable being checked.
12
- * :equals - A string or a proc which is evaluated and compared with the instance variable being checked.
11
+ * :with - A string or a proc which is evaluated and compared with the instance variable being checked.
12
+ * :with_kind_of - The expected class of the instance variable being checked.
13
13
 
14
14
  Example:
15
15
 
16
16
  <pre><code> should_assign_to :user, :posts
17
- should_assign_to :user, :class => User
18
- should_assign_to :user, :equals => proc { @user }
17
+ should_assign_to :user, :with_kind_of => User
18
+ should_assign_to :user, :with => proc { @user }
19
19
  should_assign_to(:user){ @user }
20
20
  should_not_assign_to :user, :posts
21
21
 
22
22
  it { should assign_to(:user, :posts) }
23
- it { should assign_to(:user, :class => User) }
24
- it { should assign_to(:user, :equals => @user) }
23
+ it { should assign_to(:user, :with_kind_of => User) }
24
+ it { should assign_to(:user, :with => @user) }
25
25
  it { should_not assign_to(:user, :posts) }</code></pre>
26
26
 
27
27
  h2. filter_params
@@ -3,6 +3,8 @@ module Remarkable # :nodoc:
3
3
  module Matchers # :nodoc:
4
4
  class AssignMatcher < Remarkable::Matcher::Base
5
5
  include Remarkable::Controller::Helpers
6
+
7
+ optional :with, :with_kind_of
6
8
 
7
9
  def initialize(*names, &block)
8
10
  @options = names.extract_options!
@@ -10,6 +12,11 @@ module Remarkable # :nodoc:
10
12
  @options[:equals] = block if block_given?
11
13
 
12
14
  warn "[DEPRECATION] Strings given in :equals to should_assign_to won't be evaluated anymore. You can give procs or use blocks instead." if @options[:equals].is_a?(String)
15
+ warn "[DEPRECATION] :equals option is deprecated in should_assign_to. Please give :with instead." if @options[:equals]
16
+ warn "[DEPRECATION] :class option is deprecated in should_assign_to. Please give :with_kind_of instead." if @options[:class]
17
+
18
+ @options[:with] ||= @options.delete(:equals)
19
+ @options[:with_kind_of] ||= @options.delete(:class)
13
20
  end
14
21
 
15
22
  def matches?(subject)
@@ -25,8 +32,8 @@ module Remarkable # :nodoc:
25
32
 
26
33
  def description
27
34
  description = "assign @#{@names.to_sentence}"
28
- description << " as class #{@options[:class]}" if @options[:class]
29
- description << " which is equal to #{@options[:equals]}" if @options[:equals]
35
+ description << " as class #{@options[:with_kind_of]}" if @options[:with_kind_of]
36
+ description << " which is equal to #{@options[:with]}" if @options[:with]
30
37
  description
31
38
  end
32
39
 
@@ -49,22 +56,22 @@ module Remarkable # :nodoc:
49
56
  end
50
57
 
51
58
  def is_kind_of?
52
- return true unless @options[:class]
53
- return true if @assigned_value.kind_of?(@options[:class])
59
+ return true unless @options[:with_kind_of]
60
+ return true if @assigned_value.kind_of?(@options[:with_kind_of])
54
61
 
55
- @missing = "@#{@name} is not a kind of #{@options[:class]}"
62
+ @missing = "@#{@name} is not a kind of #{@options[:with_kind_of]}"
56
63
  return false
57
64
  end
58
65
 
59
66
  def is_equals_expected_value?
60
- return true unless @options[:equals]
67
+ return true unless @options[:with]
61
68
 
62
- expected_value = if @options[:equals].is_a?(String)
63
- @spec.instance_eval(@options[:equals]) rescue @options[:equals]
64
- elsif @options[:equals].is_a?(Proc)
65
- @spec.instance_eval &@options[:equals]
69
+ expected_value = if @options[:with].is_a?(String)
70
+ @spec.instance_eval(@options[:with]) rescue @options[:with]
71
+ elsif @options[:with].is_a?(Proc)
72
+ @spec.instance_eval &@options[:with]
66
73
  else
67
- @options[:equals]
74
+ @options[:with]
68
75
  end
69
76
  return true if @assigned_value == expected_value
70
77
 
@@ -74,8 +81,8 @@ module Remarkable # :nodoc:
74
81
 
75
82
  def expectation
76
83
  expectation = "assign @#{@name}"
77
- expectation << " as class #{@options[:class]}" if @options[:class]
78
- expectation << " which is equal to #{@options[:equals].inspect}" if @options[:equals] && !@options[:equals].is_a?(Proc)
84
+ expectation << " as class #{@options[:with_kind_of]}" if @options[:with_kind_of]
85
+ expectation << " which is equal to #{@options[:with].inspect}" if @options[:with] && !@options[:with].is_a?(Proc)
79
86
  expectation
80
87
  end
81
88
 
data/remarkable.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{remarkable}
5
- s.version = "2.2.9"
5
+ s.version = "2.2.10"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Carlos Brando", "Jos\303\251 Valim", "Diego Carrion"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carlosbrando-remarkable
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.9
4
+ version: 2.2.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Carlos Brando