Class: NRSER::Types::IsA

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

Overview

Note:

Construct IsA types using the IsA factory.

Type satisfied by class membership (or mixin presence for modules).

Tests via the subject value’s ‘#is_a?` method is `true` for #mod. Works for class instances as will as mixins.

Direct Known Subclasses

ArrayType, HashType

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

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

Constructor Details

#initialize(mod, init_from_data: false, **options) ⇒ IsA

Returns a new instance of IsA.



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/nrser/types/is_a.rb', line 35

def initialize mod, init_from_data: false, **options
  unless mod.is_a?( Module )
    raise ArgumentError,
      "`mod` argument must be a Module (inc. Class), " \
      "received #{ mod.inspect }"
  end
  
  super **options
  
  @init_from_data = !!init_from_data
  
  @mod = mod
end

Instance Attribute Details

#modObject (readonly)

Returns the value of attribute mod.



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

def mod
  @mod
end

Instance Method Details

#==(other) ⇒ Object



93
94
95
96
97
# File 'lib/nrser/types/is_a.rb', line 93

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

#custom_from_data(data) ⇒ Object

Forwards to ‘mod.from_data`.

Parameters:

  • data (Object)

    Data to try to load from.

Raises:



71
72
73
74
75
76
77
# File 'lib/nrser/types/is_a.rb', line 71

def custom_from_data data
  if init_from_data?
    mod.new data
  else
    mod.from_data data
  end
end

#explainObject



50
51
52
# File 'lib/nrser/types/is_a.rb', line 50

def explain
  mod.safe_name
end

#has_from_data?Boolean

Overrides Type#has_from_data? to respond ‘true` when there is a instance-specific `@from_data` or the #mod responds to `.from_data`.

Returns:



86
87
88
89
90
# File 'lib/nrser/types/is_a.rb', line 86

def has_from_data?
  @from_data ||
    init_from_data? ||
    mod.respond_to?( :from_data )
end

#init_from_data?Boolean

Returns:



60
61
62
# File 'lib/nrser/types/is_a.rb', line 60

def init_from_data?
  @init_from_data
end

#test?(value) ⇒ Boolean

Returns:



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

def test? value
  value.is_a? mod
end