Sha256: a41b6f3c5c371276a109bebf16488799751b5be4c9d230d759752791b871ff70

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

require_relative "parameter_bag"
require_relative "../logger/logging"

module Kanal
  module Core
    module Helpers
      # Parameter bag but it checks registrator for existence of parameters
      # and if they are has needed by registrator allowances, types etc, whatever
      # registrator rules are stored for property
      class ParameterBagWithRegistrator < ParameterBag
        include Logging

        def initialize(registrator)
          super()
          @registrator = registrator
        end

        def get(name)
          validate_parameter_registration name

          super name
        end

        def set(name, value)
          validate_parameter_registration name

          readonly = @registrator.get_parameter_registration_if_exists(name).readonly?

          if readonly
            value_exists = !get(name).nil?

            if value_exists
              logger.fatal "Parameter #{name} is marked readonly! Attempted to set it's value, but
              it already has value."

              raise "Parameter #{name} is marked readonly! You tried to set it's value, but
              it already has value."
            end
          end

          super name, value
        end

        def validate_parameter_registration(name)
          unless @registrator.parameter_registered? name
            logger.fatal "Parameter #{name} was not registered! Did you forget to register that parameter?"

            raise "Parameter #{name} was not registered! Did you forget to register that parameter?"
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
kanal-0.4.1 lib/kanal/core/helpers/parameter_bag_with_registrator.rb
kanal-0.4.0 lib/kanal/core/helpers/parameter_bag_with_registrator.rb