Sha256: e5867cfdf08c055a47d5b397b2a7e2232015854d92823eb333d580470e5f372d

Contents?: true

Size: 1.51 KB

Versions: 6

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module Qismo
  #
  # Class to handle nil value
  #
  class NullObject
    #
    # @return [TrueClass,FalseClass]
    #
    def !
      true
    end

    #
    # @return [TrueClass,FalseClass]
    #
    def respond_to?(*)
      true
    end

    #
    # @return [TrueClass,FalseClass]
    #
    def instance_of?(klass)
      raise(TypeError, "class or module required") unless klass.is_a?(Class)

      self.class == klass
    end

    #
    # @return [TrueClass,FalseClass]
    #
    def kind_of?(mod)
      raise(TypeError, "class or module required") unless mod.is_a?(Module)

      self.class.ancestors.include?(mod)
    end

    alias_method :is_a?, :kind_of?

    #
    # @return [Integer]
    #
    def <=>(other)
      if other.is_a?(self.class)
        0
      else
        -1
      end
    end

    #
    # @return [TrueClass,FalseClass]
    #
    def nil?
      true
    end

    #
    # @return [String]
    #
    def as_json(*)
      "null"
    end

    #
    # @return [String]
    #
    def to_json(*args)
      nil.to_json(*args)
    end

    #
    # @return [nil]
    #
    def presence
      nil
    end

    #
    # @return [TrueClass,FalseClass]
    #
    def blank?
      true
    end

    #
    # @return [TrueClass,FalseClass]
    #
    def present?
      false
    end

    #
    # @return [NullObject]
    #
    def method_missing(m, *args, &block)
      self
    end

    #
    # @return [TrueClass,FalseClass]
    #
    def respond_to_missing?(method_name, *args)
      false
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
qismo-0.1.8 lib/qismo/models/null_object.rb
qismo-0.1.5 lib/qismo/models/null_object.rb
qismo-0.1.4 lib/qismo/models/null_object.rb
qismo-0.1.2 lib/qismo/models/null_object.rb
qismo-0.1.1 lib/qismo/models/null_object.rb
qismo-0.1.0 lib/qismo/models/null_object.rb