Sha256: 11b8393f255e955533395199e6d3ee4fb1a28c8a1f065b4ce0262f66f41a85a3

Contents?: true

Size: 1.67 KB

Versions: 3

Compression:

Stored size: 1.67 KB

Contents

module Koine
  module Attributes
    module Adapter
      class Base
        attr_reader :attribute_name

        def initialize
          with_attribute_name('annonymous_attribute_name')
        end

        def coerce(value)
          return coerce_nil if value.nil?

          coerce_not_nil(value)
        end

        def default_value
          @default_value.respond_to?(:call) &&
            @default_value.call ||
            @default_value
        end

        def with_nil_value(value = nil, &block)
          @nil_value_set = true
          @nil_value = value
          @nil_value = block if block
          self
        end

        def with_default_value(value = nil, &block)
          @default_value = value
          @default_value = block if block
          self
        end

        def with_attribute_name(name)
          @attribute_name = name
          self
        end

        private

        def coerce_nil
          if @nil_value_set
            return @nil_value.respond_to?(:call) ? @nil_value.call : @nil_value
          end

          raise_error('Cannot be nil')
        end

        def coerce_not_nil(_value)
          raise NotImplementedError
        end

        # duplicates if possible and freezes object
        def secure
          value = yield

          unless value.is_a?(::Symbol)
            value = value.dup if value.respond_to?(:dup)
          end

          value.freeze
        end

        def wrap_errors
          yield
        rescue StandardError => error
          raise_error(error)
        end

        def raise_error(message)
          raise Koine::Attributes::InvalidAttributeError.new(message, attribute_name)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
koine-attributes-1.2.3 lib/koine/attributes/adapter/base.rb
koine-attributes-1.2.2 lib/koine/attributes/adapter/base.rb
koine-attributes-1.2.1 lib/koine/attributes/adapter/base.rb