Sha256: 72eb08d971d8e726996ed67e48ebe95d64195035fd04ce9f6b8339d561b4a7e4

Contents?: true

Size: 1.98 KB

Versions: 2

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

# @api private
# @since 0.1.0
module SmartCore::ValueObject::Interface::Constructor
  class << self
    # @param base_klass [Class<SmartCore::ValueObject>]
    # @return [void]
    #
    # @api private
    # @since 0.1.0
    def included(base_klass)
      base_klass.include(::SmartCore::Initializer)
      base_klass.singleton_class.class_eval do
        alias_method :attribute, :param
        alias_method :property, :option
        alias_method :attributes, :params
        alias_method :properties, :options
      end
      base_klass.include(AttributeAccess)
      base_klass.extend_initialization_flow(&ImmutabilityControl)
    end
  end

  # @api private
  # @since 0.1.0
  module AttributeAccess
    # @return [Hash<Symbol,Any>]
    #
    # @note strongly depends on SmartCore::Initializer implementaiton
    #
    # @api public
    # @since 0.1.0
    def __attributes__
      __collect_params__
    end

    # @return [Hash<Symbol,Any>]
    #
    # @note strongly depends on SmartCore::Initializer implementaiton
    #
    # @api public
    # @since 0.1.0
    def __properties__
      __collect_options__
    end

    # @return [Hash<Symbol,Any>]
    #
    # @note strongly depends on SmartCore::Initializer implementaiton
    #
    # @api public
    # @since 0.1.0
    def __params__
      __collect_params__.merge(__collect_options__)
    end
  end

  # @api private
  # @since 0.1.0
  module ImmutabilityControl
    class << self
      # @param instance [SmartCore::ValueObject]
      # @return [void]
      #
      # @note strongly depends on SmartCore::Initializer implementation
      #
      # @api private
      # @since 0.1.0
      def freeze_instance(instance)
        instance.freeze unless instance.frozen?
      end

      # @return [Proc]
      #
      # @note strongly depends on SmartCore::Initializer implementation
      #
      # @api private
      # @since 0.1.0
      def to_proc
        proc { |instance| freeze_instance(instance) }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
smart_value-object-0.2.0 lib/smart_core/value_object/interface/constructor.rb
smart_value-object-0.1.0 lib/smart_core/value_object/interface/constructor.rb