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



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)

Since:

  • 1.0.0



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

def slug
  @slug
end

Instance Attribute Details

#filtersHash{Symbol => Filter} (readonly)

Since:

  • 1.0.0



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

def filters
  @filters
end

#nameSymbol (readonly)

Since:

  • 1.0.0



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

def name
  @name
end

#optionsHash{Symbol => Object} (readonly)

Since:

  • 1.0.0



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

Raises:

See Also:

Since:

  • 1.0.0



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

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

Instance Method Details

#cast(value) ⇒ Object

Raises:

Since:

  • 1.0.0



168
169
170
171
172
173
174
175
176
177
# File 'lib/active_interaction/filter.rb', line 168

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

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

#clean(value) ⇒ 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:

Since:

  • 1.0.0



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

def clean(value)
  value = cast(value)
  if value.nil?
    default
  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



192
193
194
# File 'lib/active_interaction/filter.rb', line 192

def database_column_type
  :string
end

#defaultObject

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:

Since:

  • 1.0.0



123
124
125
126
127
128
129
130
131
132
# File 'lib/active_interaction/filter.rb', line 123

def default
  fail NoDefaultError, name unless default?

  value = raw_default
  fail InvalidValueError if value.is_a?(GroupedInput)

  cast(value)
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



155
156
157
# File 'lib/active_interaction/filter.rb', line 155

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

#descString?

Get the description.

Examples:

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

Since:

  • 1.0.0



141
142
143
# File 'lib/active_interaction/filter.rb', line 141

def desc
  options[:desc]
end