fp_rb 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/fn_reader.rb +24 -0
- data/lib/fp_rb.rb +6 -15
- data/lib/fp_rb/version.rb +1 -1
- metadata +4 -7
- data/lib/fp_rb/maybe.rb +0 -35
- data/lib/fp_rb/operator.rb +0 -13
- data/lib/fp_rb/result.rb +0 -87
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0a74cc786c3b1ce450992bdb9fd824bb127f1660c8bbb8116a99890e22b6541d
|
4
|
+
data.tar.gz: 6d9b06377b8e699bd0fcfd3c22ef76dd939da7a43a57f0a64e5b6e58117b7ea2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6c54beba7da377c1824a3b527738be68d8946c6fcedaab03506c18179dd390177c3af95d2c0524fc1c79d2c6f6046e4b31a6eaf3d46c3fdd887974d7bceda448
|
7
|
+
data.tar.gz: 557656ab550f5ce1532df460a1d4c48f3fce28b846fa8359f34a493fe6f1e97a136255e6da6be2d16e12e637c1262a61fce7d25cd47878a55384d122df92d007
|
data/lib/fn_reader.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
class Module
|
2
|
+
# Stripped down version of ActiveSupport's mattr_reader (don't want Rails dependencies)
|
3
|
+
def fn_reader(*syms)
|
4
|
+
syms.each do |sym|
|
5
|
+
raise StandardError.new("invalid attribute name: #{sym}") unless /\A[_A-Za-z]\w*\z/.match
|
6
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
7
|
+
@@#{sym} = nil unless defined? @@#{sym}
|
8
|
+
|
9
|
+
def self.#{sym}
|
10
|
+
@@#{sym}
|
11
|
+
end
|
12
|
+
EOS
|
13
|
+
|
14
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
15
|
+
def #{sym}
|
16
|
+
@@#{sym}
|
17
|
+
end
|
18
|
+
EOS
|
19
|
+
|
20
|
+
sym_default_value = default
|
21
|
+
class_variable_set("@@#{sym}", sym_default_value) unless sym_default_value.nil?
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/fp_rb.rb
CHANGED
@@ -1,14 +1,10 @@
|
|
1
1
|
require "fp_rb/version"
|
2
2
|
|
3
|
-
require 'fp_rb/operator'
|
4
|
-
require 'fp_rb/maybe'
|
5
|
-
require 'fp_rb/result'
|
6
|
-
|
7
3
|
class Module
|
8
4
|
# stolen from Rails mattr_accessor
|
9
|
-
def
|
5
|
+
def fn_reader(*syms)
|
10
6
|
syms.each do |sym|
|
11
|
-
raise
|
7
|
+
raise StandardError.new("invalid attribute name: #{sym}") unless (sym =~ /\A[_A-Za-z]\w*\z/)
|
12
8
|
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
13
9
|
@@#{sym} = nil unless defined? @@#{sym}
|
14
10
|
def self.#{sym}
|
@@ -16,23 +12,18 @@ class Module
|
|
16
12
|
end
|
17
13
|
EOS
|
18
14
|
|
19
|
-
|
20
|
-
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
15
|
+
class_eval(<<-EOS, __FILE__, __LINE__ + 1)
|
21
16
|
def #{sym}
|
22
17
|
@@#{sym}
|
23
18
|
end
|
24
19
|
EOS
|
25
|
-
end
|
26
|
-
|
27
|
-
sym_default_value = (block_given? && default.nil?) ? yield : default
|
28
|
-
class_variable_set("@@#{sym}", sym_default_value) unless sym_default_value.nil?
|
29
20
|
end
|
30
21
|
end
|
31
22
|
end
|
32
23
|
|
33
24
|
module FpRb
|
34
|
-
|
25
|
+
fn_reader :and_then
|
35
26
|
|
36
|
-
@@and_then = ->f, a { a.and_then(f) }.curry
|
37
|
-
@@map = ->f, a { a.map(f) }.curry
|
27
|
+
@@and_then = -> f, a { a.and_then(f) }.curry
|
28
|
+
@@map = -> f, a { a.map(f) }.curry
|
38
29
|
end
|
data/lib/fp_rb/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fp_rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Martin Chabot
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-02-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: superators19
|
@@ -84,10 +84,8 @@ files:
|
|
84
84
|
- bin/console
|
85
85
|
- bin/setup
|
86
86
|
- fp_rb.gemspec
|
87
|
+
- lib/fn_reader.rb
|
87
88
|
- lib/fp_rb.rb
|
88
|
-
- lib/fp_rb/maybe.rb
|
89
|
-
- lib/fp_rb/operator.rb
|
90
|
-
- lib/fp_rb/result.rb
|
91
89
|
- lib/fp_rb/version.rb
|
92
90
|
homepage: ''
|
93
91
|
licenses:
|
@@ -108,8 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
106
|
- !ruby/object:Gem::Version
|
109
107
|
version: '0'
|
110
108
|
requirements: []
|
111
|
-
|
112
|
-
rubygems_version: 2.2.2
|
109
|
+
rubygems_version: 3.0.1
|
113
110
|
signing_key:
|
114
111
|
specification_version: 4
|
115
112
|
summary: Collection of utilities for programming in a more functional way
|
data/lib/fp_rb/maybe.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
module FpRb
|
2
|
-
class Maybe
|
3
|
-
def initialize(value)
|
4
|
-
@value = value
|
5
|
-
end
|
6
|
-
|
7
|
-
def with_default(default)
|
8
|
-
@value.nil? ? default : @value
|
9
|
-
end
|
10
|
-
|
11
|
-
def map(&block)
|
12
|
-
if @value.nil?
|
13
|
-
Maybe.new(block.(@value))
|
14
|
-
else
|
15
|
-
self
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def present?
|
20
|
-
!! @value.nil?
|
21
|
-
end
|
22
|
-
|
23
|
-
def blank?
|
24
|
-
!! @value.nil?
|
25
|
-
end
|
26
|
-
|
27
|
-
def to_result(error)
|
28
|
-
if @value
|
29
|
-
Result.ok(value)
|
30
|
-
else
|
31
|
-
Result.err(error)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
data/lib/fp_rb/operator.rb
DELETED
data/lib/fp_rb/result.rb
DELETED
@@ -1,87 +0,0 @@
|
|
1
|
-
module FpRb
|
2
|
-
class Result
|
3
|
-
def initialize(value: nil, error: nil)
|
4
|
-
raise "error or value must not be nil" if value.nil? && error.nil?
|
5
|
-
raise "error and value cannot be set at the same time. \n Error #{error}, Value #{value}" if ! value.nil? && ! error.nil?
|
6
|
-
@value = value
|
7
|
-
@error = error
|
8
|
-
end
|
9
|
-
|
10
|
-
def with_default(default)
|
11
|
-
if @value.nil?
|
12
|
-
default
|
13
|
-
else
|
14
|
-
@value
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def map(&block)
|
19
|
-
if @error
|
20
|
-
self
|
21
|
-
else
|
22
|
-
Result.ok(block.(@value))
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def and_then(&block)
|
27
|
-
if @error.nil?
|
28
|
-
block.(@value)
|
29
|
-
else
|
30
|
-
self
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def success?
|
35
|
-
!! @value
|
36
|
-
end
|
37
|
-
|
38
|
-
def error
|
39
|
-
@error
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.err(msg)
|
43
|
-
Result.new(error: Error.new(msg, Kernel.caller))
|
44
|
-
end
|
45
|
-
|
46
|
-
def self.ok(value)
|
47
|
-
Result.new(value: value)
|
48
|
-
end
|
49
|
-
|
50
|
-
def self.rescue(error_msg = nil, &block)
|
51
|
-
begin
|
52
|
-
Result.ok(block.())
|
53
|
-
rescue StandardError => e
|
54
|
-
Result.new(error: Error.new(e.inspect, e.backtrace))
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
def map_error(&block)
|
59
|
-
if @error
|
60
|
-
Result.new(error: block.(@error))
|
61
|
-
else
|
62
|
-
self
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
class Error
|
67
|
-
attr_accessor :msg, :backtrace
|
68
|
-
|
69
|
-
def initialize(msg, backtrace)
|
70
|
-
@msg = msg
|
71
|
-
@backtrace = backtrace
|
72
|
-
end
|
73
|
-
|
74
|
-
def msg
|
75
|
-
@msg
|
76
|
-
end
|
77
|
-
|
78
|
-
def details
|
79
|
-
<<EOF
|
80
|
-
ERROR = #{@msg}
|
81
|
-
Backtrace:
|
82
|
-
#{@backtrace.join("\n")}
|
83
|
-
EOF
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|