Class: ActiveInteraction::Filter
- Inherits:
-
Object
- Object
- ActiveInteraction::Filter
- Defined in:
- lib/active_interaction/filter.rb
Overview
Describes an input filter for an interaction.
Direct Known Subclasses
AbstractDateTimeFilter, AbstractNumericFilter, ArrayFilter, BooleanFilter, FileFilter, HashFilter, InterfaceFilter, ObjectFilter, RecordFilter, StringFilter, SymbolFilter
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
-
#clean(value, context) ⇒ Object
Convert a value into the expected type.
-
#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.
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 = .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 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
#clean(value, context) ⇒ Object
Convert a value into the expected type. If no value is given, fall back
to the default value.
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_type ⇒ Symbol
Gets the type of database column that would represent the filter data.
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.
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.
162 163 164 |
# File 'lib/active_interaction/filter.rb', line 162 def default? .key?(:default) end |
#desc ⇒ String?
Get the description.
148 149 150 |
# File 'lib/active_interaction/filter.rb', line 148 def desc [:desc] end |