Sha256: f86b218a490d715f913c5d369a9a7873422b7cbc884fd74bc004071a6058e121

Contents?: true

Size: 1.28 KB

Versions: 13

Compression:

Stored size: 1.28 KB

Contents

require 'set'

module Dry
  module Core
    # A list of constants you can use to avoid memory allocations or identity checks.
    #
    # @example Just include this module to your class or module
    #   class Foo
    #     def call(value = EMPTY_ARRAY)
    #        value.map(&:to_s)
    #     end
    #   end
    #
    # @api public
    module Constants
      # An empty array
      EMPTY_ARRAY = [].freeze
      # An empty hash
      EMPTY_HASH = {}.freeze
      # An empty list of options
      EMPTY_OPTS = {}.freeze
      # An empty set
      EMPTY_SET = Set.new.freeze
      # An empty string
      EMPTY_STRING = ''.freeze

      # A special value you can use as a default to know if no arguments
      # were passed to you method
      #
      # @example
      #   def method(value = Undefined)
      #     if value == Undefined
      #       puts 'no args'
      #     else
      #       puts value
      #     end
      #   end
      Undefined = Object.new.tap do |undefined|
        def undefined.to_s
          'Undefined'
        end

        def undefined.inspect
          'Undefined'
        end
      end.freeze

      def self.included(base)
        super

        constants.each do |const_name|
          base.const_set(const_name, const_get(const_name))
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
dry-core-0.4.3 lib/dry/core/constants.rb
dry-core-0.4.2 lib/dry/core/constants.rb
dry-core-0.4.1 lib/dry/core/constants.rb
dry-core-0.4.0 lib/dry/core/constants.rb
dry-core-0.3.4 lib/dry/core/constants.rb
dry-core-0.3.3 lib/dry/core/constants.rb
dry-core-0.3.2 lib/dry/core/constants.rb
dry-core-0.3.1 lib/dry/core/constants.rb
dry-core-0.3.0 lib/dry/core/constants.rb
dry-core-0.2.4 lib/dry/core/constants.rb
dry-core-0.2.3 lib/dry/core/constants.rb
dry-core-0.2.2 lib/dry/core/constants.rb
dry-core-0.2.1 lib/dry/core/constants.rb