Class: Foobara::Value::Processor::Selection
- Inherits:
-
Multi
- Object
- Foobara::Value::Processor
- Multi
- Foobara::Value::Processor::Selection
- Defined in:
- foobara-0.0.130/projects/value/src/processor/selection.rb
Direct Known Subclasses
Defined Under Namespace
Classes: MoreThanOneApplicableProcessorError, NoApplicableProcessorError
Instance Attribute Summary collapse
-
#enforce_unique ⇒ Object
Returns the value of attribute enforce_unique.
-
#error_if_none_applicable ⇒ Object
Returns the value of attribute error_if_none_applicable.
Attributes inherited from Multi
Attributes inherited from Foobara::Value::Processor
#created_in_namespace, #declaration_data, #parent_declaration_data
Class Method Summary collapse
Instance Method Summary collapse
- #always_applicable? ⇒ Boolean
-
#error_context(value) ⇒ Object
This is a problem…
- #error_message(value) ⇒ Object
-
#initialize(enforce_unique: true, error_if_none_applicable: true) ⇒ Selection
constructor
A new instance of Selection.
-
#process_value(value) ⇒ Object
TODO: move applies_message usage here from casting processor.
- #process_value!(value) ⇒ Object
- #processor_for(value) ⇒ Object
- #processor_for!(value) ⇒ Object
Methods inherited from Multi
#applicable?, #error_classes, #possible_errors, #processor_names, #register, requires_declaration_data?
Methods inherited from Foobara::Value::Processor
#applicable?, #attribute_name, #build_error, default_declaration_data, #dup_processor, error_class, error_classes, #error_path, #foobara_manifest, #inspect, instance, #method_missing, #name, new_with_agnostic_args, #possible_errors, #priority, #process_outcome, #process_outcome!, processor_name, requires_declaration_data?, requires_parent_declaration_data?, #respond_to_missing?, #runner, symbol
Methods included from IsManifestable
#foobara_domain, #foobara_manifest, #foobara_organization, #scoped_clear_caches
Methods included from Concern
foobara_class_methods_module_for, foobara_concern?, included
Constructor Details
#initialize(enforce_unique: true, error_if_none_applicable: true) ⇒ Selection
Returns a new instance of Selection.
21 22 23 24 25 26 |
# File 'foobara-0.0.130/projects/value/src/processor/selection.rb', line 21 def initialize(*, enforce_unique: true, error_if_none_applicable: true, **) self.enforce_unique = enforce_unique self.error_if_none_applicable = error_if_none_applicable super(*, **) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Foobara::Value::Processor
Instance Attribute Details
#enforce_unique ⇒ Object
Returns the value of attribute enforce_unique.
19 20 21 |
# File 'foobara-0.0.130/projects/value/src/processor/selection.rb', line 19 def enforce_unique @enforce_unique end |
#error_if_none_applicable ⇒ Object
Returns the value of attribute error_if_none_applicable.
19 20 21 |
# File 'foobara-0.0.130/projects/value/src/processor/selection.rb', line 19 def error_if_none_applicable @error_if_none_applicable end |
Class Method Details
.foobara_manifest ⇒ Object
12 13 14 15 16 |
# File 'foobara-0.0.130/projects/value/src/processor/selection.rb', line 12 def # :nocov: super.merge(processor_type: :selection) # :nocov: end |
Instance Method Details
#always_applicable? ⇒ Boolean
82 83 84 |
# File 'foobara-0.0.130/projects/value/src/processor/selection.rb', line 82 def always_applicable? true end |
#error_context(value) ⇒ Object
This is a problem… how do we know a base class won’t call this for a different error??
92 93 94 95 96 97 |
# File 'foobara-0.0.130/projects/value/src/processor/selection.rb', line 92 def error_context(value) { processor_names:, value: } end |
#error_message(value) ⇒ Object
86 87 88 89 |
# File 'foobara-0.0.130/projects/value/src/processor/selection.rb', line 86 def (value) # TODO: should override this message so we say registry or caster or whatever based on the situation "Could not find processor that is applicable for #{value}" end |
#process_value(value) ⇒ Object
TODO: move applies_message usage here from casting processor
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'foobara-0.0.130/projects/value/src/processor/selection.rb', line 29 def process_value(value) outcome = processor_for(value) if outcome.success? processor = outcome.result unless processor.nil? outcome = processor.process_value(value) end end outcome end |
#process_value!(value) ⇒ Object
43 44 45 |
# File 'foobara-0.0.130/projects/value/src/processor/selection.rb', line 43 def process_value!(value) process_value(value).result! end |
#processor_for(value) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'foobara-0.0.130/projects/value/src/processor/selection.rb', line 47 def processor_for(value) processor = if enforce_unique applicable_processors = processors.select { |p| p.applicable?(value) } if applicable_processors.size > 1 return Outcome.error( build_error( value, error_class: MoreThanOneApplicableProcessorError, message: "More than one processor applicable for #{value}", context: error_context(value).merge( applicable_processor_names: applicable_processors.map(&:name) ) ) ) end applicable_processors.first else processors.find { |processor| processor.applicable?(value) } end if processor Outcome.success(processor) elsif error_if_none_applicable Outcome.error(build_error(value, error_class: NoApplicableProcessorError)) else Outcome.success(nil) end end |
#processor_for!(value) ⇒ Object
78 79 80 |
# File 'foobara-0.0.130/projects/value/src/processor/selection.rb', line 78 def processor_for!(value) processor_for(value).result! end |