Sha256: ef0935c414479fef3f00b8e5a6873f1288b581768589620de6aeb70c27905cdb

Contents?: true

Size: 1.32 KB

Versions: 52

Compression:

Stored size: 1.32 KB

Contents

module Tins
  # Implementation of the null object pattern in Ruby.
  module Null
    def method_missing(*)
      self
    end

    def const_missing(*)
      self
    end

    def to_s
      ''
    end

    def to_str
      nil
    end

    def to_f
      0.0
    end

    def to_i
      0
    end

    def to_int
      nil
    end

    def to_a
      []
    end

    def to_ary
      nil
    end

    def inspect
      'NULL'
    end

    def nil?
      true
    end

    def blank?
      true
    end

    def as_json(*)
    end

    def to_json(*)
      'null'
    end

    module Kernel
      def null(value = nil)
        value.nil? ? Tins::NULL : value
      end

      alias Null null

      def null_plus(opts = {})
        value = opts[:value]
        opts[:caller] = caller
        if respond_to?(:caller_locations, true)
          opts[:caller_locations] = caller_locations
        end

        value.nil? ? Tins::NullPlus.new(opts) : value
      end

      alias NullPlus null_plus
    end
  end

  class NullClass < Module
    include Tins::Null
  end

  NULL = NullClass.new

  NULL.freeze

  class NullPlus
    include Tins::Null

    def initialize(opts = {})
      singleton_class.class_eval do
        opts.each do |name, value|
          define_method(name) { value }
        end
      end
    end
  end
end

require 'tins/alias'

Version data entries

52 entries across 42 versions & 2 rubygems

Version Path
tins-1.38.0 lib/tins/null.rb
tins-1.37.1 lib/tins/null.rb
tins-1.37.0 lib/tins/null.rb
tins-1.36.1 lib/tins/null.rb
tins-1.36.0 lib/tins/null.rb
tins-1.35.0 lib/tins/null.rb
tins-1.34.0 lib/tins/null.rb
tins-1.33.0 lib/tins/null.rb
tdiary-5.2.4 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/null.rb
tins-1.32.1 lib/tins/null.rb
tins-1.32.0 lib/tins/null.rb
tdiary-5.2.3 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/null.rb
tdiary-5.2.2 vendor/bundle/ruby/3.1.0/gems/tins-1.31.1/lib/tins/null.rb
tins-1.31.1 lib/tins/null.rb
tdiary-5.2.1 vendor/bundle/ruby/3.1.0/gems/tins-1.31.0/lib/tins/null.rb
tins-1.31.0 lib/tins/null.rb
tins-1.30.0 lib/tins/null.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/tins-1.29.1/lib/tins/null.rb
tdiary-5.2.0 vendor/bundle/ruby/3.0.0/gems/tins-1.29.1/lib/tins/null.rb
tdiary-5.1.7 vendor/bundle/ruby/2.7.0/gems/tins-1.29.1/lib/tins/null.rb