Class: MiniDefender::Rules::StartingWith
Class Method Summary
collapse
Instance Method Summary
collapse
#active?, available?, #bails?, #coerce, #default_value, #defaults?, #error_message, #excluded?, #force_coerce?, #implicit?, #priority, #stops?, #with_message
Constructor Details
#initialize(fragments) ⇒ StartingWith
Returns a new instance of StartingWith.
4
5
6
7
8
9
10
|
# File 'lib/mini_defender/rules/starting_with.rb', line 4
def initialize(fragments)
unless fragments.is_a?(Array) && !fragments.empty? && fragments.all? { |f| f.is_a?(String) }
raise ArgumentError, 'Expected an array of strings.'
end
@fragments = fragments
end
|
Class Method Details
.make(args) ⇒ Object
16
17
18
|
# File 'lib/mini_defender/rules/starting_with.rb', line 16
def self.make(args)
new(args)
end
|
.signature ⇒ Object
12
13
14
|
# File 'lib/mini_defender/rules/starting_with.rb', line 12
def self.signature
'starting_with'
end
|
Instance Method Details
#message(attribute, value, validator) ⇒ Object
24
25
26
27
28
29
30
|
# File 'lib/mini_defender/rules/starting_with.rb', line 24
def message(attribute, value, validator)
if @fragments.length == 1
"The value should start with #{@fragments[0]}."
else
"The value should start with one of the following #{@fragments.join(', ')}."
end
end
|
#passes?(attribute, value, validator) ⇒ Boolean
20
21
22
|
# File 'lib/mini_defender/rules/starting_with.rb', line 20
def passes?(attribute, value, validator)
@fragments.any? { |f| value.to_s.start_with?(f) }
end
|