Class: Foobara::Value::Transformer

Inherits:
Processor
  • Object
show all
Defined in:
foobara-0.0.110/projects/value/src/transformer.rb

Defined Under Namespace

Classes: Runner

Instance Attribute Summary

Attributes inherited from Processor

#created_in_namespace, #declaration_data, #parent_declaration_data

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Processor

#always_applicable?, #applicable?, #attribute_name, #build_error, default_declaration_data, #dup_processor, error_class, #error_context, #error_message, #error_path, #foobara_manifest, #initialize, #inspect, instance, #method_missing, #name, new_with_agnostic_args, #possible_errors, #priority, #process_outcome, #process_outcome!, #process_value!, 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

This class inherits a constructor from Foobara::Value::Processor

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Foobara::Value::Processor

Class Method Details

.create(options) ⇒ Object



21
22
23
# File 'foobara-0.0.110/projects/value/src/transformer.rb', line 21

def create(options)
  subclass(**options).instance
end

.error_classesObject



17
18
19
# File 'foobara-0.0.110/projects/value/src/transformer.rb', line 17

def error_classes
  []
end

.foobara_manifestObject



13
14
15
# File 'foobara-0.0.110/projects/value/src/transformer.rb', line 13

def foobara_manifest
  super.merge(processor_type: :transformer)
end

.subclass(name: nil, **options) ⇒ Object

TODO: make transform the first argument for convenience



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'foobara-0.0.110/projects/value/src/transformer.rb', line 26

def subclass(name: nil, **options)
  arity_zero = %i[always_applicable? priority]
  arity_one = %i[applicable? transform]
  allowed = arity_zero + arity_one

  invalid_options = options.keys - allowed

  unless invalid_options.empty?
    # :nocov:
    raise ArgumentError, "Invalid options #{invalid_options} expected only #{allowed}"
    # :nocov:
  end

  name ||= "#{self.name}::Anon#{SecureRandom.hex}"

  unless name.include?(":")
    name = "#{self.name}::#{name}"
  end

  Util.make_class(name, self) do
    arity_one.each do |method_name|
      if options.key?(method_name)
        method = options[method_name]

        define_method method_name do |value|
          method.call(value)
        end
      end
    end

    arity_zero.each do |method_name|
      if options.key?(method_name)
        value = options[method_name]

        define_method method_name do
          value
        end
      end
    end
  end
end

Instance Method Details

#process_value(value) ⇒ Object



75
76
77
78
79
80
81
# File 'foobara-0.0.110/projects/value/src/transformer.rb', line 75

def process_value(value)
  if applicable?(value)
    value = transform(value)
  end

  Outcome.success(value)
end

#transform(_value) ⇒ Object



69
70
71
72
73
# File 'foobara-0.0.110/projects/value/src/transformer.rb', line 69

def transform(_value)
  # :nocov:
  raise "subclass responsibility"
  # :nocov:
end