Class: ActiveInteraction::Filter
- Inherits:
-
Object
- Object
- ActiveInteraction::Filter
- 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.
Direct Known Subclasses
AbstractDateTimeFilter, AbstractNumericFilter, ArrayFilter, BooleanFilter, FileFilter, HashFilter, InterfaceFilter, ObjectFilter, RecordFilter, StringFilter, SymbolFilter
Defined Under Namespace
Class Attribute Summary collapse
- .slug ⇒ Symbol readonly
Instance Attribute Summary collapse
- #filters ⇒ Hash{Symbol => Filter} readonly
- #name ⇒ Symbol readonly
- #options ⇒ Hash{Symbol => Object} readonly
Class Method Summary collapse
-
.factory(slug) ⇒ Class
Get the filter associated with a symbol.
Instance Method Summary collapse
-
#accepts_grouped_inputs? ⇒ Boolean
Tells whether or not the filter accepts a group of parameters to form a single input.
-
#database_column_type ⇒ Symbol
Gets the type of database column that would represent the filter data.
-
#default(context = nil) ⇒ Object
Get the default value.
-
#default? ⇒ Boolean
Tells if this filter has a default value.
-
#desc ⇒ String?
Get the description.
-
#initialize(name, options = {}, &block) ⇒ Filter
constructor
A new instance of Filter.
-
#process(value, context) ⇒ Input, ...
Processes the input through the filter and returns a variety of data about the input.
Constructor Details
#initialize(name, options = {}, &block) ⇒ Filter
Returns a new instance of Filter.
68 69 70 71 72 73 74 |
# File 'lib/active_interaction/filter.rb', line 68 def initialize(name, = {}, &block) @name = name @options = .dup @filters = {} instance_eval(&block) if block_given? end |
Class Attribute Details
.slug ⇒ Symbol (readonly)
32 33 34 |
# File 'lib/active_interaction/filter.rb', line 32 def slug @slug end |
Instance Attribute Details
#filters ⇒ Hash{Symbol => Filter} (readonly)
20 21 22 |
# File 'lib/active_interaction/filter.rb', line 20 def filters @filters end |
#name ⇒ Symbol (readonly)
23 24 25 |
# File 'lib/active_interaction/filter.rb', line 23 def name @name end |
#options ⇒ Hash{Symbol => Object} (readonly)
26 27 28 |
# File 'lib/active_interaction/filter.rb', line 26 def @options end |
Class Method Details
.factory(slug) ⇒ Class
Get the filter associated with a symbol.
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.
183 184 185 |
# File 'lib/active_interaction/filter.rb', line 183 def accepts_grouped_inputs? false end |
#database_column_type ⇒ Symbol
Gets the type of database column that would represent the filter data.
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.
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.
153 154 155 |
# File 'lib/active_interaction/filter.rb', line 153 def default? .key?(:default) end |
#desc ⇒ String?
Get the description.
139 140 141 |
# File 'lib/active_interaction/filter.rb', line 139 def desc [:desc] end |
#process(value, context) ⇒ Input, ...
Processes the input through the filter and returns a variety of data
about the input.
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 |