invokable 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
+ SHA256:
3
+ metadata.gz: c67d55899040ae1d223c7a5e2ce5958bc9a14a4bdc8becde524d17de97235b1e
4
+ data.tar.gz: eac90104b5cd4ec726865e85b9d23230a66955fddf7b3aa1eb08e3c7637af0d0
5
+ SHA512:
6
+ metadata.gz: 8753b617d6dc4ba42dc02b15b039c81e800e927f4de019288e5959c58540cc363c9d629d5ecb771eb65444165ffe2fa21bf42b68dc501f0cafe04d22f56de9e7
7
+ data.tar.gz: 3f3aa13ba31160362a04dd9cb28ef32299ab58b70376f154f700f3d313c6cf92ce5d104b504aa65466254daabf3136a66d79d9a68602fdfcd1e99be06cda10ae
@@ -0,0 +1,20 @@
1
+ name: Ruby
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ build:
7
+
8
+ runs-on: ubuntu-latest
9
+
10
+ steps:
11
+ - uses: actions/checkout@v2
12
+ - name: Set up Ruby 2.6
13
+ uses: actions/setup-ruby@v1
14
+ with:
15
+ ruby-version: 2.6.x
16
+ - name: Build and test with Rake
17
+ run: |
18
+ gem install bundler
19
+ bundle install --jobs 4 --retry 3
20
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ tags
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in data-functions.gemspec
6
+ gemspec
7
+
8
+ group :development do
9
+ gem 'pry'
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,41 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ invokable (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ coderay (1.1.2)
10
+ diff-lcs (1.3)
11
+ method_source (0.9.2)
12
+ pry (0.12.2)
13
+ coderay (~> 1.1.0)
14
+ method_source (~> 0.9.0)
15
+ rake (10.5.0)
16
+ rspec (3.9.0)
17
+ rspec-core (~> 3.9.0)
18
+ rspec-expectations (~> 3.9.0)
19
+ rspec-mocks (~> 3.9.0)
20
+ rspec-core (3.9.1)
21
+ rspec-support (~> 3.9.1)
22
+ rspec-expectations (3.9.0)
23
+ diff-lcs (>= 1.2.0, < 2.0)
24
+ rspec-support (~> 3.9.0)
25
+ rspec-mocks (3.9.1)
26
+ diff-lcs (>= 1.2.0, < 2.0)
27
+ rspec-support (~> 3.9.0)
28
+ rspec-support (3.9.2)
29
+
30
+ PLATFORMS
31
+ ruby
32
+
33
+ DEPENDENCIES
34
+ bundler (~> 1.17)
35
+ invokable!
36
+ pry
37
+ rake (~> 10.0)
38
+ rspec (~> 3.0)
39
+
40
+ BUNDLED WITH
41
+ 1.17.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Delon Newman
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,89 @@
1
+ ![Ruby](https://github.com/delonnewman/invokable/workflows/Ruby/badge.svg)
2
+
3
+ # Invokable
4
+
5
+ Treat any Object, Hashes, Arrays, and Sets as Procs (like Enumerable but for Procs)
6
+
7
+ # Synopsis
8
+
9
+ ```ruby
10
+ require 'invokable/hash'
11
+
12
+ number_names = { 1 => "One", 2 => "Two", 3 => "Three" }
13
+ [1, 2, 3, 4].map(&number_names) # => ["One", "Two", "Three", nil]
14
+ ```
15
+
16
+ ```ruby
17
+ require 'invokable/array'
18
+
19
+ alpha = ('a'..'z').to_a
20
+ [1, 2, 3, 4].map(&alpha) # => ["b", "c", "d", "e"]
21
+ ```
22
+
23
+ ```ruby
24
+ require 'invokable/set'
25
+
26
+ favorite_numbers = Set[3, Math::PI]
27
+ [1, 2, 3, 4].select(&favorite_numbers) # => [3]
28
+ ```
29
+
30
+ ```ruby
31
+ require 'invokable'
32
+
33
+ # service objects
34
+ class GetDataFromSomeService
35
+ include Invokable
36
+
37
+ def call(user)
38
+ # do the dirt
39
+ end
40
+ end
41
+
42
+ data_for_user = GetDataFromSomeService.new
43
+ User.all.map(&data_for_user)
44
+ ```
45
+
46
+ Use as much or a little as you need:
47
+
48
+ ```ruby
49
+ require 'invokable' # loads Invokable module
50
+ require 'invokable/hash' # loads hash patch
51
+ require 'invokable/array' # loads array patch
52
+ require 'invokable/set' # loads set patch
53
+ require 'invokable/data' # loads all patches
54
+ ```
55
+
56
+ # Why?
57
+
58
+ A function is a mapping of one value to another with the additional constraint that for the one input value you will
59
+ always get the same output value. So, conceptually, Ruby Hashes, Arrays, and Sets are all functions. Also, there are
60
+ many one method objects out thre (e.g. ServiceObjects) that are essentially functions. Why not treat them as such?
61
+
62
+ # Installation
63
+
64
+ Add this line to your application's Gemfile:
65
+
66
+ ```ruby
67
+ gem 'invokable'
68
+ ```
69
+
70
+ And then execute:
71
+
72
+ > bundle
73
+
74
+ Or install it yourself as:
75
+
76
+ > gem install invokable
77
+
78
+ # See Also
79
+
80
+ - [Clojure](https://clojure.org)
81
+ - [Arc](http://www.arclanguage.org)
82
+
83
+ # TODO
84
+
85
+ - add support for `curry`, `memoize` and maybe transducers.
86
+
87
+ # License
88
+
89
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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/invokable.gemspec ADDED
@@ -0,0 +1,42 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "invokable/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "invokable"
7
+ spec.version = Invokable::VERSION
8
+ spec.authors = ["Delon Newman"]
9
+ spec.email = ["contact@delonnewman.name"]
10
+
11
+ spec.summary = %q{Treat any Object, Hashes, Arrays and Sets as Procs (like Enumerable but for Proc-like objects)}
12
+ spec.description = spec.summary
13
+ spec.homepage = "https://github.com/delonnewman/invokable"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = spec.homepage
23
+ spec.metadata["changelog_uri"] = "#{spec.homepage}#changelog"
24
+ spec.metadata["documentation_uri"] = "https://www.rubydoc.info/gems/invokable"
25
+ else
26
+ raise "RubyGems 2.0 or newer is required to protect against " \
27
+ "public gem pushes."
28
+ end
29
+
30
+ # Specify which files should be added to the gem when it is released.
31
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
32
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
33
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
34
+ end
35
+ spec.bindir = "bin"
36
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
+ spec.require_paths = ["lib"]
38
+
39
+ spec.add_development_dependency "bundler", "~> 1.17"
40
+ spec.add_development_dependency "rake", "~> 10.0"
41
+ spec.add_development_dependency "rspec", "~> 3.0"
42
+ end
@@ -0,0 +1,6 @@
1
+ require_relative '../invokable'
2
+
3
+ class Array
4
+ include Invokable
5
+ alias call at
6
+ end
@@ -0,0 +1,4 @@
1
+ require_relative '../invokable'
2
+ require_relative 'hash'
3
+ require_relative 'set'
4
+ require_relative 'array'
@@ -0,0 +1,6 @@
1
+ require_relative '../invokable'
2
+
3
+ class Hash
4
+ include Invokable
5
+ alias call []
6
+ end
@@ -0,0 +1,7 @@
1
+ require 'set'
2
+ require_relative '../invokable'
3
+
4
+ class Set
5
+ include Invokable
6
+ alias call include?
7
+ end
@@ -0,0 +1,3 @@
1
+ module Invokable
2
+ VERSION = "0.1.0"
3
+ end
data/lib/invokable.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'invokable/version'
2
+
3
+ # TODO: Add curry, memoize, transducers?
4
+ module Invokable
5
+ # If object responds to `call` convert into a Proc that takes a key and returns the value of the Hash for that key.
6
+ #
7
+ # @return [Proc]
8
+ def to_proc
9
+ if respond_to?(:call)
10
+ # TODO: Would method(:call) be more performant? We need benchmarks.
11
+ Proc.new do |*args|
12
+ call(*args)
13
+ end
14
+ else
15
+ raise "Don't know how to convert #{self.inspect} into a Proc"
16
+ end
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: invokable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Delon Newman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-05 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.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
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: Treat any Object, Hashes, Arrays and Sets as Procs (like Enumerable but
56
+ for Proc-like objects)
57
+ email:
58
+ - contact@delonnewman.name
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".github/workflows/ruby.yml"
64
+ - ".gitignore"
65
+ - ".rspec"
66
+ - Gemfile
67
+ - Gemfile.lock
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - invokable.gemspec
72
+ - lib/invokable.rb
73
+ - lib/invokable/array.rb
74
+ - lib/invokable/data.rb
75
+ - lib/invokable/hash.rb
76
+ - lib/invokable/set.rb
77
+ - lib/invokable/version.rb
78
+ homepage: https://github.com/delonnewman/invokable
79
+ licenses:
80
+ - MIT
81
+ metadata:
82
+ allowed_push_host: https://rubygems.org
83
+ homepage_uri: https://github.com/delonnewman/invokable
84
+ source_code_uri: https://github.com/delonnewman/invokable
85
+ changelog_uri: https://github.com/delonnewman/invokable#changelog
86
+ documentation_uri: https://www.rubydoc.info/gems/invokable
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.0.6
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Treat any Object, Hashes, Arrays and Sets as Procs (like Enumerable but for
106
+ Proc-like objects)
107
+ test_files: []