Class: NRSER::Types::IsA
Overview
Type satisfied by class membership (or mixin presence for modules).
Tests via the subject value’s ‘#is_a?` method.
Instance Attribute Summary collapse
-
#mod ⇒ Object
readonly
Returns the value of attribute mod.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#custom_from_data(data) ⇒ Object
Forwards to ‘mod.from_data`.
- #explain ⇒ Object
-
#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`.
- #init_from_data? ⇒ return_type
-
#initialize(mod, init_from_data: false, **options) ⇒ IsA
constructor
A new instance of IsA.
- #test?(value) ⇒ Boolean
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.
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/nrser/types/is_a.rb', line 13 def initialize mod, init_from_data: false, ** unless mod.is_a?( Module ) raise ArgumentError, "`mod` argument must be a Module (inc. Class), " \ "received #{ mod.inspect }" end super ** @init_from_data = !!init_from_data @mod = mod end |
Instance Attribute Details
#mod ⇒ Object (readonly)
Returns the value of attribute mod.
11 12 13 |
# File 'lib/nrser/types/is_a.rb', line 11 def mod @mod end |
Instance Method Details
#==(other) ⇒ Object
81 82 83 84 85 |
# File 'lib/nrser/types/is_a.rb', line 81 def == other equal?( other ) || ( self.class == other.class && self.mod == other.mod ) end |
#custom_from_data(data) ⇒ Object
Forwards to ‘mod.from_data`.
59 60 61 62 63 64 65 |
# File 'lib/nrser/types/is_a.rb', line 59 def custom_from_data data if init_from_data? mod.new data else mod.from_data data end end |
#explain ⇒ Object
28 29 30 |
# File 'lib/nrser/types/is_a.rb', line 28 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`.
74 75 76 77 78 |
# File 'lib/nrser/types/is_a.rb', line 74 def has_from_data? @from_data || init_from_data? || mod.respond_to?( :from_data ) end |
#init_from_data? ⇒ return_type
Document init_from_data? method.
Returns @todo Document return value.
47 48 49 |
# File 'lib/nrser/types/is_a.rb', line 47 def init_from_data? @init_from_data end |
#test?(value) ⇒ Boolean
33 34 35 |
# File 'lib/nrser/types/is_a.rb', line 33 def test? value value.is_a? mod end |