functo 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fb30677ba2bf297b33d5172f8294e9d41a86600b
4
+ data.tar.gz: 3d93acaadc048c2a5022923ea765c8218fda4647
5
+ SHA512:
6
+ metadata.gz: aec41c3f34032376bca10c93f10aaba7cfdf628ea1516543f12270f68f16ee29ce7f8a21c7fb19551bafa8a29eb142ef3f790964bb6e1cab9b76e2e307cfdac2
7
+ data.tar.gz: cd3cd144a62511eb42a8af4bb739184d2fa090b6a9d3851cd84a15315d0bb1f7c6f4287da5282241fadd946c397dd4ef980c2f87a4357b49e4d75a045fa63f58
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/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in functo.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Samuel Scully
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,153 @@
1
+ # Functo
2
+
3
+ Functo is a dynamic module for composable method objects in ruby.
4
+
5
+ It turns this:
6
+
7
+ ```ruby
8
+ class AddsTwo
9
+ attr_reader :number
10
+ protected :number
11
+
12
+ def initialize(number)
13
+ @number = number
14
+ end
15
+
16
+ def add
17
+ number + 2
18
+ end
19
+ end
20
+ ```
21
+
22
+ in to this:
23
+
24
+ ```ruby
25
+ class AddsTwo
26
+ include Functo.call :add, :number
27
+
28
+ def add
29
+ number + 2
30
+ end
31
+ end
32
+ ```
33
+
34
+ ## Installation
35
+
36
+ Add this line to your application's Gemfile:
37
+
38
+ ```ruby
39
+ gem 'functo'
40
+ ```
41
+
42
+ And then execute:
43
+
44
+ $ bundle
45
+
46
+ Or install it yourself as:
47
+
48
+ $ gem install functo
49
+
50
+ ## Usage
51
+
52
+ Functo objects can take up to three arguments.
53
+
54
+ ```ruby
55
+ class Multiply
56
+ include Functo.call :multiply, :first, :second, :third
57
+
58
+ def multiply
59
+ first * second * third
60
+ end
61
+ end
62
+
63
+ Multiply.call(10, 20, 30)
64
+ # => 6000
65
+
66
+ class Divide
67
+ include Functo.call :multiply, :first, :second, :third, :fourth
68
+
69
+ def divide
70
+ first / second / third / fourth
71
+ end
72
+ end
73
+ # => ArgumentError: given 4 arguments when only 3 are allowed
74
+ ```
75
+
76
+ If you find yourself needing more you should consider composing method objects or encapsulating some of your arguments in another object.
77
+
78
+ You can use square brackets to call Functo objects:
79
+
80
+ ```ruby
81
+ AddsTwo[3]
82
+ # => 5
83
+ ```
84
+
85
+ and they can be used in blocks:
86
+
87
+ ```ruby
88
+ [1, 2, 3].map(&AddsTwo)
89
+ # => [3, 4, 5]
90
+ ```
91
+
92
+ ### Composition
93
+
94
+ Functo objects can be composed using `compose` or the turbo operator `>>`:
95
+
96
+ ```ruby
97
+ AddMulti = AddsTwo.compose(MultipliesThree)
98
+
99
+ AddMulti.call(3)
100
+ # => 15
101
+
102
+ MultiAdd = MultipliesThree >> AddsTwo
103
+
104
+ MultiAdd[3]
105
+ # => 11
106
+ ```
107
+
108
+ The difference between the two is that the turbo operator will splat arrays passed between the composed objects but `compose` will not.
109
+
110
+ ```ruby
111
+ class SplitDigits
112
+ include Functo.call :split, :number
113
+
114
+ def split
115
+ number.to_s.split(//).map(&:to_i)
116
+ end
117
+ end
118
+
119
+ class Sum
120
+ include Functo.call :sum, :first, :second, :third
121
+
122
+ def sum
123
+ first + second + third
124
+ end
125
+ end
126
+
127
+ SumDigits = SplitDigits >> Sum
128
+
129
+ SumDigits[123]
130
+ # => 6
131
+
132
+ SumDigits2 = SplitDigits.compose(Sum)
133
+
134
+ SumDigits2[123]
135
+ # => ArgumentError: wrong number of arguments (given 1, expected 3)
136
+ ```
137
+
138
+ ## Acknowledgements
139
+
140
+ Functo was inspired by these gems:
141
+
142
+ * [concord](https://github.com/mbj/concord) by mbj
143
+ * [procto](https://github.com/snusnu/procto) by snusnu
144
+
145
+ ## Contributing
146
+
147
+ Bug reports and pull requests are welcome on GitHub at https://github.com/sbscully/functo.
148
+
149
+
150
+ ## License
151
+
152
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
153
+
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 "functo"
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/functo.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 'functo/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "functo"
8
+ spec.version = Functo::VERSION
9
+ spec.authors = ["Samuel Scully"]
10
+ spec.email = ["sbscully@gmail.com"]
11
+
12
+ spec.summary = %q{Composable method objects in ruby.}
13
+ spec.homepage = "https://github.com/sbscully/functo"
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.11"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.0"
24
+ end
@@ -0,0 +1,3 @@
1
+ class Functo < Module
2
+ VERSION = "0.1.0"
3
+ end
data/lib/functo.rb ADDED
@@ -0,0 +1,108 @@
1
+ require "functo/version"
2
+
3
+ class Functo < Module
4
+ MAX_ARGUMENTS = 3
5
+
6
+ private_class_method :new
7
+
8
+ def self.call(*names)
9
+ output = names.shift
10
+
11
+ if names.length > MAX_ARGUMENTS
12
+ raise ArgumentError.new("given #{names.length} arguments when only #{MAX_ARGUMENTS} are allowed")
13
+ end
14
+
15
+ new(names, output)
16
+ end
17
+
18
+ private
19
+
20
+ def initialize(inputs, output)
21
+ @inputs = Array(inputs)
22
+ @output = output
23
+ @inputs_module = Module.new
24
+ @output_module = Module.new
25
+
26
+ define_initialize
27
+ define_readers
28
+ define_call
29
+ end
30
+
31
+ def included(host)
32
+ host.include(@inputs_module)
33
+ host.extend(@output_module)
34
+
35
+ host.extend(ClassMethods)
36
+ end
37
+
38
+ def define_initialize
39
+ ivars = @inputs.map { |name| "@#{name}" }
40
+ size = @inputs.size
41
+
42
+ @inputs_module.class_eval do
43
+ define_method :initialize do |*args|
44
+ args_size = args.size
45
+
46
+ if args_size != size
47
+ fail ArgumentError, "wrong number of arguments (#{args_size} for #{size})"
48
+ end
49
+
50
+ ivars.zip(args) { |ivar, arg| instance_variable_set(ivar, arg) }
51
+ end
52
+
53
+ private :initialize
54
+ end
55
+ end
56
+
57
+ def define_readers
58
+ attribute_names = @inputs
59
+
60
+ @inputs_module.class_eval do
61
+ attr_reader(*attribute_names)
62
+ protected(*attribute_names)
63
+ end
64
+ end
65
+
66
+ def define_call
67
+ call_method = @output
68
+
69
+ @output_module.class_eval do
70
+ define_method :call do |*args|
71
+ new(*args).public_send(call_method)
72
+ end
73
+ end
74
+ end
75
+
76
+ module ClassMethods
77
+ def [](*args)
78
+ call(*args)
79
+ end
80
+
81
+ def to_proc
82
+ public_method(:call).to_proc
83
+ end
84
+
85
+ def compose(outer, splat: false)
86
+ inner = self
87
+ klass = Class.new
88
+
89
+ klass.define_singleton_method :call do |*args|
90
+ if splat
91
+ outer.call(*inner.call(*args))
92
+ else
93
+ outer.call(inner.call(*args))
94
+ end
95
+ end
96
+
97
+ klass.extend(ClassMethods)
98
+
99
+ klass
100
+ end
101
+
102
+ def >>(outer)
103
+ compose(outer, splat: true)
104
+ end
105
+ end
106
+ private_constant(:ClassMethods)
107
+
108
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: functo
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Scully
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-16 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: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - sbscully@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - functo.gemspec
72
+ - lib/functo.rb
73
+ - lib/functo/version.rb
74
+ homepage: https://github.com/sbscully/functo
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.5.1
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Composable method objects in ruby.
98
+ test_files: []