Sha256: 6d669696177337548fc3f1aa86edb6b1fd50aa0da27e21896b7c8343b25fb094

Contents?: true

Size: 1.25 KB

Versions: 11

Compression:

Stored size: 1.25 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

    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.freeze

  class NullPlus
    include Tins::Null

    def initialize(opts = {})
      class << self; self; end.class_eval do
        opts.each do |name, value|
          define_method(name) { value }
        end
      end
    end
  end
end

require 'tins/alias'

Version data entries

11 entries across 10 versions & 3 rubygems

Version Path
tdiary-5.1.2 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/lib/tins/null.rb
tdiary-5.1.1 vendor/bundle/ruby/2.6.0/gems/tins-0.13.2/lib/tins/null.rb
tdiary-5.0.8 vendor/bundle/gems/tins-0.13.2/lib/tins/null.rb
tdiary-5.0.8 vendor/bundle/ruby/2.5.0/gems/tins-0.13.2/lib/tins/null.rb
tins-1.0.0 lib/tins/null.rb
tins-0.13.2 lib/tins/null.rb
social_url_stats-0.0.1 vendor/ruby/1.9.1/gems/tins-0.13.1/lib/tins/null.rb
tins-0.13.1 lib/tins/null.rb
tins-0.13.0 lib/tins/null.rb
tins-0.12.0 lib/tins/null.rb
tins-0.11.0 lib/tins/null.rb