Class: Right::FilterParameterDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/right/filter_parameter_definition.rb

Overview

Filtering parameter definition

Direct Known Subclasses

FilterUndefinedParameter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ FilterParameterDefinition

Returns a new instance of FilterParameterDefinition.

Parameters:

  • name (String)

    of the field

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :on (Symbol, {Symbol => String}, {Symbol => <String>}) — default: nil

    filter on given relation

  • :predicates (<String>) — default: ALL_PREDICATES

    white-listed predicates

  • :coerce (Proc)

    coercion for the value

  • :validates (Hash{Symbol => any})

    for the value @example

    validates: presence: true, length: { is: 6 }
    


30
31
32
33
34
35
36
37
38
# File 'lib/right/filter_parameter_definition.rb', line 30

def initialize(name, options = {})
  options.assert_valid_keys(:as, :on, :predicates, :coerce, :validates)
  @as = options.fetch(:as, name).to_s
  @name = name.to_s
  @on = options[:on]
  @predicates = Array(options.fetch(:predicates, FilterPredicates.all)).map(&:to_s)
  @coerce = options.fetch(:coerce, ->(v) { v })
  @validations = options.fetch(:validates, {})
end

Instance Attribute Details

#asString (readonly)

If the property name doesn’t match the name in the query string, use the :as option

Returns:

  • (String)


8
9
10
# File 'lib/right/filter_parameter_definition.rb', line 8

def as
  @as
end

#nameString (readonly)

Returns:

  • (String)


11
12
13
# File 'lib/right/filter_parameter_definition.rb', line 11

def name
  @name
end

#onString? (readonly)

Association on which this parameter is defined.

Returns:

  • (String, nil)


15
16
17
# File 'lib/right/filter_parameter_definition.rb', line 15

def on
  @on
end

#predicates<String> (readonly)

White-listed predicates

Returns:

  • (<String>)


19
20
21
# File 'lib/right/filter_parameter_definition.rb', line 19

def predicates
  @predicates
end

#validationsProc (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Proc with defined validations

Returns:

  • (Proc)


77
78
79
# File 'lib/right/filter_parameter_definition.rb', line 77

def validations
  @validations
end

Instance Method Details

#==(other) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/right/filter_parameter_definition.rb', line 47

def ==(other)
  other.is_a?(self.class) &&
    other.name == name &&
    other.as == as &&
    other.on == on &&
    other.predicates == predicates
end

#coerce(value) ⇒ any

Returns coerced value according the definition.

Parameters:

  • value (any)

Returns:

  • (any)

    coerced value according the definition



58
59
60
# File 'lib/right/filter_parameter_definition.rb', line 58

def coerce(value)
  @coerce.call(value)
end

#defined?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/right/filter_parameter_definition.rb', line 66

def defined?
  true
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/right/filter_parameter_definition.rb', line 43

def eql?(other)
  other.is_a?(self.class) && other.name == name
end

#undefined?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/right/filter_parameter_definition.rb', line 70

def undefined?
  !self.defined?
end

#validatorObject



62
63
64
# File 'lib/right/filter_parameter_definition.rb', line 62

def validator
  FilterValueValidator.build(self)
end