Class: InquiryAttrs::SymbolInquiry

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

Overview

Wraps a Symbol to provide the same predicate interface as ActiveSupport::StringInquirer.

inquiry = SymbolInquiry.new(:active)
inquiry.active?      # => true
inquiry.inactive?    # => false
inquiry == :active   # => true
inquiry == 'active'  # => true
inquiry.is_a?(Symbol) # => true

Instance Method Summary collapse

Constructor Details

#initialize(sym) ⇒ SymbolInquiry

Returns a new instance of SymbolInquiry.

Parameters:

  • sym (Symbol)

Raises:

  • (ArgumentError) —

    if the argument is not a Symbol



17
18
19
20
21
# File 'lib/inquiry_attrs/symbol_inquiry.rb', line 17

def initialize(sym)
  raise ArgumentError, "Must be a Symbol, got #{sym.class}" unless sym.is_a?(Symbol)

  super(sym.is_a?(SymbolInquiry) ? sym.__getobj__ : sym)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



23
24
25
26
27
28
29
# File 'lib/inquiry_attrs/symbol_inquiry.rb', line 23

def method_missing(method_name, *_args, &_block)
  if method_name.to_s.end_with?('?')
    __getobj__.to_s == method_name.to_s.delete_suffix('?')
  else
    super
  end
end

Instance Method Details

#==(other) ⇒ Object

Compares against Symbol, String, or another SymbolInquiry.



36
37
38
39
40
41
42
43
# File 'lib/inquiry_attrs/symbol_inquiry.rb', line 36

def ==(other)
  case other
  when SymbolInquiry then __getobj__ == other.__getobj__
  when Symbol        then __getobj__ == other
  when String        then __getobj__.to_s == other
  else false
  end
end

#blank? ⇒ Boolean

Returns:

  • (Boolean)


50
# File 'lib/inquiry_attrs/symbol_inquiry.rb', line 50

def blank?  = false

#inspect ⇒ Object



54
# File 'lib/inquiry_attrs/symbol_inquiry.rb', line 54

def inspect = ":#{__getobj__}"

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

Returns:

  • (Boolean)


45
# File 'lib/inquiry_attrs/symbol_inquiry.rb', line 45

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

#nil? ⇒ Boolean

Returns:

  • (Boolean)


49
# File 'lib/inquiry_attrs/symbol_inquiry.rb', line 49

def nil?    = false

#present? ⇒ Boolean

Returns:

  • (Boolean)


51
# File 'lib/inquiry_attrs/symbol_inquiry.rb', line 51

def present? = true

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

Returns:

  • (Boolean)


31
32
33
# File 'lib/inquiry_attrs/symbol_inquiry.rb', line 31

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

#to_s ⇒ Object



52
# File 'lib/inquiry_attrs/symbol_inquiry.rb', line 52

def to_s    = __getobj__.to_s

#to_sym ⇒ Object



53
# File 'lib/inquiry_attrs/symbol_inquiry.rb', line 53

def to_sym  = __getobj__