Sha256: 13291a94232325af2acdc36c7cbe80a6f712844eefdadb6e475f1bf3d43ae2c0

Contents?: true

Size: 1.96 KB

Versions: 20

Compression:

Stored size: 1.96 KB

Contents

module Foobara
  module Value
    # TODO: do we really need these??  Can't just use a transformer?
    class Caster < Transformer
      class << self
        def foobara_manifest(to_include:)
          super.merge(processor_type: :caster)
        end

        def requires_declaration_data?
          false
        end

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

        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
      end

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

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

      def transform(value)
        cast(value)
      end

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

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
foobara-0.0.20 projects/value/src/caster.rb
foobara-0.0.19 projects/value/src/caster.rb
foobara-0.0.18 projects/value/src/caster.rb
foobara-0.0.17 projects/value/src/caster.rb
foobara-0.0.16 projects/value/src/caster.rb
foobara-0.0.15 projects/value/src/caster.rb
foobara-0.0.14 projects/value/src/caster.rb
foobara-0.0.13 projects/value/src/caster.rb
foobara-0.0.12 projects/value/src/caster.rb
foobara-0.0.11 projects/value/src/caster.rb
foobara-0.0.10 projects/value/src/caster.rb
foobara-0.0.9 projects/value/src/caster.rb
foobara-0.0.8 projects/value/src/caster.rb
foobara-0.0.7 projects/value/src/caster.rb
foobara-0.0.6 projects/value/src/caster.rb
foobara-0.0.5 projects/value/src/caster.rb
foobara-0.0.4 projects/value/src/caster.rb
foobara-0.0.3 projects/value/src/caster.rb
foobara-0.0.2 projects/value/src/caster.rb
foobara-0.0.1 projects/value/src/caster.rb