rusty_key 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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/README.md +158 -0
- data/Rakefile +6 -0
- data/UNLICENSE +25 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/example/definitions.rb +31 -0
- data/lib/rusty_key/boolean.rb +120 -0
- data/lib/rusty_key/case.rb +33 -0
- data/lib/rusty_key/definition.rb +97 -0
- data/lib/rusty_key/exception.rb +89 -0
- data/lib/rusty_key/misc.rb +18 -0
- data/lib/rusty_key/version.rb +3 -0
- data/lib/rusty_key.rb +29 -0
- data/rusty_key.gemspec +40 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7167c2c8764597614b0f0e91433bfb4518eb2406df9d2d9dbd69628d12cc6759
|
4
|
+
data.tar.gz: 12d40ff99010b05274847849426772575dc4e6f89e81fa5bb34fdfdd4042b30b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: aef7638538d6e04b4d6aa4dde985c1366492c5a43390d38a02519a0551516b584817a9452b65b4e832600fcf59aba9287e08d9474ca114fe3d15f4a1b6372efa
|
7
|
+
data.tar.gz: '088d5911ee9b0daf8afa2642de25fe152f61f534ec6874b5988f7aa4673c65ac4201c0c529c52fd41e6b67c4c11898eed27d3db6d562a2686a6e5aceaa65f499'
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at mkg@fastmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
# rusty_key
|
2
|
+
|
3
|
+
This gem replaces many keywords with more-or-less equivalent method calls.
|
4
|
+
|
5
|
+
## Why?
|
6
|
+
|
7
|
+
I made a joke about Rust's proposed ["dot keyword" syntax](https://boats.gitlab.io/blog/post/await-decision/), namely that they should apply it to all their keywords.
|
8
|
+
|
9
|
+
Then I took that joke too far.
|
10
|
+
|
11
|
+
I have nothing but respect for the Rust Lang Team's work, and their proposal regarding the `await` keyword makes perfect sense to me. This is just a well-intentioned goof.
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Don't?
|
16
|
+
|
17
|
+
If you really want to, start with this:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
require 'rusty_key'
|
21
|
+
|
22
|
+
RustyKey.using
|
23
|
+
```
|
24
|
+
|
25
|
+
Below are some things you can do, but it's probably not an exhaustive list. Consider using braces rather than `do` and `end` to heighten the effect.
|
26
|
+
|
27
|
+
# Define methods
|
28
|
+
```ruby
|
29
|
+
:foo.def { |x, y, *zs, key: value, &block|
|
30
|
+
# stuff
|
31
|
+
}
|
32
|
+
```
|
33
|
+
|
34
|
+
# Define and monkey patch classes
|
35
|
+
```ruby
|
36
|
+
:Dog.class {
|
37
|
+
:initialize.def { |color|
|
38
|
+
# stuff
|
39
|
+
}
|
40
|
+
|
41
|
+
:bark.def {
|
42
|
+
"wruff!"
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
Dog.new(:green).bark # => "wruff!"
|
47
|
+
```
|
48
|
+
|
49
|
+
# Define and monkey patch modules
|
50
|
+
```ruby
|
51
|
+
:PrettyBow.module {
|
52
|
+
# stuff
|
53
|
+
}
|
54
|
+
```
|
55
|
+
# using, alias, include, extend
|
56
|
+
```ruby
|
57
|
+
:RustyKey.using
|
58
|
+
```
|
59
|
+
```ruby
|
60
|
+
:f.def { |x|
|
61
|
+
# stuff
|
62
|
+
}
|
63
|
+
:func.alias :f
|
64
|
+
```
|
65
|
+
```ruby
|
66
|
+
:String.class {
|
67
|
+
:Cheese.include
|
68
|
+
:Kite.extend
|
69
|
+
}
|
70
|
+
```
|
71
|
+
|
72
|
+
# Flow control
|
73
|
+
```ruby
|
74
|
+
computation = -> { expensive }
|
75
|
+
computation.if(necessary?)
|
76
|
+
.else_if(cheap_check) { one_thing }
|
77
|
+
.elsif(-> { expensive_check }) { another_thing }
|
78
|
+
.else! { go_to_sleep }
|
79
|
+
```
|
80
|
+
|
81
|
+
# Boolean logic
|
82
|
+
```ruby
|
83
|
+
false.or { true } # => truthy
|
84
|
+
false.and { expensive } # => falsey
|
85
|
+
400.and { 20 } # => truthy
|
86
|
+
```
|
87
|
+
|
88
|
+
# Case expressions
|
89
|
+
```ruby
|
90
|
+
x.case
|
91
|
+
.when(String) { "It's a string!" }
|
92
|
+
.when(0..10) { "Pretty small..." }
|
93
|
+
.when(-> y { y.respond_to? :phone }) { "Hello? }
|
94
|
+
.else { go_to_sleep }
|
95
|
+
```
|
96
|
+
|
97
|
+
# Raise and handle exceptions
|
98
|
+
```ruby
|
99
|
+
# raises RuntimeError, like `raise "msg"`
|
100
|
+
"something's wrong!".raise
|
101
|
+
```
|
102
|
+
```ruby
|
103
|
+
-> {
|
104
|
+
ArgumentError.new("no arguing!").raise
|
105
|
+
}.rescue(OtherError, DifferentError) { |e|
|
106
|
+
# not called
|
107
|
+
}.rescue(ArgumentError) { |e|
|
108
|
+
puts "Please don't argue!"
|
109
|
+
}.rescue(Exception) { |e|
|
110
|
+
# already caught, so this isn't called
|
111
|
+
}.rescue { |e|
|
112
|
+
# would catch any uncaught StandardError
|
113
|
+
}.ensure! {
|
114
|
+
puts "Thank you for following the rules."
|
115
|
+
}
|
116
|
+
```
|
117
|
+
|
118
|
+
# return and yield
|
119
|
+
```ruby
|
120
|
+
:f.def { |x|
|
121
|
+
-> { x / 3 }.return_if(x > 10) # guard clause
|
122
|
+
(x * 3).return # explicit return
|
123
|
+
}
|
124
|
+
```
|
125
|
+
```ruby
|
126
|
+
:takes_block.def { |&block|
|
127
|
+
25.yield # yields 25 to block
|
128
|
+
}
|
129
|
+
```
|
130
|
+
|
131
|
+
## Caveats
|
132
|
+
|
133
|
+
* Nested classes (and possibly other nested things) don't work.
|
134
|
+
* There's no replacement for `refine`.
|
135
|
+
* For `#elsif`/`#else_if`, wrap conditions in procs or they will always get evaluated.
|
136
|
+
* Specifically the class `Module` can't be monkey patched this way, or at least it couldn't at some point. Haven't tested that in a while.
|
137
|
+
* Speaking of which: There are no automated tests. I didn't think this would continue to the point where I might need them; I was wrong.
|
138
|
+
* Use `#return_if` and `#return_unless` for conditional returns (e.g. guard clauses) unless you're very comfortable with the difference in semantics between regular procs and lambdas.
|
139
|
+
|
140
|
+
## Acknowledgements
|
141
|
+
|
142
|
+
I'd like to thank:
|
143
|
+
|
144
|
+
* John Mair, whose [binding_of_caller gem](https://github.com/banister/binding_of_caller), released under the MIT license, makes most of this possible.
|
145
|
+
* The Rust Lang Team, who do great work and inspire terrible jokes.
|
146
|
+
* Matz, who will hopefully not ban me from using Ruby for releasing this.
|
147
|
+
|
148
|
+
## Contributing
|
149
|
+
|
150
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/smoochbot/rusty. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
151
|
+
|
152
|
+
## Unlicense
|
153
|
+
|
154
|
+
This is free and unencumbered software released into the [public domain](http://unlicense.org).
|
155
|
+
|
156
|
+
## Code of Conduct
|
157
|
+
|
158
|
+
Everyone interacting in the Rusty project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/smoochbot/rusty/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/UNLICENSE
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
2
|
+
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
4
|
+
distribute this software, either in source code form or as a compiled
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
6
|
+
means.
|
7
|
+
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
9
|
+
of this software dedicate any and all copyright interest in the
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
11
|
+
of the public at large and to the detriment of our heirs and
|
12
|
+
successors. We intend this dedication to be an overt act of
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
14
|
+
software under copyright law.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
For more information, please refer to <http://unlicense.org/>
|
25
|
+
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rusty_key"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# A demonstration of how to define classes, methods and aliases using rusty.
|
4
|
+
|
5
|
+
require_relative '../lib/rusty_key.rb'
|
6
|
+
|
7
|
+
RustyKey.using
|
8
|
+
|
9
|
+
:StringBuilder.class {
|
10
|
+
:initialize.def { |*strs|
|
11
|
+
@string = strs.join.dup
|
12
|
+
}
|
13
|
+
|
14
|
+
:<<.def { |*strs|
|
15
|
+
@string << strs.join
|
16
|
+
self
|
17
|
+
}
|
18
|
+
:append.alias :<<
|
19
|
+
|
20
|
+
:to_s.def {
|
21
|
+
@string.dup.freeze
|
22
|
+
}
|
23
|
+
}
|
24
|
+
|
25
|
+
sb = StringBuilder.new("This ", "is ")
|
26
|
+
sb << "how "
|
27
|
+
sb.append("rusty_", "key ") << "works."
|
28
|
+
|
29
|
+
puts sb.to_s
|
30
|
+
#=> This is how rusty_key works.
|
31
|
+
|
@@ -0,0 +1,120 @@
|
|
1
|
+
module RustyKey
|
2
|
+
refine Object do
|
3
|
+
def and
|
4
|
+
raise ArgumentError("Object#and requires a block") unless block_given?
|
5
|
+
self && yield
|
6
|
+
end
|
7
|
+
|
8
|
+
def or
|
9
|
+
raise ArgumentError("Object#or requires a block") unless block_given?
|
10
|
+
self || yield
|
11
|
+
end
|
12
|
+
|
13
|
+
def if(condition)
|
14
|
+
-> { self }.if(condition)
|
15
|
+
end
|
16
|
+
|
17
|
+
def unless(condition)
|
18
|
+
-> { self }.unless(condition)
|
19
|
+
end
|
20
|
+
|
21
|
+
def if!(condition)
|
22
|
+
self.if(condition).call
|
23
|
+
end
|
24
|
+
|
25
|
+
def unless!(condition)
|
26
|
+
self.unless(condition).call
|
27
|
+
end
|
28
|
+
|
29
|
+
def return_if(condition)
|
30
|
+
if condition
|
31
|
+
result = self.if!(condition)
|
32
|
+
id = result.object_id
|
33
|
+
binding.of_caller(1).eval("return ObjectSpace._id2ref(#{id})")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def return_unless(condition)
|
38
|
+
unless condition
|
39
|
+
result = self.unless!(condition)
|
40
|
+
id = result.object_id
|
41
|
+
binding.of_caller(1).eval("return ObjectSpace._id2ref(#{id})")
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
refine Proc do
|
47
|
+
def if(condition)
|
48
|
+
Conditional.if(self, condition)
|
49
|
+
end
|
50
|
+
|
51
|
+
def unless(condition)
|
52
|
+
Conditional.unless(self, condition)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
class Conditional
|
59
|
+
private_class_method :new
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def check(condition)
|
64
|
+
if condition.respond_to?(:call)
|
65
|
+
!!condition.call
|
66
|
+
else
|
67
|
+
!!condition
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
attr_accessor :found, :result, :otherwise
|
72
|
+
|
73
|
+
def initialize(action, negate, condition)
|
74
|
+
self.found = negate ? !check(condition) : check(condition)
|
75
|
+
self.result = found ? action : -> {}
|
76
|
+
end
|
77
|
+
|
78
|
+
public
|
79
|
+
|
80
|
+
class << self
|
81
|
+
def if(action, condition)
|
82
|
+
new(action, false, condition)
|
83
|
+
end
|
84
|
+
def unless(action, condition)
|
85
|
+
new(action, true, condition)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def elsif(condition, &action)
|
90
|
+
if !found && check(condition)
|
91
|
+
self.found = true
|
92
|
+
self.result = action
|
93
|
+
end
|
94
|
+
self
|
95
|
+
end
|
96
|
+
alias else_if elsif
|
97
|
+
|
98
|
+
def call
|
99
|
+
if found
|
100
|
+
result&.call
|
101
|
+
else
|
102
|
+
otherwise&.call
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def else(&action)
|
107
|
+
self.otherwise = action
|
108
|
+
self
|
109
|
+
end
|
110
|
+
|
111
|
+
def else!(&action)
|
112
|
+
self.else(&action)
|
113
|
+
self.call
|
114
|
+
end
|
115
|
+
|
116
|
+
def to_proc
|
117
|
+
-> { self.call }
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module RustyKey
|
2
|
+
refine Object do
|
3
|
+
def case
|
4
|
+
Case.new(self)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
class Case
|
11
|
+
def initialize(value)
|
12
|
+
@value = value
|
13
|
+
@found = false
|
14
|
+
@result = -> {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def when(condition, &block)
|
18
|
+
if !@found && condition === @value
|
19
|
+
@found = true
|
20
|
+
@result = block
|
21
|
+
end
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
def else(&block)
|
26
|
+
if @found
|
27
|
+
@result&.call(@value)
|
28
|
+
else
|
29
|
+
block&.call(@value)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module RustyKey
|
2
|
+
refine Symbol do
|
3
|
+
def def(&b)
|
4
|
+
binding.of_caller(1).eval('self').send(:define_method, self, b || -> {})
|
5
|
+
self
|
6
|
+
end
|
7
|
+
|
8
|
+
def alias(original)
|
9
|
+
binding.of_caller(1).eval("alias #{self} #{original}")
|
10
|
+
end
|
11
|
+
|
12
|
+
def include
|
13
|
+
binding.of_caller(1).eval("include #{self}")
|
14
|
+
end
|
15
|
+
|
16
|
+
def extend
|
17
|
+
binding.of_caller(1).eval("extend #{self}")
|
18
|
+
end
|
19
|
+
|
20
|
+
alias _class class
|
21
|
+
def class(&b)
|
22
|
+
return _class unless b
|
23
|
+
# get context in which we're trying to define our class
|
24
|
+
context = binding.of_caller(1).eval('self.class')
|
25
|
+
# does a class with this name already exist?
|
26
|
+
if context.const_defined?(self) &&
|
27
|
+
context.const_get(self).is_a?(Class)
|
28
|
+
# a class with this name does exist! we don't want to just replace it,
|
29
|
+
# so let's get to monkey patching.
|
30
|
+
context.const_get(self).class_eval(&b)
|
31
|
+
else
|
32
|
+
# there's no class with this name. there might be another constant,
|
33
|
+
# but who cares? TODO: care
|
34
|
+
context.const_set(self, Class.new(&b))
|
35
|
+
end
|
36
|
+
nil
|
37
|
+
end
|
38
|
+
alias Class class
|
39
|
+
alias clazz Class
|
40
|
+
alias klass Class
|
41
|
+
|
42
|
+
def Module(&b)
|
43
|
+
# get context in which we're trying to define our module
|
44
|
+
context = binding.of_caller(1).eval('self.class')
|
45
|
+
# does a module (not a class!) with this name already exist?
|
46
|
+
if context.const_defined?(self) &&
|
47
|
+
!context.const_get(self).is_a?(Class) &&
|
48
|
+
context.const_get(self).is_a?(Module)
|
49
|
+
# a module with this name does exist! we don't want to just replace it,
|
50
|
+
# so let's get to monkey patching.
|
51
|
+
context.const_get(self).module_eval(&b)
|
52
|
+
else
|
53
|
+
# there's no module with this name. there might be another constant,
|
54
|
+
# but who cares? TODO: care
|
55
|
+
context.const_set(self, Module.new(&b))
|
56
|
+
end
|
57
|
+
nil
|
58
|
+
end
|
59
|
+
alias module Module
|
60
|
+
end
|
61
|
+
|
62
|
+
refine Class do
|
63
|
+
alias _class class
|
64
|
+
def class(&b)
|
65
|
+
b ? self.class_eval(&b) : _class
|
66
|
+
end
|
67
|
+
alias Class class
|
68
|
+
alias clazz Class
|
69
|
+
alias klass Class
|
70
|
+
end
|
71
|
+
|
72
|
+
refine Module do
|
73
|
+
def Module(&b)
|
74
|
+
self.module_eval(&b)
|
75
|
+
end
|
76
|
+
alias module Module
|
77
|
+
|
78
|
+
alias _include include
|
79
|
+
alias _extend extend
|
80
|
+
|
81
|
+
def include(*args)
|
82
|
+
if args.empty?
|
83
|
+
binding.of_caller(1).eval("include #{self}")
|
84
|
+
else
|
85
|
+
_include(*args)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def extend(*args)
|
90
|
+
if args.empty?
|
91
|
+
binding.of_caller(1).eval("extend #{self}")
|
92
|
+
else
|
93
|
+
_extend(*args)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'binding_of_caller'
|
2
|
+
|
3
|
+
module RustyKey
|
4
|
+
refine Exception do
|
5
|
+
def raise
|
6
|
+
id = self.object_id
|
7
|
+
binding.of_caller(1).eval("raise ObjectSpace._id2ref(#{id})")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
refine String do
|
12
|
+
def raise
|
13
|
+
binding.of_caller(1).eval("raise \"#{self}\"")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
refine Proc do
|
18
|
+
def rescue(*types, &handler)
|
19
|
+
RescuedProc.new(self, *types, &handler)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
class RescuedProc
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
attr_accessor :prc, :handlers, :otherwise, :finally, :result
|
30
|
+
|
31
|
+
def merge_handlers(handler, *types, initial: handlers)
|
32
|
+
# rescue clause with unspecified type handles StandardError
|
33
|
+
types << StandardError if types.empty?
|
34
|
+
types.reduce(initial) do |acc, k|
|
35
|
+
# rescue clauses should be prioritized by order of declaration, desc
|
36
|
+
acc.merge(k => handler) { |k, existing| existing }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
public
|
41
|
+
|
42
|
+
def initialize(wrapped, *types, &handler)
|
43
|
+
self.handlers = merge_handlers(handler, *types, initial: {})
|
44
|
+
self.prc = wrapped
|
45
|
+
self.result = nil
|
46
|
+
end
|
47
|
+
|
48
|
+
def rescue(*types, &handler)
|
49
|
+
self.handlers = merge_handlers(handler, *types)
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
def else(&handler)
|
54
|
+
self.otherwise = handler
|
55
|
+
self
|
56
|
+
end
|
57
|
+
|
58
|
+
def call
|
59
|
+
begin
|
60
|
+
self.result = prc.call
|
61
|
+
rescue Exception => e
|
62
|
+
if match = handlers.find { |type, handler| e.is_a? type }&.last
|
63
|
+
match&.call(e)
|
64
|
+
else
|
65
|
+
raise e
|
66
|
+
end
|
67
|
+
else
|
68
|
+
otherwise&.call
|
69
|
+
ensure
|
70
|
+
finally&.call
|
71
|
+
end
|
72
|
+
result
|
73
|
+
end
|
74
|
+
|
75
|
+
def to_proc
|
76
|
+
-> { self.call }
|
77
|
+
end
|
78
|
+
|
79
|
+
def ensure(&handler)
|
80
|
+
self.finally = handler
|
81
|
+
self
|
82
|
+
end
|
83
|
+
|
84
|
+
def ensure!(&handler)
|
85
|
+
self.ensure(&handler)
|
86
|
+
self.call
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module RustyKey
|
2
|
+
refine Object do
|
3
|
+
def return
|
4
|
+
id = self.object_id
|
5
|
+
binding.of_caller(1).eval("return ObjectSpace._id2ref(#{id})")
|
6
|
+
end
|
7
|
+
|
8
|
+
def yield
|
9
|
+
b = binding.of_caller(1)
|
10
|
+
# this is kinda hacky, but it is fun
|
11
|
+
if b.local_variables.include? :block
|
12
|
+
b.eval("block.call(#{self})")
|
13
|
+
else
|
14
|
+
b.eval("yield #{self}")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/lib/rusty_key.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'binding_of_caller'
|
2
|
+
require 'rusty_key/version'
|
3
|
+
require 'rusty_key/definition'
|
4
|
+
require 'rusty_key/case'
|
5
|
+
require 'rusty_key/boolean'
|
6
|
+
require 'rusty_key/exception'
|
7
|
+
require 'rusty_key/misc'
|
8
|
+
|
9
|
+
module RustyKey
|
10
|
+
end
|
11
|
+
|
12
|
+
## monkey-patching these to allow `RustyKey.using`
|
13
|
+
|
14
|
+
class Symbol
|
15
|
+
def using
|
16
|
+
b = binding.of_caller(1)
|
17
|
+
b.eval("using #{self}")
|
18
|
+
b
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Module
|
23
|
+
def using
|
24
|
+
b = binding.of_caller(1)
|
25
|
+
b.eval("using #{self}")
|
26
|
+
b
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
data/rusty_key.gemspec
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "rusty_key/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "rusty_key"
|
8
|
+
spec.version = RustyKey::VERSION
|
9
|
+
spec.authors = ["Margaret K. Geiger"]
|
10
|
+
spec.email = ["mkg@fastmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Replaces keywords with method calls.}
|
13
|
+
spec.homepage = "https://github.com/smoochbot/rusty_key"
|
14
|
+
spec.license = "Public Domain"
|
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["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
# Specify which files should be added to the gem when it is released.
|
27
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
28
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
29
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
30
|
+
end
|
31
|
+
spec.bindir = "exe"
|
32
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
spec.add_runtime_dependency "binding_of_caller"
|
36
|
+
|
37
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
38
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
39
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rusty_key
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Margaret K. Geiger
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-05-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: binding_of_caller
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- mkg@fastmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- CODE_OF_CONDUCT.md
|
80
|
+
- Gemfile
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- UNLICENSE
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- example/definitions.rb
|
87
|
+
- lib/rusty_key.rb
|
88
|
+
- lib/rusty_key/boolean.rb
|
89
|
+
- lib/rusty_key/case.rb
|
90
|
+
- lib/rusty_key/definition.rb
|
91
|
+
- lib/rusty_key/exception.rb
|
92
|
+
- lib/rusty_key/misc.rb
|
93
|
+
- lib/rusty_key/version.rb
|
94
|
+
- rusty_key.gemspec
|
95
|
+
homepage: https://github.com/smoochbot/rusty_key
|
96
|
+
licenses:
|
97
|
+
- Public Domain
|
98
|
+
metadata:
|
99
|
+
homepage_uri: https://github.com/smoochbot/rusty_key
|
100
|
+
source_code_uri: https://github.com/smoochbot/rusty_key
|
101
|
+
post_install_message:
|
102
|
+
rdoc_options: []
|
103
|
+
require_paths:
|
104
|
+
- lib
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '0'
|
115
|
+
requirements: []
|
116
|
+
rubygems_version: 3.0.1
|
117
|
+
signing_key:
|
118
|
+
specification_version: 4
|
119
|
+
summary: Replaces keywords with method calls.
|
120
|
+
test_files: []
|