Class: Warped::Filter::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/warped/api/filter/base.rb,
lib/warped/api/filter/base/value.rb

Direct Known Subclasses

Boolean, Date, DateTime, Decimal, Integer, String, Time

Defined Under Namespace

Classes: Value

Constant Summary collapse

RELATIONS =
%w[eq neq gt gte lt lte between in not_in starts_with ends_with contains is_null is_not_null].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, strict:, alias_name: nil, **options) ⇒ Base

Returns a new instance of Base.

Parameters:

  • name (String)

    The name of the filter.

  • alias_name (String) (defaults to: nil)

    The alias name of the filter, used for renaming the filter key in the URL params

  • options (Hash)

    The filter options.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
# File 'lib/warped/api/filter/base.rb', line 26

def initialize(name, strict:, alias_name: nil, **options)
  raise ArgumentError, "name cannot be nil" if name.nil?

  @name = name.to_s
  @strict = strict
  @alias_name = alias_name&.to_s
  @options = options
end

Instance Attribute Details

#alias_nameObject (readonly)

Returns the value of attribute alias_name.



12
13
14
# File 'lib/warped/api/filter/base.rb', line 12

def alias_name
  @alias_name
end

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/warped/api/filter/base.rb', line 12

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/warped/api/filter/base.rb', line 12

def options
  @options
end

#strictObject (readonly)

Returns the value of attribute strict.



12
13
14
# File 'lib/warped/api/filter/base.rb', line 12

def strict
  @strict
end

Class Method Details

.kindSymbol?

Returns The filter kind.

Returns:

  • (Symbol, nil)

    The filter kind.



17
18
19
20
21
# File 'lib/warped/api/filter/base.rb', line 17

def self.kind
  filter_type = name.demodulize.underscore.to_sym

  filter_type == :filter ? nil : filter_type
end

Instance Method Details

#cast(value) ⇒ Object

Returns The casted value.

Parameters:

  • relation (Object)

    The filter relation.

Returns:

  • (Object)

    The casted value.

Raises:



48
49
50
51
52
# File 'lib/warped/api/filter/base.rb', line 48

def cast(value)
  return if value.nil?

  value
end

#html_typeString

Returns The HTML input type.

Returns:

  • (String)

    The HTML input type.

Raises:

  • (NotImplementedError)


73
74
75
# File 'lib/warped/api/filter/base.rb', line 73

def html_type
  raise NotImplementedError, "#{self.class.name}#html_type not implemented"
end

#kindSymbol?

Returns The filter kind.

Returns:

  • (Symbol, nil)

    The filter kind.



36
37
38
# File 'lib/warped/api/filter/base.rb', line 36

def kind
  self.class.kind
end

#parameter_nameString

Returns The name to use in the URL params.

Returns:

  • (String)

    The name to use in the URL params.



68
69
70
# File 'lib/warped/api/filter/base.rb', line 68

def parameter_name
  alias_name.presence || name
end

#relation(relation) ⇒ String

Returns The validated filter relation.

Parameters:

  • relation (String)

    The validated filter relation.

Returns:

  • (String)

    The validated filter relation.

Raises:



57
58
59
60
61
62
63
64
65
# File 'lib/warped/api/filter/base.rb', line 57

def relation(relation)
  if valid_relation?(relation)
    relation
  else
    raise RelationError, "Invalid relation: #{relation}" unless strict

    "eq"
  end
end

#relationsArray<String>

Returns The valid filter relations.

Returns:

  • (Array<String>)

    The valid filter relations.



41
42
43
# File 'lib/warped/api/filter/base.rb', line 41

def relations
  self.class::RELATIONS
end