functo 0.1.0 → 0.1.1
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/README.md +57 -0
- data/lib/functo/version.rb +1 -1
- data/lib/functo.rb +15 -3
- 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: 2083fd9241f81ae8e66afd0db7f287b11c2fc56c
|
4
|
+
data.tar.gz: 66a47b6423663ec128fc2420c70dc47031503bf7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54c9d97f8a56098aff7e9fa499e5643ffe3e8e095f942d193e13731118d082de0f69dedc39a25b4c907e0a908d852b3e8fbcb9fe33ccfbfbc8eb35c720b26386
|
7
|
+
data.tar.gz: 94dc2a1e06f415cf1a49dc5b762e95848d0c4a8299bed7bbef6c2da07ceb08263c253a3bb1dd1a0c8ad4b70e8ad46a79381ed51e0af2e245c4c690fd4291de6b
|
data/README.md
CHANGED
@@ -135,6 +135,63 @@ SumDigits2[123]
|
|
135
135
|
# => ArgumentError: wrong number of arguments (given 1, expected 3)
|
136
136
|
```
|
137
137
|
|
138
|
+
### Types
|
139
|
+
|
140
|
+
Types and coercion are not built in to Functo but they can be achieved by passing anything that responds to `[]` to the constructor.
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
class ValidatesNumeric
|
144
|
+
include Functo.call :validate, :number
|
145
|
+
|
146
|
+
ValidationError = Class.new(StandardError)
|
147
|
+
|
148
|
+
def validate
|
149
|
+
raise ValidationError unless number.is_a?(Numeric)
|
150
|
+
|
151
|
+
number
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
class AddsThree
|
156
|
+
include Functo.call :add, number: ValidatesNumeric
|
157
|
+
|
158
|
+
def add
|
159
|
+
number + 2
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
AddsThree[10]
|
164
|
+
# => 13
|
165
|
+
|
166
|
+
AddsThree['10']
|
167
|
+
# => ValidationError
|
168
|
+
```
|
169
|
+
|
170
|
+
You could use the `dry-types` gem for example.
|
171
|
+
|
172
|
+
```ruby
|
173
|
+
require 'dry-types'
|
174
|
+
|
175
|
+
module Types
|
176
|
+
include Dry::Types.module
|
177
|
+
end
|
178
|
+
|
179
|
+
class AddsFour
|
180
|
+
include Functo.call :add, number: Types::Strict::Int
|
181
|
+
|
182
|
+
def add
|
183
|
+
number + 4
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
AddsFour[4]
|
188
|
+
# => 4
|
189
|
+
|
190
|
+
AddsFour['4']
|
191
|
+
# => Dry::Types::ConstraintError
|
192
|
+
|
193
|
+
```
|
194
|
+
|
138
195
|
## Acknowledgements
|
139
196
|
|
140
197
|
Functo was inspired by these gems:
|
data/lib/functo/version.rb
CHANGED
data/lib/functo.rb
CHANGED
@@ -8,17 +8,26 @@ class Functo < Module
|
|
8
8
|
def self.call(*names)
|
9
9
|
output = names.shift
|
10
10
|
|
11
|
+
if names.first.is_a?(Hash)
|
12
|
+
inputs = names.first
|
13
|
+
filters = inputs.values
|
14
|
+
names = inputs.keys
|
15
|
+
else
|
16
|
+
filters = []
|
17
|
+
end
|
18
|
+
|
11
19
|
if names.length > MAX_ARGUMENTS
|
12
20
|
raise ArgumentError.new("given #{names.length} arguments when only #{MAX_ARGUMENTS} are allowed")
|
13
21
|
end
|
14
22
|
|
15
|
-
new(names, output)
|
23
|
+
new(names, filters, output)
|
16
24
|
end
|
17
25
|
|
18
26
|
private
|
19
27
|
|
20
|
-
def initialize(inputs, output)
|
21
|
-
@inputs =
|
28
|
+
def initialize(inputs, filters, output)
|
29
|
+
@inputs = inputs
|
30
|
+
@filters = filters
|
22
31
|
@output = output
|
23
32
|
@inputs_module = Module.new
|
24
33
|
@output_module = Module.new
|
@@ -37,6 +46,7 @@ class Functo < Module
|
|
37
46
|
|
38
47
|
def define_initialize
|
39
48
|
ivars = @inputs.map { |name| "@#{name}" }
|
49
|
+
filters = @filters
|
40
50
|
size = @inputs.size
|
41
51
|
|
42
52
|
@inputs_module.class_eval do
|
@@ -47,6 +57,8 @@ class Functo < Module
|
|
47
57
|
fail ArgumentError, "wrong number of arguments (#{args_size} for #{size})"
|
48
58
|
end
|
49
59
|
|
60
|
+
args = args.zip(filters).map { |arg, f| f.respond_to?(:[]) ? f[arg] : arg }
|
61
|
+
|
50
62
|
ivars.zip(args) { |ivar, arg| instance_variable_set(ivar, arg) }
|
51
63
|
end
|
52
64
|
|