Class: ActiveInteraction::Filter

Inherits:
Object
  • Object
show all
Defined in:
lib/active_interaction/filter.rb

Overview

Describes an input filter for an interaction.

Since:

  • 1.0.0

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ Filter

Returns a new instance of Filter.

Options Hash (options):

  • :default (Object)

    Fallback value to use when given ‘nil`.

Since:

  • 1.0.0

Parameters:

  • (defaults to: {})


69
70
71
72
73
74
75
# File 'lib/active_interaction/filter.rb', line 69

def initialize(name, options = {}, &block)
  @name = name
  @options = options.dup
  @filters = {}

  instance_eval(&block) if block_given?
end

Class Attribute Details

.slugSymbol (readonly)

Since:

  • 1.0.0

Returns:



33
34
35
# File 'lib/active_interaction/filter.rb', line 33

def slug
  @slug
end

Instance Attribute Details

#filtersHash{Symbol => Filter} (readonly)

Since:

  • 1.0.0

Returns:



21
22
23
# File 'lib/active_interaction/filter.rb', line 21

def filters
  @filters
end

#nameSymbol (readonly)

Since:

  • 1.0.0

Returns:



24
25
26
# File 'lib/active_interaction/filter.rb', line 24

def name
  @name
end

#optionsHash{Symbol => Object} (readonly)

Since:

  • 1.0.0

Returns:



27
28
29
# File 'lib/active_interaction/filter.rb', line 27

def options
  @options
end

Class Method Details

.factory(slug) ⇒ Class

Get the filter associated with a symbol.

Examples:

ActiveInteraction::Filter.factory(:boolean)
# => ActiveInteraction::BooleanFilter
ActiveInteraction::Filter.factory(:invalid)
# => ActiveInteraction::MissingFilterError: :invalid

Raises:

  • If the slug doesn’t map to a filter.

See Also:

Since:

  • 1.0.0

Parameters:

Returns:



51
52
53
# File 'lib/active_interaction/filter.rb', line 51

def factory(slug)
  CLASSES.fetch(slug) { raise MissingFilterError, slug.inspect }
end

Instance Method Details

#cast(value, _interaction) ⇒ Object

Raises:

  • If the value is missing and there is no default.

  • If the value is invalid.

Since:

  • 1.0.0

Parameters:

Returns:



175
176
177
178
179
180
181
182
183
184
# File 'lib/active_interaction/filter.rb', line 175

def cast(value, _interaction)
  case value
  when NilClass
    raise MissingValueError, name unless default?

    nil
  else
    raise InvalidValueError, "#{name}: #{describe(value)}"
  end
end

#clean(value, context) ⇒ Object

Convert a value into the expected type. If no value is given, fall back

to the default value.

Examples:

ActiveInteraction::Filter.new(:example).clean(nil)
# => ActiveInteraction::MissingValueError: example
ActiveInteraction::Filter.new(:example).clean(0)
# => ActiveInteraction::InvalidValueError: example: 0
ActiveInteraction::Filter.new(:example, default: nil).clean(nil)
# => nil
ActiveInteraction::Filter.new(:example, default: 0).clean(nil)
# => ActiveInteraction::InvalidDefaultError: example: 0

Raises:

  • If the value is missing and there is no default.

  • If the value is invalid.

  • If the default is missing.

  • If the default is invalid.

Since:

  • 1.0.0

Parameters:

Returns:



100
101
102
103
104
105
106
107
# File 'lib/active_interaction/filter.rb', line 100

def clean(value, context)
  value = cast(value, context)
  if value.nil?
    default(context)
  else
    value
  end
end

#database_column_typeSymbol

Gets the type of database column that would represent the filter data.

Examples:

ActiveInteraction::TimeFilter.new(Time.now).database_column_type
# => :datetime
ActiveInteraction::Filter.new(:example).database_column_type
# => :string

Since:

  • 1.2.0

Returns:

  • A database column type. If no sensible mapping exists, returns ‘:string`.



199
200
201
# File 'lib/active_interaction/filter.rb', line 199

def database_column_type
  :string
end

#default(context = nil) ⇒ Object

Get the default value.

Examples:

ActiveInteraction::Filter.new(:example).default
# => ActiveInteraction::NoDefaultError: example
ActiveInteraction::Filter.new(:example, default: nil).default
# => nil
ActiveInteraction::Filter.new(:example, default: 0).default
# => ActiveInteraction::InvalidDefaultError: example: 0

Raises:

  • If the default is missing.

  • If the default is invalid.

Since:

  • 1.0.0

Parameters:

  • (defaults to: nil)

Returns:



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/active_interaction/filter.rb', line 127

def default(context = nil)
  raise NoDefaultError, name unless default?

  value = raw_default(context)
  raise InvalidValueError if value.is_a?(GroupedInput)

  cast(value, context)
rescue InvalidNestedValueError => error
  raise InvalidDefaultError, "#{name}: #{value.inspect} (#{error})"
rescue InvalidValueError, MissingValueError
  raise InvalidDefaultError, "#{name}: #{value.inspect}"
end

#default?Boolean

Tells if this filter has a default value.

Examples:

ActiveInteraction::Filter.new(:example).default?
# => false
ActiveInteraction::Filter.new(:example, default: nil).default?
# => true

Since:

  • 1.0.0

Returns:



161
162
163
# File 'lib/active_interaction/filter.rb', line 161

def default?
  options.key?(:default)
end

#descString?

Get the description.

Examples:

ActiveInteraction::Filter.new(:example, desc: 'Description!').desc
# => "Description!"

Since:

  • 1.0.0

Returns:



147
148
149
# File 'lib/active_interaction/filter.rb', line 147

def desc
  options[:desc]
end