verbalize 1.0.1 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3597fd0a4fe2e3ee402bc43159dc23229910575d
4
- data.tar.gz: 550e8867c47c8242bc01640c111fa149d8d24d36
3
+ metadata.gz: af1f862ae8c6cbaef0af29e87c01e805cd667be3
4
+ data.tar.gz: 3080e524a6b251d8633dab8673951f75371058fa
5
5
  SHA512:
6
- metadata.gz: 042bc2b94824e56ef38fbd5ca2c0959f92e3ed95cb3fda6aa6a4eeeead5c31763ead385a42e9068c44d23ee52e7f689facfaaf6f9ee8d0694c4b283f8e10966d
7
- data.tar.gz: 2e5611c685bf82d323017990fc5040c61d7cd32bee5a1359760ce3b8830fda7be9dba43c1cc06216c132737e385c1aa8c790e91740f13ef650166c34051b2433
6
+ metadata.gz: ec18ba755d82586787fa4dfea0220a5321092ee8aad1a2791eb6088e3fa19aef645375e1f5dd7a4808a2dd55556db6f0b5beaa5a362c9f054bf4decb823199d7
7
+ data.tar.gz: 7abe67eebe6e6785b1e0ed92cd844a8819ff7cd7a3841f0b65655351ac57e36a376833446e3398052d95272df910ebf80996859d6d276b6479d4959a671ea98a
data/circle.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  machine:
2
2
  ruby:
3
- version: '2.1'
3
+ version: '2.2'
4
4
 
5
5
  test:
6
6
  post:
data/lib/verbalize.rb CHANGED
@@ -1,13 +1,15 @@
1
1
  require 'verbalize/version'
2
2
  require 'verbalize/build_initialize_method'
3
- require 'verbalize/build_action_method'
3
+ require 'verbalize/build_safe_action_method'
4
+ require 'verbalize/build_dangerous_action_method'
4
5
  require 'verbalize/build_attribute_readers'
5
6
  require 'verbalize/result'
6
7
 
7
8
  module Verbalize
8
- def fail!(failure_value)
9
- @verbalize_outcome = :error
10
- throw :verbalize_error, failure_value
9
+ VerbalizeError = Class.new(StandardError)
10
+
11
+ def fail!(failure_value = nil)
12
+ throw(:verbalize_error, Result.new(outcome: :error, value: failure_value))
11
13
  end
12
14
 
13
15
  def self.included(target)
@@ -36,7 +38,13 @@ module Verbalize
36
38
 
37
39
  optional_keywords = Array(optional)
38
40
 
