Class: NRSER::Types::Is

Inherits:
Type show all
Defined in:
lib/nrser/types/is.rb

Overview

Type satisfied only by it’s exact #value object (identity comparison via ‘#equal?`).

Direct Known Subclasses

BooleanType

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

#===, #builtin_inspect, #check, #check!, #from_data, #from_s, #has_from_data?, #has_from_s?, #has_to_data?, #inspect, #intersection, #name, #not, #respond_to?, #test, #to_data, #to_s, #union, #xor

Constructor Details

#initialize(value, **options) ⇒ Is

Returns a new instance of Is.



33
34
35
36
# File 'lib/nrser/types/is.rb', line 33

def initialize value, **options
  super **options
  @value = value
end

Instance Attribute Details

#valueObject (readonly)

Attributes



31
32
33
# File 'lib/nrser/types/is.rb', line 31

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



59
60
61
62
63
# File 'lib/nrser/types/is.rb', line 59

def == other
  equal?(other) ||
  ( self.class == other.class &&
    @value == other.value )
end

#explainObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/nrser/types/is.rb', line 38

def explain
  case value
  when Module
    module_type = if value.is_a?( Class ) then 'Class' else 'Module' end
    
    name = if value.anonymous?
      value.to_s.split( ':' ).last[0...-1]
    else
      value.name
    end
    
    "#{ module_type }<#{ name }>"
  else
    value.inspect
  end
end

#test?(value) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/nrser/types/is.rb', line 55

def test? value
  @value.equal? value
end