Class: ActiveInteraction::Filter

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

Overview

Describes an input filter for an interaction.

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.

Parameters:

  • name (Symbol)
  • options (Hash{Symbol => Object}) (defaults to: {})

Options Hash (options):

  • :default (Object)

    Fallback value to use when given ‘nil`.



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

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

  instance_eval(&block) if block_given?
end

Class Attribute Details

.slugSymbol (readonly)

Returns:

  • (Symbol)


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

def slug
  @slug
end

Instance Attribute Details

#filtersHash{Symbol => Filter} (readonly)

Returns:



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

def filters
  @filters
end

#nameSymbol (readonly)

Returns:

  • (Symbol)


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

def name
  @name
end

#optionsHash{Symbol => Object} (readonly)

Returns:

  • (Hash{Symbol => Object})


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

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

Parameters:

  • slug (Symbol)

Returns:

  • (Class)

Raises:

See Also:



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

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

Instance Method Details

#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, nil)
# => ActiveInteraction::MissingValueError: example
ActiveInteraction::Filter.new(:example).clean(0, nil)
# => ActiveInteraction::InvalidValueError: example: 0
ActiveInteraction::Filter.new(:example, default: nil).clean(nil, nil)
# => nil
ActiveInteraction::Filter.new(:example, default: 0).clean(nil, nil)
# => ActiveInteraction::InvalidDefaultError: example: 0

Parameters:

  • value (Object)
  • context (Base, nil)

Returns:

  • (Object)

Raises:



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

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

Returns:

  • (Symbol)

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



177
178
179
# File 'lib/active_interaction/filter.rb', line 177

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

Parameters:

  • context (Base, nil) (defaults to: nil)

Returns:

  • (Object)

Raises:



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

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 => e
  raise InvalidDefaultError, "#{name}: #{value.inspect} (#{e})"
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

Returns:

  • (Boolean)


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

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

#descString?

Get the description.

Examples:

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

Returns:

  • (String, nil)


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

def desc
  options[:desc]
end