singl 0.0.1
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 +7 -0
- data/.github/workflows/ruby.yml +38 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +20 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +61 -0
- data/LICENSE +18 -0
- data/README.md +87 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/singl.rb +54 -0
- data/singl.gemspec +31 -0
- metadata +61 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 8a9de8c441e89028318455e1e4849db3cffcd535e80107a144da957d66053355
|
|
4
|
+
data.tar.gz: 497904dd2e2f9be36d60262c4c259b3fcd73990100a386bf8fa40f4bf28bcbdb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: c8051604d5159cadba1e4b5afc27fd1b8edaebc2e833446b1025cc41ba978690d13a7748a02226a060362ff95d28333aa2ad736df2719a1f8ba7fbe43f6c5064
|
|
7
|
+
data.tar.gz: 882f0e643a83c8c8989534d9729c9a9481869c59e3ff8672c8416611cf7a1ed4856a6b3748ac5811ffca63adf98780656d7085438e393854eed83a4fcbcd3ebc
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# This workflow uses actions that are not certified by GitHub.
|
|
2
|
+
# They are provided by a third-party and are governed by
|
|
3
|
+
# separate terms of service, privacy policy, and support
|
|
4
|
+
# documentation.
|
|
5
|
+
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
|
6
|
+
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
|
7
|
+
|
|
8
|
+
name: Ruby
|
|
9
|
+
|
|
10
|
+
on:
|
|
11
|
+
push:
|
|
12
|
+
branches: [ "master" ]
|
|
13
|
+
pull_request:
|
|
14
|
+
branches: [ "master" ]
|
|
15
|
+
|
|
16
|
+
permissions:
|
|
17
|
+
contents: read
|
|
18
|
+
|
|
19
|
+
jobs:
|
|
20
|
+
test:
|
|
21
|
+
|
|
22
|
+
runs-on: ubuntu-latest
|
|
23
|
+
strategy:
|
|
24
|
+
matrix:
|
|
25
|
+
ruby-version: ['2.7', '3.0']
|
|
26
|
+
|
|
27
|
+
steps:
|
|
28
|
+
- uses: actions/checkout@v3
|
|
29
|
+
- name: Set up Ruby
|
|
30
|
+
# To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
|
|
31
|
+
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
|
32
|
+
# uses: ruby/setup-ruby@v1
|
|
33
|
+
uses: ruby/setup-ruby@2b019609e2b0f1ea1a2bc8ca11cb82ab46ada124
|
|
34
|
+
with:
|
|
35
|
+
ruby-version: ${{ matrix.ruby-version }}
|
|
36
|
+
bundler-cache: true # runs 'bundle install' and caches installed gems automatically
|
|
37
|
+
- name: Run tests
|
|
38
|
+
run: bundle exec rake
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
TargetRubyVersion: 2.7.0
|
|
3
|
+
NewCops: enable
|
|
4
|
+
|
|
5
|
+
Style/StringLiterals:
|
|
6
|
+
Enabled: true
|
|
7
|
+
EnforcedStyle: double_quotes
|
|
8
|
+
|
|
9
|
+
Style/StringLiteralsInInterpolation:
|
|
10
|
+
Enabled: true
|
|
11
|
+
EnforcedStyle: double_quotes
|
|
12
|
+
|
|
13
|
+
Layout/LineLength:
|
|
14
|
+
Max: 120
|
|
15
|
+
|
|
16
|
+
Lint/MissingSuper:
|
|
17
|
+
Enabled: false
|
|
18
|
+
|
|
19
|
+
Metrics/MethodLength:
|
|
20
|
+
Max: 20
|
data/Gemfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
source "https://rubygems.org"
|
|
4
|
+
|
|
5
|
+
# Specify your gem's dependencies in it.gemspec
|
|
6
|
+
gemspec
|
|
7
|
+
|
|
8
|
+
group :development do
|
|
9
|
+
gem "pry", "~> 0.14.1"
|
|
10
|
+
gem "rake", "~> 13.0"
|
|
11
|
+
gem "rspec", "~> 3.0"
|
|
12
|
+
gem "rubocop", "~> 0.80"
|
|
13
|
+
end
|
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
singl (0.0.1)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
ast (2.4.2)
|
|
10
|
+
coderay (1.1.3)
|
|
11
|
+
diff-lcs (1.5.0)
|
|
12
|
+
method_source (1.0.0)
|
|
13
|
+
parallel (1.22.1)
|
|
14
|
+
parser (3.1.2.1)
|
|
15
|
+
ast (~> 2.4.1)
|
|
16
|
+
pry (0.14.1)
|
|
17
|
+
coderay (~> 1.1)
|
|
18
|
+
method_source (~> 1.0)
|
|
19
|
+
rainbow (3.1.1)
|
|
20
|
+
rake (13.0.6)
|
|
21
|
+
regexp_parser (2.5.0)
|
|
22
|
+
rexml (3.2.5)
|
|
23
|
+
rspec (3.11.0)
|
|
24
|
+
rspec-core (~> 3.11.0)
|
|
25
|
+
rspec-expectations (~> 3.11.0)
|
|
26
|
+
rspec-mocks (~> 3.11.0)
|
|
27
|
+
rspec-core (3.11.0)
|
|
28
|
+
rspec-support (~> 3.11.0)
|
|
29
|
+
rspec-expectations (3.11.0)
|
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
31
|
+
rspec-support (~> 3.11.0)
|
|
32
|
+
rspec-mocks (3.11.1)
|
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
34
|
+
rspec-support (~> 3.11.0)
|
|
35
|
+
rspec-support (3.11.0)
|
|
36
|
+
rubocop (0.93.1)
|
|
37
|
+
parallel (~> 1.10)
|
|
38
|
+
parser (>= 2.7.1.5)
|
|
39
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
40
|
+
regexp_parser (>= 1.8)
|
|
41
|
+
rexml
|
|
42
|
+
rubocop-ast (>= 0.6.0)
|
|
43
|
+
ruby-progressbar (~> 1.7)
|
|
44
|
+
unicode-display_width (>= 1.4.0, < 2.0)
|
|
45
|
+
rubocop-ast (1.21.0)
|
|
46
|
+
parser (>= 3.1.1.0)
|
|
47
|
+
ruby-progressbar (1.11.0)
|
|
48
|
+
unicode-display_width (1.8.0)
|
|
49
|
+
|
|
50
|
+
PLATFORMS
|
|
51
|
+
x86_64-linux
|
|
52
|
+
|
|
53
|
+
DEPENDENCIES
|
|
54
|
+
pry (~> 0.14.1)
|
|
55
|
+
rake (~> 13.0)
|
|
56
|
+
rspec (~> 3.0)
|
|
57
|
+
rubocop (~> 0.80)
|
|
58
|
+
singl!
|
|
59
|
+
|
|
60
|
+
BUNDLED WITH
|
|
61
|
+
2.2.3
|
data/LICENSE
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Copyright © 2022 Rafael Andrade
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge,
|
|
4
|
+
to any person obtaining a copy of this software and associated documentation files (the “Software”),
|
|
5
|
+
to deal in the Software without restriction,
|
|
6
|
+
including without limitation the rights to use, copy, modify, merge,
|
|
7
|
+
publish, distribute, sublicense, and/or sell copies of the Software,
|
|
8
|
+
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
9
|
+
|
|
10
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
11
|
+
|
|
12
|
+
THE SOFTWARE IS PROVIDED “AS IS”,
|
|
13
|
+
WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
|
14
|
+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
16
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
17
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
18
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
# Singl
|
|
2
|
+
|
|
3
|
+
Have you ever tried to use instance methods on `case` expressions,
|
|
4
|
+
and got bored by all the repetition?
|
|
5
|
+
|
|
6
|
+
```ruby
|
|
7
|
+
tmp_var = my_method.that_gets_an_integer(foo)
|
|
8
|
+
case
|
|
9
|
+
when tmp_var.odd? then foo_bar(baz)
|
|
10
|
+
when tmp_var.zero? then frob(nicate)
|
|
11
|
+
else raise Error
|
|
12
|
+
end
|
|
13
|
+
```
|
|
14
|
+
Say no more to this, using `Singl.ify!`
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
Singl.ify! Integer
|
|
18
|
+
|
|
19
|
+
case my_method.that_gets_an_integer(foo)
|
|
20
|
+
when Integer.odd? then foo_bar(baz)
|
|
21
|
+
when Integer.zero? then frob(nicate)
|
|
22
|
+
else raise Error
|
|
23
|
+
end
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
You can also use it in your own classes:
|
|
27
|
+
|
|
28
|
+
```ruby
|
|
29
|
+
class Foobar
|
|
30
|
+
....
|
|
31
|
+
def f
|
|
32
|
+
self.logic
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
include Singl
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
baz = Foobar.new
|
|
39
|
+
Foobar.f baz
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Or, for bonus points, completely ad-hoc:
|
|
43
|
+
|
|
44
|
+
```ruby
|
|
45
|
+
case my_method.that_gets_an_integer(foo)
|
|
46
|
+
when Singl.odd? then foo_bar(baz)
|
|
47
|
+
when Singl.zero? then frob(nicate)
|
|
48
|
+
else raise Error
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
Singl.f baz
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
For more info, see the [associated blog post][]
|
|
55
|
+
|
|
56
|
+
[associated blog post]: https://web.ist.utl.pt/ist186503/posts/008_Ruby-turning-instance-methods-into-class-methods.html
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
## Installation
|
|
60
|
+
|
|
61
|
+
Add this line to your application's Gemfile:
|
|
62
|
+
|
|
63
|
+
```ruby
|
|
64
|
+
gem 'singl'
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
And then execute:
|
|
68
|
+
|
|
69
|
+
$ bundle install
|
|
70
|
+
|
|
71
|
+
Or install it yourself as:
|
|
72
|
+
|
|
73
|
+
$ gem install singl
|
|
74
|
+
|
|
75
|
+
## Usage
|
|
76
|
+
|
|
77
|
+
(See above)
|
|
78
|
+
|
|
79
|
+
## Development
|
|
80
|
+
|
|
81
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
82
|
+
|
|
83
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
84
|
+
|
|
85
|
+
## Contributing
|
|
86
|
+
|
|
87
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/RafaelPAndrade/singl.
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require "bundler/setup"
|
|
5
|
+
require "singl"
|
|
6
|
+
|
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
+
|
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
11
|
+
# require "pry"
|
|
12
|
+
# Pry.start
|
|
13
|
+
|
|
14
|
+
require "irb"
|
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/singl.rb
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Instance to singleton method converter
|
|
4
|
+
module Singl
|
|
5
|
+
VERSION = "0.0.1"
|
|
6
|
+
class Error < StandardError; end
|
|
7
|
+
|
|
8
|
+
# Converts all the instance methods of the given class into
|
|
9
|
+
# singleton methods that get the intended receiver as the first argument
|
|
10
|
+
# If called with no arguments, returns a lambda that can be called with the intended receiver
|
|
11
|
+
#
|
|
12
|
+
# = Example
|
|
13
|
+
# >> Singl.ify! Integer
|
|
14
|
+
# >> Integer.even? 4
|
|
15
|
+
# => true
|
|
16
|
+
# >> Integer.even?
|
|
17
|
+
# => #<Proc:0x000057f0877ba488 (lambda)>
|
|
18
|
+
# >> Integer.even?.call 4
|
|
19
|
+
# => true
|
|
20
|
+
def self.ify!(klass)
|
|
21
|
+
klass.instance_methods
|
|
22
|
+
.map { |m| klass.instance_method m }
|
|
23
|
+
.filter { |m| m.arity.zero? and m.owner == klass }
|
|
24
|
+
.each do |m|
|
|
25
|
+
klass.define_singleton_method(m.name) do |*args|
|
|
26
|
+
l = ->(selv, *inargs) { m.bind_call(selv, *inargs) }
|
|
27
|
+
|
|
28
|
+
return l if args.empty?
|
|
29
|
+
|
|
30
|
+
l.call(*args)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
klass
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Creates a lambda that sends the method to the first argument given
|
|
37
|
+
# If no arguments are given, then the lambda is returned
|
|
38
|
+
def self.method_missing(name, *args)
|
|
39
|
+
l = ->(selv, *inargs) { selv.send(name, *inargs) }
|
|
40
|
+
|
|
41
|
+
return l if args.empty?
|
|
42
|
+
|
|
43
|
+
l.call(*args)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def self.respond_to_missing?(*)
|
|
47
|
+
true
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Automatically apply Singl.ify! when used as a mixin in a class
|
|
51
|
+
def self.included(mod)
|
|
52
|
+
ify!(mod) if mod.is_a? Class
|
|
53
|
+
end
|
|
54
|
+
end
|
data/singl.gemspec
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/singl"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "singl"
|
|
7
|
+
spec.version = Singl::VERSION
|
|
8
|
+
spec.authors = ["RafaelPAndrade"]
|
|
9
|
+
spec.license = "MIT"
|
|
10
|
+
|
|
11
|
+
spec.summary = "Turning instance methods into singletons"
|
|
12
|
+
spec.description = <<~DESC
|
|
13
|
+
Given a class, it will extract all instance methods taking no arguments into singleton methods receiving self as first argument
|
|
14
|
+
DESC
|
|
15
|
+
spec.homepage = "https://web.ist.utl.pt/projects/#{spec.name}"
|
|
16
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
|
|
17
|
+
|
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
19
|
+
spec.metadata["source_code_uri"] = "https://github.com/RafaelPAndrade/#{spec.name}"
|
|
20
|
+
spec.metadata["changelog_uri"] = "#{spec.metadata["source_code_uri"]}/blob/master/CHANGELOG.md"
|
|
21
|
+
|
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
25
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
|
26
|
+
end
|
|
27
|
+
spec.require_paths = ["lib"]
|
|
28
|
+
|
|
29
|
+
# For more information and examples about making a new gem, checkout our
|
|
30
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
|
31
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: singl
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- RafaelPAndrade
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2022-08-17 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: 'Given a class, it will extract all instance methods taking no arguments
|
|
14
|
+
into singleton methods receiving self as first argument
|
|
15
|
+
|
|
16
|
+
'
|
|
17
|
+
email:
|
|
18
|
+
executables: []
|
|
19
|
+
extensions: []
|
|
20
|
+
extra_rdoc_files: []
|
|
21
|
+
files:
|
|
22
|
+
- ".github/workflows/ruby.yml"
|
|
23
|
+
- ".gitignore"
|
|
24
|
+
- ".rspec"
|
|
25
|
+
- ".rubocop.yml"
|
|
26
|
+
- Gemfile
|
|
27
|
+
- Gemfile.lock
|
|
28
|
+
- LICENSE
|
|
29
|
+
- README.md
|
|
30
|
+
- Rakefile
|
|
31
|
+
- bin/console
|
|
32
|
+
- bin/setup
|
|
33
|
+
- lib/singl.rb
|
|
34
|
+
- singl.gemspec
|
|
35
|
+
homepage: https://web.ist.utl.pt/projects/singl
|
|
36
|
+
licenses:
|
|
37
|
+
- MIT
|
|
38
|
+
metadata:
|
|
39
|
+
homepage_uri: https://web.ist.utl.pt/projects/singl
|
|
40
|
+
source_code_uri: https://github.com/RafaelPAndrade/singl
|
|
41
|
+
changelog_uri: https://github.com/RafaelPAndrade/singl/blob/master/CHANGELOG.md
|
|
42
|
+
post_install_message:
|
|
43
|
+
rdoc_options: []
|
|
44
|
+
require_paths:
|
|
45
|
+
- lib
|
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
- - ">="
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: 2.7.0
|
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
|
+
requirements:
|
|
53
|
+
- - ">="
|
|
54
|
+
- !ruby/object:Gem::Version
|
|
55
|
+
version: '0'
|
|
56
|
+
requirements: []
|
|
57
|
+
rubygems_version: 3.2.3
|
|
58
|
+
signing_key:
|
|
59
|
+
specification_version: 4
|
|
60
|
+
summary: Turning instance methods into singletons
|
|
61
|
+
test_files: []
|