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.
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, ** 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.
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`.
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 |
#explain ⇒ Object
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`.
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
Document init_from_data? method.
Returns @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
31 32 33 |
# File 'lib/nrser/types/is_a.rb', line 31 def test? value value.is_a? mod end |