verbalize 2.2.0 → 2.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +4 -2
- data/CHANGELOG.md +9 -0
- data/README.md +34 -15
- data/lib/verbalize/action.rb +40 -0
- data/lib/verbalize/build.rb +6 -4
- data/lib/verbalize/version.rb +1 -1
- data/verbalize.gemspec +2 -2
- metadata +11 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 318052d8276c42ce2790698c6293930e55f6ae6a726e53e15279f4e562b8daeb
|
4
|
+
data.tar.gz: cb2e05c821a3e0af1e9c7ded9677c756b5eaf5e2466acd7ce63f46fd48a5cc5a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0a2d4819f7168e6062a2ddf1cd8b2881988371597604601ab8f40d7e7e730574bac68f189d7d0aea9f3d1ac878b0e28a4ebab205766935f2c7d202a16faf21ce
|
7
|
+
data.tar.gz: 31a1b94052096f62aef1238ef6f1a250f7dc5eabadca325ee1c730feaba78e24492f7e4d931363c9f7743abc736cf0da178e1f586bc3fe5fd53fc9941cf8627d
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,15 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
5
5
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
6
6
|
|
7
|
+
## [Unreleased]
|
8
|
+
### Added
|
9
|
+
- release documentation
|
10
|
+
|
11
|
+
## 2.3.0 - 2019-09-23
|
12
|
+
### Added
|
13
|
+
- you can now specify `validates :key { |value| ... }` on actions to run validation on the input
|
14
|
+
*before* running the `call` block.
|
15
|
+
|
7
16
|
## 2.2.0 - 2017-08-19
|
8
17
|
### Added
|
9
18
|
- changelog started
|
data/README.md
CHANGED
@@ -125,6 +125,25 @@ Add.optional_inputs # [:c, :d]
|
|
125
125
|
Add.inputs # [:a, :b, :c, :d]
|
126
126
|
```
|
127
127
|
|
128
|
+
## Validation
|
129
|
+
```ruby
|
130
|
+
class FloatAdd
|
131
|
+
include Verbalize::Action
|
132
|
+
|
133
|
+
input :a, :b
|
134
|
+
validate(:a) { |a| a.is_a?(Float) }
|
135
|
+
validate(:b) { |b| b.is_a?(Float) && b > 10.0 }
|
136
|
+
|
137
|
+
def call
|
138
|
+
a + b
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
FloatAdd.call!(a: 1, b: 1) # fails with Input "a" failed validation!
|
143
|
+
FloatAdd.call!(a: 1.0, b: 1.0) # fails with Input "b" failed validation!
|
144
|
+
FloatAdd.call!(a: 1.0, b: 12.0) # 13.0
|
145
|
+
```
|
146
|
+
|
128
147
|
## Comparison/Benchmark
|
129
148
|
```ruby
|
130
149
|
require 'verbalize'
|
@@ -188,21 +207,21 @@ end
|
|
188
207
|
|
189
208
|
```
|
190
209
|
Warming up --------------------------------------
|
191
|
-
Ruby
|
192
|
-
Verbalize
|
193
|
-
Actionizer
|
194
|
-
Interactor 4.
|
210
|
+
Ruby 63.091k i/100ms
|
211
|
+
Verbalize 40.521k i/100ms
|
212
|
+
Actionizer 5.226k i/100ms
|
213
|
+
Interactor 4.874k i/100ms
|
195
214
|
Calculating -------------------------------------
|
196
|
-
Ruby
|
197
|
-
Verbalize
|
198
|
-
Actionizer
|
199
|
-
Interactor
|
215
|
+
Ruby 751.604k (± 3.0%) i/s - 3.785M in 5.041472s
|
216
|
+
Verbalize 457.598k (± 6.1%) i/s - 2.310M in 5.072488s
|
217
|
+
Actionizer 54.874k (± 3.5%) i/s - 276.978k in 5.054541s
|
218
|
+
Interactor 52.294k (± 3.2%) i/s - 263.196k in 5.038365s
|
200
219
|
|
201
220
|
Comparison:
|
202
|
-
Ruby:
|
203
|
-
Verbalize:
|
204
|
-
Actionizer:
|
205
|
-
Interactor:
|
221
|
+
Ruby: 751604.0 i/s
|
222
|
+
Verbalize: 457597.9 i/s - 1.64x slower
|
223
|
+
Actionizer: 54873.6 i/s - 13.70x slower
|
224
|
+
Interactor: 52293.6 i/s - 14.37x slower
|
206
225
|
|
207
226
|
```
|
208
227
|
|
@@ -355,10 +374,10 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
355
374
|
|
356
375
|
## Releasing
|
357
376
|
|
358
|
-
- Update
|
359
|
-
- Commit to master and push to github
|
377
|
+
- Update CHANGELOG.md, and lib/verbalize/version.rb, commit, and push
|
360
378
|
- [Create a new release on github](https://github.com/taylorzr/verbalize/releases/new)
|
361
|
-
-
|
379
|
+
- Build the gem version: `gem build verbalize.gemspec`
|
380
|
+
- Push the gem version to rubygems: `gem push verbalize-<VERSION>.gem`
|
362
381
|
|
363
382
|
## Contributing
|
364
383
|
|
data/lib/verbalize/action.rb
CHANGED
@@ -11,10 +11,25 @@ module Verbalize
|
|
11
11
|
throw(THROWN_SYMBOL, failure_value)
|
12
12
|
end
|
13
13
|
|
14
|
+
def action_inputs
|
15
|
+
self.class.inputs.map { |i| [i, self.send(i)] }.to_h
|
16
|
+
end
|
17
|
+
|
14
18
|
def self.included(target)
|
15
19
|
target.extend ClassMethods
|
16
20
|
end
|
17
21
|
|
22
|
+
private
|
23
|
+
|
24
|
+
def __setup(key, value)
|
25
|
+
is_valid = self.class.input_is_valid?(key, value)
|
26
|
+
local_error = self.class.pop_local_error
|
27
|
+
fail!(local_error) if !is_valid && local_error
|
28
|
+
fail! "Input '#{key}' failed validation!" unless is_valid
|
29
|
+
|
30
|
+
instance_variable_set(:"@#{key}", value)
|
31
|
+
end
|
32
|
+
|
18
33
|
module ClassMethods
|
19
34
|
def required_inputs
|
20
35
|
@required_inputs || []
|
@@ -36,6 +51,26 @@ module Verbalize
|
|
36
51
|
@defaults
|
37
52
|
end
|
38
53
|
|
54
|
+
def input_validations
|
55
|
+
@input_validations ||= {}
|
56
|
+
end
|
57
|
+
|
58
|
+
def input_is_valid?(input, value)
|
59
|
+
return true unless input_validations.include?(input.to_sym)
|
60
|
+
|
61
|
+
input_validations[input].call(value) == true
|
62
|
+
rescue => e
|
63
|
+
@local_error = e
|
64
|
+
false
|
65
|
+
end
|
66
|
+
|
67
|
+
def pop_local_error
|
68
|
+
return nil unless @local_error
|
69
|
+
@local_error
|
70
|
+
ensure
|
71
|
+
@local_error = nil
|
72
|
+
end
|
73
|
+
|
39
74
|
# Because call/call! are defined when Action.input is called, they would
|
40
75
|
# not be defined when there is no input. So we pre-define them here, and
|
41
76
|
# if there is any input, they are overwritten
|
@@ -59,6 +94,11 @@ module Verbalize
|
|
59
94
|
class_eval Build.call(required_inputs, optional_inputs, default_inputs)
|
60
95
|
end
|
61
96
|
|
97
|
+
def validate(keyword, &block)
|
98
|
+
raise Verbalize::Error, 'Missing block to validate against!' unless block_given?
|
99
|
+
input_validations[keyword.to_sym] = block
|
100
|
+
end
|
101
|
+
|
62
102
|
def assign_defaults(optional)
|
63
103
|
@defaults = optional.select { |kw| kw.is_a?(Hash) }.reduce(&:merge)
|
64
104
|
@defaults = (@defaults || {})
|
data/lib/verbalize/build.rb
CHANGED
@@ -44,9 +44,9 @@ attr_reader #{attribute_readers_string}
|
|
44
44
|
end
|
45
45
|
|
46
46
|
def declaration_arguments_string
|
47
|
-
required_segments = required_keywords.map { |
|
48
|
-
optional_segments = optional_keywords.map { |
|
49
|
-
default_segments = default_keywords.map { |
|
47
|
+
required_segments = required_keywords.map { |kw| "#{kw}:" }
|
48
|
+
optional_segments = optional_keywords.map { |kw| "#{kw}: nil" }
|
49
|
+
default_segments = default_keywords.map { |kw| "#{kw}: self.defaults[:#{kw}].call" }
|
50
50
|
(required_segments + optional_segments + default_segments).join(', ')
|
51
51
|
end
|
52
52
|
|
@@ -55,7 +55,9 @@ attr_reader #{attribute_readers_string}
|
|
55
55
|
end
|
56
56
|
|
57
57
|
def initialize_body
|
58
|
-
all_keywords.map
|
58
|
+
all_keywords.map do |keyword|
|
59
|
+
"__setup(:#{keyword}, #{keyword})"
|
60
|
+
end.join("\n ")
|
59
61
|
end
|
60
62
|
|
61
63
|
def attribute_readers_string
|
data/lib/verbalize/version.rb
CHANGED
data/verbalize.gemspec
CHANGED
@@ -21,9 +21,9 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.require_paths = ['lib']
|
22
22
|
|
23
23
|
spec.add_development_dependency 'bundler'
|
24
|
-
spec.add_development_dependency 'rake'
|
24
|
+
spec.add_development_dependency 'rake'
|
25
25
|
spec.add_development_dependency 'rspec'
|
26
26
|
spec.add_development_dependency 'coveralls'
|
27
|
-
spec.add_development_dependency 'rubocop'
|
27
|
+
spec.add_development_dependency 'rubocop'
|
28
28
|
spec.add_development_dependency 'pry'
|
29
29
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: verbalize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zach Taylor
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-09-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -70,16 +70,16 @@ dependencies:
|
|
70
70
|
name: rubocop
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '0
|
75
|
+
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '0
|
82
|
+
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: pry
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -142,8 +142,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
142
142
|
- !ruby/object:Gem::Version
|
143
143
|
version: '0'
|
144
144
|
requirements: []
|
145
|
-
|
146
|
-
rubygems_version: 2.6.11
|
145
|
+
rubygems_version: 3.0.3
|
147
146
|
signing_key:
|
148
147
|
specification_version: 4
|
149
148
|
summary: Verb based class pattern
|