Class: Foobara::Value::Caster

Inherits:
Transformer show all
Defined in:
foobara-0.0.110/projects/value/src/caster.rb

Overview

TODO: do we really need these?? Can’t just use a transformer?

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 Transformer

error_classes, #process_value

Methods inherited from Processor

#always_applicable?, #attribute_name, #build_error, default_declaration_data, #dup_processor, error_class, error_classes, #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, #process_value!, processor_name, 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



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

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

.foobara_manifestObject



6
7
8
# File 'foobara-0.0.110/projects/value/src/caster.rb', line 6

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

.requires_declaration_data?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'foobara-0.0.110/projects/value/src/caster.rb', line 10

def requires_declaration_data?
  false
end

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



18
19
20
21
22
23
24
25
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
# File 'foobara-0.0.110/projects/value/src/caster.rb', line 18

def subclass(name: nil, **options)
  arity_zero = %i[name applies_message]
  arity_one = %i[applicable? cast]
  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

#applicable?(_value) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
# File 'foobara-0.0.110/projects/value/src/caster.rb', line 61

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

#applies_messageObject



67
68
69
70
71
# File 'foobara-0.0.110/projects/value/src/caster.rb', line 67

def applies_message
  # :nocov:
  raise "subclass responsibility"
  # :nocov:
end

#cast(_value) ⇒ Object



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

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

#transform(value) ⇒ Object



73
74
75
# File 'foobara-0.0.110/projects/value/src/caster.rb', line 73

def transform(value)
  cast(value)
end