Class: TAlgebra::Monad::Parser
Defined Under Namespace
Classes: Optional
Constant Summary
Constants inherited
from Either
Either::LEFT, Either::RIGHT
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
included
Methods inherited from Either
#==, #from_either, #from_either!, left, #left?, pure, #right?, #to_obj
Constructor Details
#initialize(is:, value:, name: nil) ⇒ Parser
Returns a new instance of Parser.
6
7
8
9
|
# File 'lib/t_algebra/monad/parser.rb', line 6
def initialize(is:, value:, name: nil)
super(is: is, value: value)
@name = name
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
38
39
40
|
# File 'lib/t_algebra/monad/parser.rb', line 38
def name
@name
end
|
Class Method Details
.chain_bind(ma, &block) ⇒ Object
28
29
30
31
32
|
# File 'lib/t_algebra/monad/parser.rb', line 28
def chain_bind(ma, &block)
raise "Yield blocks must return instances of #{self}. Got #{ma.class}" unless [Parser, Parser::Optional].include?(ma.class)
ma.as_parser.bind(&block)
end
|
.failure(msg, name = nil) ⇒ Object
12
13
14
|
# File 'lib/t_algebra/monad/parser.rb', line 12
def failure(msg, name = nil)
new(is: Either::LEFT, value: msg, name: name)
end
|
.fetch(o, key) ⇒ Object
20
21
22
|
# File 'lib/t_algebra/monad/parser.rb', line 20
def fetch(o, key)
parse(o).fetch(key)
end
|
.fetch!(o, key) ⇒ Object
24
25
26
|
# File 'lib/t_algebra/monad/parser.rb', line 24
def fetch!(o, key)
parse(o).fetch!(key)
end
|
.parse(val, name = nil) ⇒ Object
16
17
18
|
# File 'lib/t_algebra/monad/parser.rb', line 16
def parse(val, name = nil)
new(is: Either::RIGHT, value: val, name: name)
end
|
Instance Method Details
#as_parser ⇒ Object
110
111
112
|
# File 'lib/t_algebra/monad/parser.rb', line 110
def as_parser
instance_of?(Parser::Optional) ? Parser.new(is: is, value: value, name: name) : self
end
|
#bind ⇒ Object
58
59
60
61
62
|
# File 'lib/t_algebra/monad/parser.rb', line 58
def bind
super
rescue => e
self.class.failure("Unable to bind: #{e}", name)
end
|
75
76
77
78
|
# File 'lib/t_algebra/monad/parser.rb', line 75
def (&block)
return yield("#{name}: #{value}") if left? && !name.nil?
from_either(&block)
end
|
80
81
82
|
# File 'lib/t_algebra/monad/parser.rb', line 80
def
{ |e| raise UnsafeError.new("#extract_parsed! exception. #{e}") }
end
|
#fetch(key) ⇒ Object
44
45
46
|
# File 'lib/t_algebra/monad/parser.rb', line 44
def fetch(key)
with_name(key).fmap { |o| o.respond_to?(:[]) ? o[key] : o.send(key) }.optional
end
|
#fetch!(key) ⇒ Object
48
49
50
|
# File 'lib/t_algebra/monad/parser.rb', line 48
def fetch!(key)
with_name(key).fmap { |o| o.respond_to?(:[]) ? o[key] : o.send(key) }.required
end
|
#fmap ⇒ Object
52
53
54
55
56
|
# File 'lib/t_algebra/monad/parser.rb', line 52
def fmap
super.with_name(name)
rescue => e
self.class.failure("Unable to fmap: #{e}", name)
end
|
#is_a?(*klasses) ⇒ Boolean
64
65
66
67
68
|
# File 'lib/t_algebra/monad/parser.rb', line 64
def is_a?(*klasses)
validate("Must be type #{klasses.join(", ")}") do |v|
klasses.any? { |k| v.is_a?(k) }
end
end
|
#optional ⇒ Object
88
89
90
|
# File 'lib/t_algebra/monad/parser.rb', line 88
def optional
Optional.new(is: is, value: value, name: name)
end
|
#required ⇒ Object
84
85
86
|
# File 'lib/t_algebra/monad/parser.rb', line 84
def required
validate("Is required") { |v| !v.nil? }
end
|
#validate(msg = "Invalid") ⇒ Object
70
71
72
73
|
# File 'lib/t_algebra/monad/parser.rb', line 70
def validate(msg = "Invalid")
n = name
bind { |val| yield(val) ? Parser.parse(val, n) : Parser.failure(msg, n) }
end
|
#with_name(name) ⇒ Object
39
40
41
42
|
# File 'lib/t_algebra/monad/parser.rb', line 39
def with_name(name)
@name = name
self
end
|