cuprum 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/DEVELOPMENT.md +0 -4
- data/README.md +40 -0
- data/lib/cuprum.rb +4 -0
- data/lib/cuprum/function.rb +0 -2
- data/lib/cuprum/operation.rb +85 -0
- data/lib/cuprum/version.rb +1 -1
- metadata +9 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 457807ef9d8b39d6980467ad264972081f01b9ec
|
4
|
+
data.tar.gz: 92fedf44dfd331ef25e1429c8f35c2af7ede6180
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef543f82ad31ce828f01d4bfea7805da392ea524f4477af24ca77cb80b77ad623d9439af35ef2f8f5ef6d25b119e03dec104b73db966e287f2ebfc8141ab70de
|
7
|
+
data.tar.gz: 73c642f2383e11273184ec3145ab61808cd80646bdf1687ca9abb94a439d3f9bfbc12fceb4806845c49ac7b91eda570997a2e2d669a2c566da5dbca88a9e4df4
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 0.2.0
|
4
|
+
|
5
|
+
The "Fully Armed and Operational" Update.
|
6
|
+
|
7
|
+
### Operations
|
8
|
+
|
9
|
+
Implemented `Cuprum::Operation`. As a Function, but with an additional trick of tracking its own most recent execution result.
|
10
|
+
|
3
11
|
## 0.1.0
|
4
12
|
|
5
13
|
Initial version.
|
data/DEVELOPMENT.md
CHANGED
data/README.md
CHANGED
@@ -104,6 +104,46 @@ In addition, the result object defines `#success?` and `#failure?` predicates. I
|
|
104
104
|
result.failure? #=> true
|
105
105
|
result.value #=> nil
|
106
106
|
|
107
|
+
### Operations
|
108
|
+
|
109
|
+
An Operation is like a Function, but with an additional trick of tracking its own most recent execution result. This allows us to simplify some conditional logic, especially boilerplate code used to interact with frameworks.
|
110
|
+
|
111
|
+
class CreateBookOperation < Cuprum::Operation
|
112
|
+
def process
|
113
|
+
# Implementation here.
|
114
|
+
end # method process
|
115
|
+
end # class
|
116
|
+
|
117
|
+
def create
|
118
|
+
operation = CreateBookOperation.new.call(book_params)
|
119
|
+
|
120
|
+
if operation.success?
|
121
|
+
redirect_to(operation.value)
|
122
|
+
else
|
123
|
+
@book = operation.value
|
124
|
+
|
125
|
+
render :new
|
126
|
+
end # if-else
|
127
|
+
end # create
|
128
|
+
|
129
|
+
Like a Function, an Operation can be defined directly by passing an implementation block to the constructor or by creating a subclass that overwrites the #process method.
|
130
|
+
|
131
|
+
An operation inherits the `#call` method from Cuprum::Function (see above), and delegates the `#value`, `#errors`, `#success?`, and `#failure` methods to the most recent result (see below). If the operation has not been called, the operation will return default values.
|
132
|
+
|
133
|
+
In addition, an operation defines the following methods:
|
134
|
+
|
135
|
+
#### `#result`
|
136
|
+
|
137
|
+
The most recent result, from the previous time `#call` was executed for the operation.
|
138
|
+
|
139
|
+
#### `#called?`
|
140
|
+
|
141
|
+
True if the operation has been called and there is a result available by calling `#result` or one of the delegated methods, otherwise false.
|
142
|
+
|
143
|
+
#### `#reset!`
|
144
|
+
|
145
|
+
Clears the most recent result and resets `#called?` to false. This frees the result and any linked data for garbage collection. It also clears any internal state from the operation.
|
146
|
+
|
107
147
|
### Results
|
108
148
|
|
109
149
|
A Cuprum::Result is a data object that encapsulates the result of calling a Cuprum function - the returned value, the success or failure status, and any errors generated by the function. It defines the following methods:
|
data/lib/cuprum.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# A lightweight, functional-lite toolkit for making business logic a first-class
|
2
2
|
# citizen of your application.
|
3
3
|
module Cuprum
|
4
|
+
autoload :Function, 'cuprum/function'
|
5
|
+
autoload :Operation, 'cuprum/operation'
|
6
|
+
autoload :Result, 'cuprum/result'
|
7
|
+
|
4
8
|
# @return [String] The current version of the gem.
|
5
9
|
def self.version
|
6
10
|
VERSION
|
data/lib/cuprum/function.rb
CHANGED
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'cuprum/function'
|
2
|
+
|
3
|
+
module Cuprum
|
4
|
+
# Functional object that with syntactic sugar for tracking the last result.
|
5
|
+
#
|
6
|
+
# An Operation is like a Function, but with an additional trick of tracking
|
7
|
+
# its own most recent execution result. This allows us to simplify some
|
8
|
+
# conditional logic, especially boilerplate code used to interact with
|
9
|
+
# frameworks.
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
# def create
|
13
|
+
# operation = CreateBookOperation.new.call(book_params)
|
14
|
+
#
|
15
|
+
# if operation.success?
|
16
|
+
# redirect_to(operation.value)
|
17
|
+
# else
|
18
|
+
# @book = operation.value
|
19
|
+
#
|
20
|
+
# render :new
|
21
|
+
# end # if-else
|
22
|
+
# end # create
|
23
|
+
#
|
24
|
+
# Like a Function, an Operation can be defined directly by passing an
|
25
|
+
# implementation block to the constructor or by creating a subclass that
|
26
|
+
# overwrites the #process method.
|
27
|
+
#
|
28
|
+
# @see Cuprum::Function
|
29
|
+
class Operation < Cuprum::Function
|
30
|
+
# @return [Cuprum::Result] The result from the most recent call of the
|
31
|
+
# operation.
|
32
|
+
attr_reader :result
|
33
|
+
|
34
|
+
# (see Cuprum::Function#call)
|
35
|
+
def call *args, &block
|
36
|
+
reset! if called? # Clear reference to most recent result.
|
37
|
+
|
38
|
+
@result = super
|
39
|
+
end # method call
|
40
|
+
|
41
|
+
# @return [Boolean] true if the operation has been called and has a
|
42
|
+
# reference to the most recent result; otherwise false.
|
43
|
+
def called?
|
44
|
+
!result.nil?
|
45
|
+
end # method called?
|
46
|
+
|
47
|
+
# @return [Array] the errors from the most recent result, or nil if the
|
48
|
+
# operation has not been called.
|
49
|
+
def errors
|
50
|
+
super || (called? ? result.errors : nil)
|
51
|
+
end # method errors
|
52
|
+
|
53
|
+
# @return [Boolean] true if the most recent result had errors, or false if
|
54
|
+
# the most recent result had no errors or if the operation has not been
|
55
|
+
# called.
|
56
|
+
def failure?
|
57
|
+
called? ? result.failure? : false
|
58
|
+
end # method success?
|
59
|
+
|
60
|
+
# Clears the reference to the most recent call of the operation, if any.
|
61
|
+
# This allows the result and any referenced data to be garbage collected.
|
62
|
+
# Use this method to clear any instance variables or state internal to the
|
63
|
+
# operation (an operation should never have external state apart from the
|
64
|
+
# last result).
|
65
|
+
#
|
66
|
+
# If the operation cannot be run more than once, this method should raise an
|
67
|
+
# error.
|
68
|
+
def reset!
|
69
|
+
@result = nil
|
70
|
+
end # method reset
|
71
|
+
|
72
|
+
# @return [Boolean] true if the most recent result had no errors, or false
|
73
|
+
# if the most recent result had errors or if the operation has not been
|
74
|
+
# called.
|
75
|
+
def success?
|
76
|
+
called? ? result.success? : false
|
77
|
+
end # method success?
|
78
|
+
|
79
|
+
# @return [Object] the value of the most recent result, or nil if the
|
80
|
+
# operation has not been called.
|
81
|
+
def value
|
82
|
+
called? ? result.value : nil
|
83
|
+
end # method value
|
84
|
+
end # class
|
85
|
+
end # module
|
data/lib/cuprum/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cuprum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rob "Merlin" Smith
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: rspec-sleeping_king_studios
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 2.3
|
33
|
+
version: '2.3'
|
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: 2.3
|
40
|
+
version: '2.3'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rubocop
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,14 +58,14 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: 1.15
|
61
|
+
version: '1.15'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: 1.15
|
68
|
+
version: '1.15'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: simplecov
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -94,6 +94,7 @@ files:
|
|
94
94
|
- README.md
|
95
95
|
- lib/cuprum.rb
|
96
96
|
- lib/cuprum/function.rb
|
97
|
+
- lib/cuprum/operation.rb
|
97
98
|
- lib/cuprum/result.rb
|
98
99
|
- lib/cuprum/version.rb
|
99
100
|
homepage: http://sleepingkingstudios.com
|