Class: InquiryAttrs::NilInquiry

Inherits:
Object
  • Object
show all
Defined in:
lib/inquiry_attrs/nil_inquiry.rb

Overview

Returned by an inquired attribute when its raw value is blank.

Every ? predicate returns false, so callers never receive a NoMethodError and nil comparisons work naturally.

user.status         # => InquiryAttrs::NilInquiry::INSTANCE  (when nil/blank)
user.status.nil?    # => true
user.status.active? # => false
user.status == nil  # => true

Constant Summary collapse

INSTANCE =
new.freeze

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *_args, &_block) ⇒ Object

Any ? method returns false — no NoMethodError on blank attributes.



18
19
20
21
22
# File 'lib/inquiry_attrs/nil_inquiry.rb', line 18

def method_missing(method_name, *_args, &_block)
  return false if method_name.to_s.end_with?('?')

  super
end

Instance Method Details

#==(other) ⇒ Object



42
43
44
# File 'lib/inquiry_attrs/nil_inquiry.rb', line 42

def ==(other)
  other.nil? || other == '' || other.equal?(INSTANCE)
end

#blank? ⇒ Boolean

Returns:

  • (Boolean)


35
# File 'lib/inquiry_attrs/nil_inquiry.rb', line 35

def blank? = true

#empty? ⇒ Boolean

Returns:

  • (Boolean)


36
# File 'lib/inquiry_attrs/nil_inquiry.rb', line 36

def empty? = true

#inspect ⇒ Object



40
# File 'lib/inquiry_attrs/nil_inquiry.rb', line 40

def inspect = 'nil'

#is_a?(klass) ⇒ Boolean Also known as: kind_of?, instance_of?

NilClass cannot be subclassed in Ruby, so we override the type-check methods explicitly — the same technique used by SymbolInquiry for Symbol.

Returns:

  • (Boolean)


30
# File 'lib/inquiry_attrs/nil_inquiry.rb', line 30

def is_a?(klass) = klass == NilClass || super

#nil? ⇒ Boolean

Returns:

  • (Boolean)


34
# File 'lib/inquiry_attrs/nil_inquiry.rb', line 34

def nil? = true

#present? ⇒ Boolean

Returns:

  • (Boolean)


37
# File 'lib/inquiry_attrs/nil_inquiry.rb', line 37

def present? = false

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/inquiry_attrs/nil_inquiry.rb', line 24

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.end_with?('?') || super
end

#to_s ⇒ Object



38
# File 'lib/inquiry_attrs/nil_inquiry.rb', line 38

def to_s = ''

#to_str ⇒ Object



39
# File 'lib/inquiry_attrs/nil_inquiry.rb', line 39

def to_str = ''