Class: Validation::Rule::TagCount

Inherits:
Object
  • Object
show all
Defined in:
lib/diaspora_federation/validators/rules/tag_count.rb

Overview

Rule for validating the number of tags in a string. Only the “#” characters will be counted. The string can be nil.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ TagCount

Creates a new rule for a maximum tag count validation

Parameters:

  • params (Hash)

Options Hash (params):

  • :maximum (Integer)

    maximum allowed tag count



16
17
18
19
20
21
22
# File 'lib/diaspora_federation/validators/rules/tag_count.rb', line 16

def initialize(params)
  unless params.include?(:maximum) && params[:maximum].is_a?(Integer)
    raise ArgumentError, "A number has to be specified for :maximum"
  end

  @params = params
end

Instance Attribute Details

#paramsHash (readonly)

This rule must have a maximum param.

Returns:

  • (Hash)

    params



11
12
13
# File 'lib/diaspora_federation/validators/rules/tag_count.rb', line 11

def params
  @params
end

Instance Method Details

#error_keySymbol

The error key for this rule

Returns:

  • (Symbol)

    error key



26
27
28
# File 'lib/diaspora_federation/validators/rules/tag_count.rb', line 26

def error_key
  :tag_count
end

#valid_value?(value) ⇒ Boolean

Determines if value doesn’t have more than maximum tags

Returns:



31
32
33
# File 'lib/diaspora_federation/validators/rules/tag_count.rb', line 31

def valid_value?(value)
  value.nil? || value.count("#") <= params[:maximum]
end