39
- class_eval BuildActionMethod.call(
41
+ class_eval BuildSafeActionMethod.call(
42
+ required_keywords: required_keywords,
43
+ optional_keywords: optional_keywords,
44
+ method_name: method_name
45
+ )
46
+
47
+ class_eval BuildDangerousActionMethod.call(
40
48
  required_keywords: required_keywords,
41
49
  optional_keywords: optional_keywords,
42
50
  method_name: method_name
@@ -53,10 +61,22 @@ module Verbalize
53
61
  end
54
62
 
55
63
  def call
56
- action = new
57
- value = catch(:verbalize_error) { action.send(:call) }
58
- outcome = action.instance_variable_get(:@verbalize_outcome) || :ok
59
- Result.new(outcome: outcome, value: value)
64
+ action = new
65
+ result = catch(:verbalize_error) { action.send(:call) }
66
+ if result.is_a?(Result)
67
+ result
68
+ else
69
+ Result.new(outcome: :ok, value: result)
70
+ end
71
+ end
72
+
73
+ def call!
74
+ new.send(:call)
75
+ rescue UncaughtThrowError => uncaught_throw_error
76
+ fail_value = uncaught_throw_error.value.last
77
+ error = VerbalizeError.new("Unhandled fail! called with: #{fail_value.inspect}.")
78
+ error.set_backtrace(uncaught_throw_error.backtrace[2..-1])
79
+ raise error
60
80
  end
61
81
  end
62
82
  end
@@ -0,0 +1,21 @@
1
+ require_relative 'build_method_base'
2
+
3
+ module Verbalize
4
+ class BuildDangerousActionMethod < BuildMethodBase
5
+ private
6
+
7
+ def declaration
8
+ "def self.#{method_name}!(#{declaration_keyword_arguments})"
9
+ end
10
+
11
+ def body
12
+ <<-BODY.chomp
13
+ new(#{initialize_keywords_and_values}).send(:#{method_name})
14
+ rescue UncaughtThrowError => uncaught_throw_error
15
+ error = VerbalizeError.new("Unhandled fail! called with: \#{uncaught_throw_error.value.last.inspect}.")
16
+ error.set_backtrace(uncaught_throw_error.backtrace[2..-1])
17
+ raise error
18
+ BODY
19
+ end
20
+ end
21
+ end
@@ -12,7 +12,7 @@ module Verbalize
12
12
  return if all_keywords.empty?
13
13
 
14
14
  lines = all_keywords.map do |keyword|
15
- "@#{keyword} = #{keyword}"
15
+ " @#{keyword} = #{keyword}"
16
16
  end
17
17
 
18
18
  lines.join("\n")
@@ -22,7 +22,7 @@ class BuildMethodBase
22
22
  attr_reader :required_keywords, :optional_keywords, :method_name
23
23
 
24
24
  def parts
25
- [declaration, body, 'end']
25
+ [declaration, body, "end\n"]
26
26
  end
27
27
 
28
28
  def declaration
@@ -49,4 +49,8 @@ class BuildMethodBase
49
49
  return if all_keywords.empty?
50
50
  (required_keyword_segments + optional_keyword_segments).join(', ')
51
51
  end
52
+
53
+ def initialize_keywords_and_values
54
+ all_keywords.map { |variable| "#{variable}: #{variable}" }.join(', ')
55
+ end
52
56
  end
@@ -0,0 +1,23 @@
1
+ require_relative 'build_method_base'
2
+
3
+ module Verbalize
4
+ class BuildSafeActionMethod < BuildMethodBase
5
+ private
6
+
7
+ def declaration
8
+ "def self.#{method_name}(#{declaration_keyword_arguments})"
9
+ end
10
+
11
+ def body
12
+ <<-BODY.chomp
13
+ action = new(#{initialize_keywords_and_values})
14
+ result = catch(:verbalize_error) { action.send(#{method_name.inspect}) }
15
+ if result.is_a?(Result)
16
+ result
17
+ else
18
+ Result.new(outcome: :ok, value: result)
19
+ end
20
+ BODY
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module Verbalize
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.1.1'.freeze
3
3
  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: 1.0.1
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Taylor
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-10 00:00:00.000000000 Z
11
+ date: 2016-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -113,10 +113,11 @@ files:
113
113
  - bin/setup
114
114
  - circle.yml
115
115
  - lib/verbalize.rb
116
- - lib/verbalize/build_action_method.rb
117
116
  - lib/verbalize/build_attribute_readers.rb
117
+ - lib/verbalize/build_dangerous_action_method.rb
118
118
  - lib/verbalize/build_initialize_method.rb
119
119
  - lib/verbalize/build_method_base.rb
120
+ - lib/verbalize/build_safe_action_method.rb
120
121
  - lib/verbalize/result.rb
121
122
  - lib/verbalize/version.rb
122
123
  - verbalize.gemspec
@@ -1,24 +0,0 @@
1
- require_relative 'build_method_base'
2
-
3
- module Verbalize
4
- class BuildActionMethod < 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
- "value = catch(:verbalize_error) { action.send(#{method_name.inspect}) }",
15
- 'outcome = action.instance_variable_get(:@verbalize_outcome) || :ok',
16
- 'Result.new(outcome: outcome, value: value)'
17
- ].join("\n")
18
- end
19
-
20
- def initialize_keyword_arguments
21
- all_keywords.map { |variable| "#{variable}: #{variable}" }.join(', ')
22
- end
23
- end
24
- end