Sha256: c4388632553d81a2801816ab6abf6b1e2bd66bb8b1c576798d6492c8a5e2af4c

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

#typed: strict

require 'sorbet-runtime'

module DVLA
  module Atlas
    class Artefacts
      extend T::Sig

      sig { params(vargs: T.any(String, Symbol), kwargs: T.untyped).void }
      def define_fields(*vargs, **kwargs)
        vargs.each do |attr|
          initialise_fields(attr)
        end

        kwargs.each_pair do |key, value|
          initialise_fields(key)
          send(:"#{key}=", value) # As an initial value has been passed for this field, we want to set it using the newly defined setter
        end
      end

    private

      sig { params(name: T.any(String, Symbol)).void }
      def initialise_fields(name)
        # Create the history of the newly defined field. This will start out empty. There is no public way to set this so
        # no need to expose it by defining a method.
        instance_variable_set(:"@#{name}_history", [])

        # Create the getter for the new field, and ensure that it is run upon call if it is a proc.
        define_singleton_method :"#{name}" do
          value = instance_variable_get(:"@#{name}")
          value.respond_to?(:call) ? value.call : value
        end

        # Define a getter for the history of the new field
        define_singleton_method :"#{name}_history" do
          instance_variable_get(:"@#{name}_history")
        end

        # Define the setter for the new field. This also pushes the current value of the field into the history unless
        # it is currently nil and the history is empty. This is to prevent each history list starting with a nil entry
        # as all fields are nil when defined.
        define_singleton_method :"#{name}=" do |arg|
          current_value = send(:"#{name}")
          send(:"#{name}_history").push(current_value) unless send(:"#{name}_history").empty? && current_value.nil?
          instance_variable_set(:"@#{name}", arg)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dvla-atlas-1.1.0 lib/dvla/atlas/artefacts.rb