Sha256: 00fe2c4e16ed7c0eaafa806b349bf25dddcefcbfc22a3657be7145861bab80fd

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

module Brigadier
  module Parameters
    module Base
      attr_reader :name, :description, :value

      def forbidden_parameters
        [ :name, :description, :value ]
      end

      def value=(value)
        @value = block_defined? ? block.call(value) : value
        ENV[env_variable_value_key_name] = @value.to_s
      end

      def normalised_attribute_name
        attribute_name.gsub(/[ -]/, '_')
      end

      def attribute_name
        @attribute_name ||= args.fetch(:attribute_name, name).to_s
      end

      def hidden?
        args.fetch(:hidden, false)
      end

      def value?
        !value.nil?
      end

      def valid?
        validate! && true
      rescue Exceptions::Base
        false
      end

      def required?
        args.fetch(:required, false)
      end

      def block_defined?
        !block.nil?
      end

      private

        attr_reader :block

        def default_value
          nil
        end

        def validate_using_klass!
          validator_klasses.each { |klass| klass.new(self).validate! }
        end

        def validator_klasses
          args.fetch(:validators, nil)
        end

        def assign_value_from_env_variable
          value = ENV.fetch(env_variable_value_key_name, default_value)
          self.value = value if value
        end

        def normalised_name
          name.gsub(/[ -]/, '_')
        end

        def env_variable_value_key_name
          normalised_name.upcase
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
brigadier-0.1.1 lib/brigadier/parameters/base.rb