Class: ActiveInteraction::Filter

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

Overview

Describes an input filter for an interaction.

Defined Under Namespace

Classes: Column, Error

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

#accepts_grouped_inputs?Boolean

Tells whether or not the filter accepts a group of parameters to form a single input.

Examples:

ActiveInteraction::TimeFilter.new(Time.now).accepts_grouped_inputs?
# => true
ActiveInteraction::Filter.new(:example).accepts_grouped_inputs?
# => false

Returns:

  • (Boolean)


183
184
185
# File 'lib/active_interaction/filter.rb', line 183

def accepts_grouped_inputs?
  false
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`.



168
169
170
# File 'lib/active_interaction/filter.rb', line 168

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:



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/active_interaction/filter.rb', line 114

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

  value = raw_default(context)
  raise InvalidDefaultError, "#{name}: #{value.inspect}" if value.is_a?(GroupedInput)

  if value.nil?
    nil
  else
    default = process(value, context)
    if default.errors.any? && default.errors.first.is_a?(Filter::Error)
      raise InvalidDefaultError, "#{name}: #{value.inspect}"
    end

    default.value
  end
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)


153
154
155
# File 'lib/active_interaction/filter.rb', line 153

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

#descString?

Get the description.

Examples:

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

Returns:

  • (String, nil)


139
140
141
# File 'lib/active_interaction/filter.rb', line 139

def desc
  options[:desc]
end

#process(value, context) ⇒ Input, ...

Processes the input through the filter and returns a variety of data

about the input.

Examples:

input = ActiveInteraction::Filter.new(:example, default: nil).process(nil, nil)
input.value
# => nil

Parameters:

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

Returns:

Raises:



90
91
92
93
94
# File 'lib/active_interaction/filter.rb', line 90

def process(value, context)
  value, error = cast(value, context)

  Input.new(self, value: value, error: error)
end