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
AbstractFilter, ArrayFilter, BooleanFilter, 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
- #cast(value, _interaction) ⇒ Object
-
#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.
69 70 71 72 73 74 75 |
# File 'lib/active_interaction/filter.rb', line 69 def initialize(name, = {}, &block) @name = name @options = .dup @filters = {} instance_eval(&block) if block_given? end |
Class Attribute Details
.slug ⇒ Symbol (readonly)
33 34 35 |
# File 'lib/active_interaction/filter.rb', line 33 def slug @slug end |
Instance Attribute Details
#filters ⇒ Hash{Symbol => Filter} (readonly)
21 22 23 |
# File 'lib/active_interaction/filter.rb', line 21 def filters @filters end |
#name ⇒ Symbol (readonly)
24 25 26 |
# File 'lib/active_interaction/filter.rb', line 24 def name @name end |
#options ⇒ Hash{Symbol => Object} (readonly)
27 28 29 |
# File 'lib/active_interaction/filter.rb', line 27 def @options end |
Class Method Details
.factory(slug) ⇒ Class
Get the filter associated with a symbol.
51 52 53 |
# File 'lib/active_interaction/filter.rb', line 51 def factory(slug) CLASSES.fetch(slug) { raise MissingFilterError, slug.inspect } end |
Instance Method Details
#cast(value, _interaction) ⇒ Object
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/active_interaction/filter.rb', line 175 def cast(value, _interaction) case value when NilClass raise MissingValueError, name unless default? nil else raise InvalidValueError, "#{name}: #{describe(value)}" end end |
#clean(value, context) ⇒ Object
Convert a value into the expected type. If no value is given, fall back
to the default value.
100 101 102 103 104 105 106 107 |
# File 'lib/active_interaction/filter.rb', line 100 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.
199 200 201 |
# File 'lib/active_interaction/filter.rb', line 199 def database_column_type :string end |
#default(context = nil) ⇒ Object
Get the default value.
127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/active_interaction/filter.rb', line 127 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 => error raise InvalidDefaultError, "#{name}: #{value.inspect} (#{error})" rescue InvalidValueError, MissingValueError raise InvalidDefaultError, "#{name}: #{value.inspect}" end |
#default? ⇒ Boolean
Tells if this filter has a default value.
161 162 163 |
# File 'lib/active_interaction/filter.rb', line 161 def default? .key?(:default) end |
#desc ⇒ String?
Get the description.
147 148 149 |
# File 'lib/active_interaction/filter.rb', line 147 def desc [:desc] end |