Class: NRSER::Types::IsA

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

Overview

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

Tests via the subject value’s ‘#is_a?` method.

Direct Known Subclasses

ArrayType, HashType

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Type

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

Constructor Details

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

Returns a new instance of IsA.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/nrser/types/is_a.rb', line 11

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.



9
10
11
# File 'lib/nrser/types/is_a.rb', line 9

def mod
  @mod
end

Instance Method Details

#==(other) ⇒ Object



79
80
81
82
83
# File 'lib/nrser/types/is_a.rb', line 79

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 (*)

    Data to try to load from.

Raises:



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

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

#explainObject



26
27
28
# File 'lib/nrser/types/is_a.rb', line 26

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:

  • (Boolean)


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

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

#init_from_data?return_type

TODO:

Document init_from_data? method.

Returns @todo Document return value.

Parameters:

  • arg_name (type)

    @todo Add name param description.

Returns:

  • (return_type)

    @todo Document return value.



45
46
47
# File 'lib/nrser/types/is_a.rb', line 45

def init_from_data?
  @init_from_data
end

#test?(value) ⇒ Boolean

Returns:

  • (Boolean)


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

def test? value
  value.is_a? mod
end