curryable 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/CODE_OF_CONDUCT.md +28 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +17 -0
- data/LICENSE.txt +21 -0
- data/README.md +48 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/curryable.gemspec +24 -0
- data/lib/curryable/version.rb +3 -0
- data/lib/curryable.rb +94 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c69f5509d381742d21f5c98772a3b88c063c19a4
|
4
|
+
data.tar.gz: 03a2828d99d416ef2e09ad6371a35dd7307a13ae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 963468f73b842c42c27d6c1746f9c6485c5cf9bb72d6ad6e9a3babc6bf977c93a8e832335ebcc73818ecdef671243581984be54b52d618bfa3cec9d19469bc44
|
7
|
+
data.tar.gz: a24666d8a5f70fe3f207ad6d2e46c292e0f2bb62b31e771a8c25124719e7f83f5389bc96c318cc8b3e70664460333d80ae5e963cf723aa34ed204a20514e4f57
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all
|
4
|
+
people who contribute through reporting issues, posting feature requests,
|
5
|
+
updating documentation, submitting pull requests or patches, and other
|
6
|
+
activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, age, or religion.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include the use of sexual
|
14
|
+
language or imagery, derogatory comments or personal attacks, trolling, public
|
15
|
+
or private harassment, insults, or other unprofessional conduct.
|
16
|
+
|
17
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
18
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
19
|
+
that are not aligned to this Code of Conduct. Project maintainers who do not
|
20
|
+
follow the Code of Conduct may be removed from the project team.
|
21
|
+
|
22
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
23
|
+
reported by opening an issue or contacting one or more of the project
|
24
|
+
maintainers.
|
25
|
+
|
26
|
+
This Code of Conduct is adapted from the [Contributor
|
27
|
+
Covenant](http:contributor-covenant.org), version 1.0.0, available at
|
28
|
+
[http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Stephen Best and Dominic Baggot
|
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,48 @@
|
|
1
|
+
Curryable
|
2
|
+
=========
|
3
|
+
|
4
|
+
## What is a command object?
|
5
|
+
|
6
|
+
An object (noun) that acts as a container for a function (verb).
|
7
|
+
|
8
|
+
## Design goals
|
9
|
+
|
10
|
+
* Objects should be immutable
|
11
|
+
* Object should act like a function
|
12
|
+
* Arguments to the object can be curried
|
13
|
+
* Objects should be inspectable. Inspection should reveal
|
14
|
+
* the class
|
15
|
+
* which arguments have been curried, and their values
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
class SignUpUser
|
19
|
+
def initialize(user_creator:, attributes:, email_api:, crm_api:)
|
20
|
+
@user_creator = user_creator
|
21
|
+
@attributes = attributes
|
22
|
+
@email_api = email_api
|
23
|
+
@crm_api = crm_api
|
24
|
+
end
|
25
|
+
|
26
|
+
attr_reader :user_creator, :attributes, :email_api, :crm_api
|
27
|
+
private :user_creator, :attributes, :email_api, :crm_api
|
28
|
+
|
29
|
+
def call
|
30
|
+
create_user_record
|
31
|
+
send_confirmation_email
|
32
|
+
add_user_to_crm
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
signup_service = Curryable.new(SignUpUser).call(
|
37
|
+
user_creator: User,
|
38
|
+
email_api: EmailAPI.new(EMAIL_AUTH_TOKEN),
|
39
|
+
crm_api: CRMAPI.new(CRM_AUTH_TOKEN),
|
40
|
+
)
|
41
|
+
|
42
|
+
signup_service.inspect
|
43
|
+
# => <# Curryable<SignUpUser> user_creator=User, email_api=<#EmailAPI>, crm_api=<#CRMAPI>, attributes=>
|
44
|
+
|
45
|
+
signup_service.call(
|
46
|
+
attributes: user_attrs,
|
47
|
+
)
|
48
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "curryable"
|
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
data/curryable.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'curryable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "curryable"
|
8
|
+
spec.version = Curryable::VERSION
|
9
|
+
spec.authors = ["Stephen Best", "Dominic Baggot"]
|
10
|
+
spec.email = ["bestie@gmail.com", "dominic.baggot@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Enables immutable command objects to act as currable functions}
|
13
|
+
spec.homepage = "https://github.com/evilstreak/curryable"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = "exe"
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "pry", "0.10.1"
|
24
|
+
end
|
data/lib/curryable.rb
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
class Curryable
|
2
|
+
def initialize(command_class, *arguments)
|
3
|
+
@command_class = command_class
|
4
|
+
@arguments = arguments
|
5
|
+
end
|
6
|
+
|
7
|
+
attr_reader :command_class, :arguments
|
8
|
+
private :command_class, :arguments
|
9
|
+
|
10
|
+
def call(*new_arguments)
|
11
|
+
curryable = self.class.new(
|
12
|
+
command_class,
|
13
|
+
*combined_arguments(new_arguments)
|
14
|
+
)
|
15
|
+
|
16
|
+
if curryable.enough_arguments?
|
17
|
+
curryable.execute
|
18
|
+
else
|
19
|
+
curryable
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
protected
|
24
|
+
|
25
|
+
def enough_arguments?
|
26
|
+
enough_positional_arguments? && enough_keyword_arguments?
|
27
|
+
end
|
28
|
+
|
29
|
+
def execute
|
30
|
+
command_class.new(*arguments).call
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def combined_arguments(new_arguments)
|
36
|
+
combined = arguments + new_arguments
|
37
|
+
|
38
|
+
positional = combined.take(arity)
|
39
|
+
|
40
|
+
possible_keywords = combined.drop(arity)
|
41
|
+
|
42
|
+
unless possible_keywords.all? { |arg| arg.is_a?(Hash) }
|
43
|
+
excess_arg_count = combined.length
|
44
|
+
|
45
|
+
raise ArgumentError.new(
|
46
|
+
"wrong number of arguments (#{excess_arg_count} for #{arity})"
|
47
|
+
)
|
48
|
+
end
|
49
|
+
|
50
|
+
keywords = possible_keywords.reduce(&:merge) || {}
|
51
|
+
|
52
|
+
unknown_keywords = keywords.keys - required_keywords
|
53
|
+
|
54
|
+
if unknown_keywords.any?
|
55
|
+
plural = unknown_keywords.length > 1 ? "s" : ""
|
56
|
+
|
57
|
+
raise ArgumentError.new(
|
58
|
+
"unknown keyword#{plural}: #{unknown_keywords.join(", ")}"
|
59
|
+
)
|
60
|
+
end
|
61
|
+
|
62
|
+
positional + [keywords].reject(&:empty?)
|
63
|
+
end
|
64
|
+
|
65
|
+
def enough_positional_arguments?
|
66
|
+
arguments.length >= arity
|
67
|
+
end
|
68
|
+
|
69
|
+
def enough_keyword_arguments?
|
70
|
+
provided_keywords & required_keywords == required_keywords
|
71
|
+
end
|
72
|
+
|
73
|
+
def provided_keywords
|
74
|
+
provided_keyword_arguments.keys
|
75
|
+
end
|
76
|
+
|
77
|
+
def provided_keyword_arguments
|
78
|
+
arguments.drop(arity).fetch(0, {})
|
79
|
+
end
|
80
|
+
|
81
|
+
def required_keywords
|
82
|
+
parameters
|
83
|
+
.select { |(type, _name)| type == :keyreq }
|
84
|
+
.map { |(_type, name)| name }
|
85
|
+
end
|
86
|
+
|
87
|
+
def arity
|
88
|
+
parameters.count { |(type, _name)| type == :req }
|
89
|
+
end
|
90
|
+
|
91
|
+
def parameters
|
92
|
+
command_class.instance_method(:initialize).parameters
|
93
|
+
end
|
94
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: curryable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stephen Best
|
8
|
+
- Dominic Baggot
|
9
|
+
autorequire:
|
10
|
+
bindir: exe
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-05-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.9'
|
21
|
+
type: :development
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.9'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: rake
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '10.0'
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '10.0'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: pry
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 0.10.1
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - '='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 0.10.1
|
56
|
+
description:
|
57
|
+
email:
|
58
|
+
- bestie@gmail.com
|
59
|
+
- dominic.baggot@gmail.com
|
60
|
+
executables: []
|
61
|
+
extensions: []
|
62
|
+
extra_rdoc_files: []
|
63
|
+
files:
|
64
|
+
- ".gitignore"
|
65
|
+
- ".rspec"
|
66
|
+
- CODE_OF_CONDUCT.md
|
67
|
+
- Gemfile
|
68
|
+
- Gemfile.lock
|
69
|
+
- LICENSE.txt
|
70
|
+
- README.md
|
71
|
+
- Rakefile
|
72
|
+
- bin/console
|
73
|
+
- bin/setup
|
74
|
+
- curryable.gemspec
|
75
|
+
- lib/curryable.rb
|
76
|
+
- lib/curryable/version.rb
|
77
|
+
homepage: https://github.com/evilstreak/curryable
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.4.6
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Enables immutable command objects to act as currable functions
|
101
|
+
test_files: []
|