verbalize 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f008ed2bf42154162943268c8c10d75ca09ca586
4
+ data.tar.gz: fdc71af9ad1a73d1f49ce85ec2bbc94a4870c1ac
5
+ SHA512:
6
+ metadata.gz: 85ba485dee2564adb6cb8a3d3ca073a05f7cf27f65ef94599664575e64ae10d2d009c3d0bc404e1dc332e88f95c778f8e2bf7ff311cc314e3feace3fd0728f69
7
+ data.tar.gz: 5cda9a96c5e0162fd9f3f16645f85dcd8cefb6a1b8c0db49091b7ce4aff594eeedc2e28ba1fa9f229e1589f7d8fe77beed2a992cf604ed00cd920ef47d143083
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.1
3
+
4
+ Metrics/LineLength:
5
+ Max: 90
6
+
7
+ Documentation:
8
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in verbalize.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Zach Taylor
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,185 @@
1
+ # Verbalize
2
+
3
+ [![Build Status](https://circleci.com/gh/taylorzr/verbalize.svg?style=shield&circle-token=58dcd03ffacd1c21e57766a0fba6b2008bafd777)](https://circleci.com/gh/taylorzr/verbalize/tree/master) [![Coverage Status](https://coveralls.io/repos/github/taylorzr/verbalize/badge.svg?branch=master)](https://coveralls.io/github/taylorzr/verbalize?branch=master)
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ class Add
9
+ include Verbalize
10
+
11
+ input :a, :b
12
+
13
+ def call
14
+ a + b
15
+ end
16
+ end
17
+
18
+ result = Add.call(a: 35, b: 7)
19
+ result.outcome # => :ok
20
+ result.value # => 42
21
+ result.succeeded? # => true
22
+ result.failed? # => false
23
+
24
+ outcome, value = Add.call(a: 35, b: 7)
25
+ outcome # => :ok
26
+ value # => 42
27
+ ```
28
+
29
+ ```ruby
30
+ class Add
31
+ include Verbalize
32
+
33
+ input :a, :b
34
+
35
+ def call
36
+ a + b
37
+ end
38
+
39
+ private
40
+
41
+ def a
42
+ @a ||= 35
43
+ end
44
+
45
+ def b
46
+ @b ||= 7
47
+ end
48
+ end
49
+
50
+ Add.call # => [:ok, 42]
51
+ Add.call(a: 42, b: 0) # => [:ok, 42]
52
+ ```
53
+
54
+ ```ruby
55
+ class Divide
56
+ include Verbalize
57
+
58
+ input :a, :b
59
+
60
+ def call
61
+ fail! 'You can’t divide by 0' if b == 0
62
+ a / b
63
+ end
64
+ end
65
+
66
+ result = Divide.call(a: 1, b: 0) # => [:error, 'You can’t divide by 0']
67
+ result.failed? # => true
68
+ ```
69
+
70
+ ## Comparison/Benchmark
71
+ ```ruby
72
+ class RubyAdd
73
+ def self.call(a:, b:)
74
+ new(a: a, b: b).call
75
+ end
76
+
77
+ def initialize(a:, b:)
78
+ @a = a
79
+ @b = b
80
+ end
81
+
82
+ def call
83
+ a + b
84
+ end
85
+
86
+ private
87
+
88
+ attr_reader :a, :b
89
+ end
90
+ ```
91
+
92
+ ```ruby
93
+ class VerbalizeAdd
94
+ include Verbalize
95
+
96
+ input :a, :b
97
+
98
+ def call
99
+ a + b
100
+ end
101
+ end
102
+ ```
103
+
104
+ ```ruby
105
+ class ActionizerAdd
106
+ include Actionizer
107
+
108
+ def call
109
+ output.sum = input.a + input.b
110
+ end
111
+ end
112
+ ```
113
+
114
+ ```ruby
115
+ class InteractorAdd
116
+ include Interactor
117
+
118
+ def call
119
+ context.sum = context.a + context.b
120
+ end
121
+ end
122
+ ```
123
+
124
+ ```ruby
125
+ require 'benchmark/ips'
126
+
127
+ Benchmark.ips do |x|
128
+ x.report('Ruby') { RubyAdd.call(a: 1, b: 2) }
129
+ x.report('Verbal') { VerbalAdd.call(a: 1, b: 2) }
130
+ x.report('Actionizer') { ActionizerAdd.call(a: 1, b: 2) }
131
+ x.report('Interactor') { InteractorAdd.call(a: 1, b: 2) }
132
+ x.compare!
133
+ end
134
+ ```
135
+
136
+ ```
137
+ Calculating -------------------------------------
138
+ Interactor 4619 i/100ms
139
+ Actionizer 4919 i/100ms
140
+ Verbal 21841 i/100ms
141
+ Ruby 43212 i/100ms
142
+ -------------------------------------------------
143
+ Interactor 46966.6 (±7.5%) i/s - 235569 in 5.046586s
144
+ Actionizer 48493.5 (±6.0%) i/s - 245950 in 5.091045s
145
+ Verbal 259273.2 (±4.7%) i/s - 1310460 in 5.065844s
146
+ Ruby 618459.0 (±5.4%) i/s - 3111264 in 5.046011s
147
+
148
+ Comparison:
149
+ Ruby: 618459.0 i/s
150
+ Verbal: 259273.2 i/s - 2.39x slower
151
+ Actionizer: 48493.5 i/s - 12.75x slower
152
+ Interactor: 46966.6 i/s - 13.17x slower
153
+ ```
154
+
155
+ ## Installation
156
+
157
+ Add this line to your application's Gemfile:
158
+
159
+ ```ruby
160
+ gem 'verbalize'
161
+ ```
162
+
163
+ And then execute:
164
+
165
+ $ bundle
166
+
167
+ Or install it yourself as:
168
+
169
+ $ gem install verbalize
170
+
171
+ ## Development
172
+
173
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
174
+
175
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
176
+
177
+ ## Contributing
178
+
179
+ Bug reports and pull requests are welcome on GitHub at https://github.com/taylorzr/verbalize.
180
+
181
+
182
+ ## License
183
+
184
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
185
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'verbalize'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/circle.yml ADDED
@@ -0,0 +1,7 @@
1
+ machine:
2
+ ruby:
3
+ version: '2.1'
4
+
5
+ test:
6
+ post:
7
+ - bundle exec rubocop
data/lib/verbalize.rb ADDED
@@ -0,0 +1,42 @@
1
+ require 'verbalize/version'
2
+ require 'verbalize/build_initialize'
3
+ require 'verbalize/build_action'
4
+ require 'verbalize/build_attributes'
5
+ require 'verbalize/build_argument_validator'
6
+ require 'verbalize/result'
7
+
8
+ module Verbalize
9
+ def outcome
10
+ @outcome = @fail || :ok
11
+ end
12
+
13
+ def fail!(failure_value)
14
+ @fail = :error
15
+ throw :verbalize_error, failure_value
16
+ end
17
+
18
+ def self.included(target)
19
+ target.extend ClassMethods
20
+ end
21
+
22
+ module ClassMethods
23
+ def call
24
+ action = new
25
+ value = catch(:verbalize_error) { action.call }
26
+ Result.new(outcome: action.outcome, value: value)
27
+ end
28
+
29
+ def input(*arguments, verbalize_method_name: :call, **keyword_arguments)
30
+ raise ArgumentError unless keyword_arguments.empty?
31
+ class_eval BuildAction.new(arguments, verbalize_method_name).build
32
+ class_eval BuildInitialize.new(arguments).build
33
+ class_eval BuildAttributes.new(arguments).build
34
+ class_eval BuildArgumentValidator.new(arguments).build
35
+ end
36
+
37
+ def verbalize(*arguments, **keyword_arguments)
38
+ method_name, *arguments = arguments
39
+ input(*arguments, verbalize_method_name: method_name, **keyword_arguments)
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,24 @@
1
+ require_relative 'build_method_base'
2
+
3
+ module Verbalize
4
+ class BuildAction < BuildMethodBase
5
+ private
6
+
7
+ def declaration
8
+ "def self.#{method_name}(#{declaration_keyword_arguments})"
9
+ end
10
+
11
+ def body
12
+ [
13
+ "action = new(#{initialize_keyword_arguments})",
14
+ '_verbalize_validate_arguments(action)',
15
+ "value = catch(:verbalize_error) { action.#{method_name} }",
16
+ 'Result.new(outcome: action.outcome, value: value)'
17
+ ].join("\n")
18
+ end
19
+
20
+ def initialize_keyword_arguments
21
+ keywords.map { |variable| "#{variable}: #{variable}" }.join(', ')
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,36 @@
1
+ require_relative 'build_method_base'
2
+
3
+ module Verbalize
4
+ class BuildArgumentValidator < BuildMethodBase
5
+ private
6
+
7
+ def declaration
8
+ 'def self._verbalize_validate_arguments(instance)'
9
+ end
10
+
11
+ def body
12
+ return if keywords.empty?
13
+ (check_values + raise_error_if_necessary).join("\n")
14
+ end
15
+
16
+ def check_values
17
+ [
18
+ "keywords = [#{keywords.map(&:inspect).join(', ')}]",
19
+ 'keywords_without_values = keywords.select do |keyword|',
20
+ ' instance.send(keyword).nil?',
21
+ 'end'
22
+ ]
23
+ end
24
+
25
+ def raise_error_if_necessary
26
+ [
27
+ 'if keywords_without_values.any?',
28
+ " error_message = 'missing keyword'",
29
+ " error_message += 's' if keywords_without_values.count > 1",
30
+ %q{ error_message += ": #{keywords_without_values.join(', ')}"},
31
+ ' raise ArgumentError.new(error_message)',
32
+ 'end'
33
+ ]
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,16 @@
1
+ module Verbalize
2
+ class BuildAttributes
3
+ def initialize(keywords)
4
+ @keywords = keywords
5
+ end
6
+
7
+ def build
8
+ return '' if keywords.empty?
9
+ "attr_reader #{keywords.map(&:inspect).join ', '}"
10
+ end
11
+
12
+ private
13
+
14
+ attr_reader :keywords
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ require_relative 'build_method_base'
2
+
3
+ module Verbalize
4
+ class BuildInitialize < BuildMethodBase
5
+ private
6
+
7
+ def declaration
8
+ "def initialize(#{declaration_keyword_arguments})"
9
+ end
10
+
11
+ def body
12
+ return if keywords.empty?
13
+
14
+ lines = keywords.map do |keyword|
15
+ "@#{keyword} = #{keyword}"
16
+ end
17
+
18
+ lines.join("\n")
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,31 @@
1
+ class BuildMethodBase
2
+ def initialize(keywords, method_name = :call)
3
+ @keywords = keywords
4
+ @method_name = method_name
5
+ end
6
+
7
+ def build
8
+ parts.compact.join "\n"
9
+ end
10
+
11
+ private
12
+
13
+ attr_reader :keywords, :method_name
14
+
15
+ def parts
16
+ [declaration, body, 'end']
17
+ end
18
+
19
+ def declaration
20
+ raise NotImplementedError
21
+ end
22
+
23
+ def body
24
+ raise NotImplementedError
25
+ end
26
+
27
+ def declaration_keyword_arguments
28
+ return if keywords.empty?
29
+ keywords.map { |keyword| "#{keyword}: nil" }.join(', ')
30
+ end
31
+ end
@@ -0,0 +1,23 @@
1
+ module Verbalize
2
+ class Result < Array
3
+ def initialize(outcome:, value:)
4
+ super([outcome, value])
5
+ end
6
+
7
+ def succeeded?
8
+ !failed?
9
+ end
10
+
11
+ def failed?
12
+ outcome == :error
13
+ end
14
+
15
+ def outcome
16
+ first
17
+ end
18
+
19
+ def value
20
+ last
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Verbalize
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/verbalize.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'verbalize/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'verbalize'
8
+ spec.version = Verbalize::VERSION
9
+ spec.authors = ['Zach Taylor']
10
+ spec.email = ['taylorzr@gmail.com']
11
+
12
+ spec.summary = 'Verb based class pattern'
13
+ spec.homepage = 'https://github.com/taylorzr/verbalize'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_development_dependency 'bundler'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'coveralls'
27
+ spec.add_development_dependency 'rubocop'
28
+ spec.add_development_dependency 'pry'
29
+ end
metadata ADDED
@@ -0,0 +1,148 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: verbalize
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Zach Taylor
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - taylorzr@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".rubocop.yml"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - circle.yml
115
+ - lib/verbalize.rb
116
+ - lib/verbalize/build_action.rb
117
+ - lib/verbalize/build_argument_validator.rb
118
+ - lib/verbalize/build_attributes.rb
119
+ - lib/verbalize/build_initialize.rb
120
+ - lib/verbalize/build_method_base.rb
121
+ - lib/verbalize/result.rb
122
+ - lib/verbalize/version.rb
123
+ - verbalize.gemspec
124
+ homepage: https://github.com/taylorzr/verbalize
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.4.8
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: Verb based class pattern
148
+ test_files: []