Class: NRSER::Types::When
Overview
This was kinda hacked in when my idiot-ass figured out that all this types BS could fit in real well with Ruby’s ‘===`, allowing types to be used in `when` clauses.
Previously, make used to see if something was a module, and turn those into ‘is_a` types, and turn everything else into `is`, but this kind of sucked for a bunch of reasons I don’t totally remember.
Now, if a value is not a special case (like ‘nil`) or already a type, make turns it into a When.
When instances are totally Ruby-centric, and are thus mostly to support in-runtime testing - you wouldn’t want a When type to be part of an API schema or something - but they’re really nice for the internal stuff.
Wraps an object as a type, using Ruby’s “case equality” ‘===` to test membership (like a `when` clause in a `case` expression).
Deals with some data loading too.
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
The wrapped Object whose ‘#===` will be used to test membership.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #custom_from_s(string) ⇒ Object
- #explain ⇒ Object
-
#from_data(data) ⇒ Object
If #object responds to ‘#from_data`, call that and check results.
- #has_from_data? ⇒ Boolean
- #has_from_s? ⇒ Boolean
-
#initialize(object, **options) ⇒ When
constructor
Instantiate a new ‘NRSER::Types::When`.
-
#test?(value) ⇒ Boolean
Instance Methods ======================================================================.
Methods inherited from Type
#===, #builtin_inspect, #check, #check!, #from_s, #has_to_data?, #inspect, #intersection, #name, #not, #respond_to?, #test, #to_data, #to_s, #union, #xor
Constructor Details
#initialize(object, **options) ⇒ When
Instantiate a new ‘NRSER::Types::When`.
54 55 56 57 |
# File 'lib/nrser/types/when.rb', line 54 def initialize object, ** super ** @object = object end |
Instance Attribute Details
Instance Method Details
#==(other) ⇒ Object
111 112 113 114 115 |
# File 'lib/nrser/types/when.rb', line 111 def == other equal?( other ) || ( self.class == other.class && self.object == other.object ) end |
#custom_from_s(string) ⇒ Object
78 79 80 |
# File 'lib/nrser/types/when.rb', line 78 def custom_from_s string object.from_s string end |
#explain ⇒ Object
68 69 70 |
# File 'lib/nrser/types/when.rb', line 68 def explain @object.inspect end |
#from_data(data) ⇒ Object
If #object responds to ‘#from_data`, call that and check results.
Otherwise, forward up to Type#from_data.
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/nrser/types/when.rb', line 93 def from_data data if @from_data.nil? if @object.respond_to? :from_data check @object.from_data( data ) else super data end else @from_data.call data end end |
#has_from_data? ⇒ Boolean
106 107 108 |
# File 'lib/nrser/types/when.rb', line 106 def has_from_data? @from_data || @object.respond_to?( :from_data ) end |
#has_from_s? ⇒ Boolean
73 74 75 |
# File 'lib/nrser/types/when.rb', line 73 def has_from_s? @from_s || object.respond_to?( :from_s ) end |
#test?(value) ⇒ Boolean
Instance Methods
63 64 65 |
# File 'lib/nrser/types/when.rb', line 63 def test? value @object === value end |