Class: Result
- Inherits:
-
Object
- Object
- Result
- Defined in:
- lib/Olib/pattern_matching/result.rb
Defined Under Namespace
Modules: Constructors
Instance Attribute Summary collapse
-
#val ⇒ Object
readonly
Returns the value of attribute val.
Class Method Summary collapse
- ._compare_object_shape(this, expected) ⇒ Object
- ._compare_values(left, right) ⇒ Object
- ._match_class(this, other) ⇒ Object
- ._match_result(this, other) ⇒ Object
- .compare(this, other) ⇒ Object
- .included(ctx) ⇒ Object
- .of ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #===(other) ⇒ Object
-
#initialize(val) ⇒ Result
constructor
A new instance of Result.
- #to_json(*args) ⇒ Object
- #to_proc ⇒ Object
Constructor Details
#initialize(val) ⇒ Result
Returns a new instance of Result.
56 57 58 |
# File 'lib/Olib/pattern_matching/result.rb', line 56 def initialize(val) @val = val end |
Instance Attribute Details
#val ⇒ Object (readonly)
Returns the value of attribute val.
54 55 56 |
# File 'lib/Olib/pattern_matching/result.rb', line 54 def val @val end |
Class Method Details
._compare_object_shape(this, expected) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/Olib/pattern_matching/result.rb', line 30 def self._compare_object_shape(this, expected) fail "expected Shape(#{expected.inspect}) must be a Hash" unless expected.is_a?(Hash) ## fail fast when not possible to be true return false if this.is_a?(Hash) and expected.size > this.size ## compare all keys expected.all? do |k, v| if this.respond_to?(k.to_sym) _compare_values(v, this.send(k.to_sym)) elsif this.is_a?(Array) and k.is_a?(Fixnum) _compare_values(v, this[k.to_i]) elsif this.respond_to?(:[]) and not this.is_a?(Array) _compare_values(v, this[k.to_s]) or _compare_values(v, this[k.to_sym]) else false end end end |
._compare_values(left, right) ⇒ Object
26 27 28 |
# File 'lib/Olib/pattern_matching/result.rb', line 26 def self._compare_values(left, right) left == Any || right == Any || left === right end |
._match_class(this, other) ⇒ Object
21 22 23 24 |
# File 'lib/Olib/pattern_matching/result.rb', line 21 def self._match_class(this, other) return this.is_a?(other) if [Err, Ok].include?(other) this.val.is_a?(other) end |
._match_result(this, other) ⇒ Object
48 49 50 51 52 |
# File 'lib/Olib/pattern_matching/result.rb', line 48 def self._match_result(this, other) return false unless this.is_a?(other.class) return _compare_object_shape(this.val, other.val) if this.val.is_a?(Object) and other.val.is_a?(Hash) return _compare_values(this.val, other.val) end |
.compare(this, other) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/Olib/pattern_matching/result.rb', line 13 def self.compare(this, other) return _match_class(this, other) if other.is_a?(Class) return _match_result(this, other) if this.is_a?(Result) and other.is_a?(Result) return _compare_object_shape(this.val, other) if this.val.is_a?(Object) and other.is_a?(Hash) return _compare_object_shape(other, this.val) if this.val.is_a?(Hash) and other.is_a?(Object) return _compare_values(this.val, other) end |
.included(ctx) ⇒ Object
5 6 7 8 |
# File 'lib/Olib/pattern_matching/result.rb', line 5 def self.included(ctx) ctx.include(Result::Constructors) ctx.extend(Result:Constructors) end |
.of ⇒ Object
10 |
# File 'lib/Olib/pattern_matching/result.rb', line 10 def self.of(); -> val { self.new(val); }; end |
Instance Method Details
#==(other) ⇒ Object
60 61 62 |
# File 'lib/Olib/pattern_matching/result.rb', line 60 def ==(other) self.===(other) end |
#===(other) ⇒ Object
64 65 66 |
# File 'lib/Olib/pattern_matching/result.rb', line 64 def ===(other) Result.compare(self, other) end |
#to_json(*args) ⇒ Object
68 69 70 |
# File 'lib/Olib/pattern_matching/result.rb', line 68 def to_json(*args) @val.to_json(*args) end |
#to_proc ⇒ Object
72 73 74 |
# File 'lib/Olib/pattern_matching/result.rb', line 72 def to_proc -> other { Result.compare(self, other) } end |