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 (Fixnum)

    maximum allowed tag count



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

def initialize(params)
  unless params.include?(:maximum) && params[:maximum].is_a?(Fixnum)
    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



9
10
11
# File 'lib/diaspora_federation/validators/rules/tag_count.rb', line 9

def params
  @params
end

Instance Method Details

#error_keySymbol

The error key for this rule

Returns:

  • (Symbol)

    error key



24
25
26
# File 'lib/diaspora_federation/validators/rules/tag_count.rb', line 24

def error_key
  :tag_count
end

#valid_value?(value) ⇒ Boolean

Determines if value doesn’t have more than maximum tags

Returns:



29
30
31
# File 'lib/diaspora_federation/validators/rules/tag_count.rb', line 29

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