Sha256: f9b8ff162f13aea00e32843d02bf7b0252ef6c44d00f117c22148eceab2b270c

Contents?: true

Size: 1.56 KB

Versions: 3

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require_relative "../logging/logger"

module Kanal
  module Core
    module Helpers
      # For registered property info,
      # this class is used for future additions,
      # maybe type validations or something
      class ParameterRegistration
        def initialize(readonly)
          @readonly = readonly
        end

        def readonly?
          @readonly
        end
      end

      # Class holds parameter names that are allowed
      # to be used.
      class ParameterRegistrator
        include Logging::Logger

        def initialize
          @parameters_by_name = {}
        end

        # readonly paramaeter means that once it was initialized - it cannot
        # be changed. handy for input parameters populated by interface or
        # whatever
        def register_parameter(name, readonly: false)
          if @parameters_by_name.key? name
            logger.fatal "Attempted to register already registered parameter '#{name}'"

            raise "Parameter named #{name} already registered!"
          end

          logger.debug "Registering parameter '#{name}'"

          registration = ParameterRegistration.new readonly

          @parameters_by_name[name] = registration
        end

        # returns nil if no parameter registered
        def get_parameter_registration_if_exists(name)
          return nil unless @parameters_by_name.key? name

          @parameters_by_name[name]
        end

        def parameter_registered?(name)
          !get_parameter_registration_if_exists(name).nil?
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
kanal-0.8.0 lib/kanal/core/helpers/parameter_registrator.rb
kanal-0.7.0 lib/kanal/core/helpers/parameter_registrator.rb
kanal-0.6.0 lib/kanal/core/helpers/parameter_registrator.rb