Sha256: 1c28f18ff52eba92cb74d6abeaa17474020d19fb82b25145793e1b3eeff3911b

Contents?: true

Size: 1.31 KB

Versions: 71

Compression:

Stored size: 1.31 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.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

71 entries across 67 versions & 8 rubygems

Version Path
logstash-filter-cache-redis-0.3.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/lib/tins/null.rb
logstash-filter-cache-redis-0.2.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/lib/tins/null.rb
logstash-filter-cache-redis-0.1.0 vendor/bundle/jruby/1.9/gems/tins-1.6.0/lib/tins/null.rb
tins-1.15.0 lib/tins/null.rb
tdiary-5.0.5 vendor/bundle/gems/tins-1.13.2/lib/tins/null.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/tins-1.13.2/lib/tins/null.rb
tdiary-5.0.5 vendor/bundle/gems/tins-1.14.0/lib/tins/null.rb
tins-1.14.0 lib/tins/null.rb
tins-1.13.3 lib/tins/null.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/tins-1.13.2/lib/tins/null.rb
tdiary-5.0.4 vendor/bundle/gems/tins-1.13.2/lib/tins/null.rb
tins-1.13.2 lib/tins/null.rb
ivanvc-logstash-input-s3-3.1.1.4 vendor/local/gems/tins-1.13.0/lib/tins/null.rb
ivanvc-logstash-input-s3-3.1.1.3 vendor/local/gems/tins-1.13.0/lib/tins/null.rb
ivanvc-logstash-input-s3-3.1.1.2 vendor/local/gems/tins-1.13.0/lib/tins/null.rb
tins-1.13.0 lib/tins/null.rb
tdiary-5.0.2 vendor/bundle/gems/tins-1.12.0/lib/tins/null.rb
tins-1.12.0 lib/tins/null.rb
tins-1.11.0 lib/tins/null.rb
tdiary-5.0.1 vendor/bundle/gems/tins-1.10.2/lib/tins/null.rb