functo 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/functo.rb +30 -6
- data/lib/functo/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 11f0d93203f4181a8d9783fefd67aba0ae43856d
|
4
|
+
data.tar.gz: d3c4fc5ced337e6cc26cbd5b664541037692f6e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c3c28d9beeef0a14e599e2faaf53233a84a838858c250bbc1a7b8be5d1e085b5efcb0e572164cd3be227fa80a89999d985577fe1155b2417c486b074b220ca70
|
7
|
+
data.tar.gz: e1541a748c161c5d2eacf7dd093fcb83ed171bead03d7eea0318f21f250a11e54272fca809e3f35389e1d103c8a5518a6476c9cbd031726f4f7ac88768f9c9b3
|
data/lib/functo.rb
CHANGED
@@ -2,11 +2,23 @@ require "functo/version"
|
|
2
2
|
|
3
3
|
class Functo < Module
|
4
4
|
MAX_ARGUMENTS = 3
|
5
|
+
PASS = '__FUNCTO_PASS__'.freeze
|
5
6
|
|
6
7
|
private_class_method :new
|
7
8
|
|
9
|
+
def self.wrap(obj)
|
10
|
+
klass = Class.new
|
11
|
+
|
12
|
+
klass.define_singleton_method :call do |*args|
|
13
|
+
obj.call(*args)
|
14
|
+
end
|
15
|
+
|
16
|
+
klass.extend(ClassMethods)
|
17
|
+
klass
|
18
|
+
end
|
19
|
+
|
8
20
|
def self.pass
|
9
|
-
|
21
|
+
PASS
|
10
22
|
end
|
11
23
|
|
12
24
|
def self.call(*names)
|
@@ -17,7 +29,7 @@ class Functo < Module
|
|
17
29
|
filters = inputs.values
|
18
30
|
names = inputs.keys
|
19
31
|
else
|
20
|
-
filters = []
|
32
|
+
filters = [pass] * names.length
|
21
33
|
end
|
22
34
|
|
23
35
|
if names.length > MAX_ARGUMENTS
|
@@ -50,7 +62,7 @@ class Functo < Module
|
|
50
62
|
|
51
63
|
def define_initialize
|
52
64
|
ivars = @inputs.map { |name| "@#{name}" }
|
53
|
-
|
65
|
+
filter_proc = method(:apply_filters).to_proc
|
54
66
|
size = @inputs.size
|
55
67
|
|
56
68
|
@inputs_module.class_eval do
|
@@ -61,8 +73,7 @@ class Functo < Module
|
|
61
73
|
fail ArgumentError, "wrong number of arguments (#{args_size} for #{size})"
|
62
74
|
end
|
63
75
|
|
64
|
-
args =
|
65
|
-
|
76
|
+
args = filter_proc.(args)
|
66
77
|
ivars.zip(args) { |ivar, arg| instance_variable_set(ivar, arg) }
|
67
78
|
end
|
68
79
|
|
@@ -89,6 +100,20 @@ class Functo < Module
|
|
89
100
|
end
|
90
101
|
end
|
91
102
|
|
103
|
+
def apply_filters(args)
|
104
|
+
args.zip(@filters).map do |arg, filter|
|
105
|
+
if filter === Functo.pass
|
106
|
+
arg
|
107
|
+
elsif filter.respond_to?(:[])
|
108
|
+
filter[arg]
|
109
|
+
elsif filter.respond_to?(:call)
|
110
|
+
filter.call(arg)
|
111
|
+
else
|
112
|
+
raise ArgumentError.new("filters must respond to `[]` or `call`")
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
92
117
|
module ClassMethods
|
93
118
|
def [](*args)
|
94
119
|
call(*args)
|
@@ -111,7 +136,6 @@ class Functo < Module
|
|
111
136
|
end
|
112
137
|
|
113
138
|
klass.extend(ClassMethods)
|
114
|
-
|
115
139
|
klass
|
116
140
|
end
|
117
141
|
|
data/lib/functo/version.rb
CHANGED