Class: Result

Inherits:
Object
  • Object
show all
Defined in:
lib/Olib/pattern_matching/result.rb

Direct Known Subclasses

Any, Err, Ok, Where

Defined Under Namespace

Modules: Constructors

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#valObject (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

.ofObject



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_procObject



72
73
74
# File 'lib/Olib/pattern_matching/result.rb', line 72

def to_proc
  -> other { Result.compare(self, other) }
end