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
::When. -
#test?(value) ⇒ Boolean
Instance Methods ======================================================================.
Methods inherited from Type
#===, #builtin_inspect, #check, #check!, #default_name, #default_symbolic, #from_s, #has_to_data?, #inspect, #intersection, #name, #not, #respond_to?, #symbolic, #test, #to_data, #to_proc, #to_s, #union, #xor
Constructor Details
#initialize(object, **options) ⇒ When
Instantiate a new ::When.
59 60 61 62 |
# File 'lib/nrser/types/when.rb', line 59 def initialize object, ** super ** @object = object end |
Instance Attribute Details
Instance Method Details
#==(other) ⇒ Object
116 117 118 119 120 |
# File 'lib/nrser/types/when.rb', line 116 def == other equal?( other ) || ( self.class == other.class && self.object == other.object ) end |
#custom_from_s(string) ⇒ Object
83 84 85 |
# File 'lib/nrser/types/when.rb', line 83 def custom_from_s string object.from_s string end |
#explain ⇒ Object
73 74 75 |
# File 'lib/nrser/types/when.rb', line 73 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.
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/nrser/types/when.rb', line 98 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
111 112 113 |
# File 'lib/nrser/types/when.rb', line 111 def has_from_data? @from_data || @object.respond_to?( :from_data ) end |
#has_from_s? ⇒ Boolean
78 79 80 |
# File 'lib/nrser/types/when.rb', line 78 def has_from_s? @from_s || object.respond_to?( :from_s ) end |
#test?(value) ⇒ Boolean
Instance Methods
68 69 70 |
# File 'lib/nrser/types/when.rb', line 68 def test? value @object === value end |