multi_send 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 +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +76 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/multi_send.rb +53 -0
- data/lib/multi_send/object.rb +13 -0
- data/lib/multi_send/version.rb +3 -0
- data/multi_send.gemspec +25 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 735e815b05601a8bb8733b29f2fe232fd796aa99
|
4
|
+
data.tar.gz: 865c4e2fd5cf4a4b78de6f9805e2e4b956a06136
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 42b2d45da162c408abcd64999eb90ef3fcfe912ef9b6dd40bc85e9b9edd5155bde307552d88a75f8b605a38dc9ad2367e8461e3a18998be159285070934286b8
|
7
|
+
data.tar.gz: 22c1efde2bd627c038b05a113456e2ec8bdf27becd8859196cb0b83cff1c1a4ca12e7309b3ced010490ab621e621e949ea4e8a6aaef3128c7e75f860ecf8656f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Matt Parsons
|
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,76 @@
|
|
1
|
+
# MultiSend
|
2
|
+
|
3
|
+
MultiSend makes it easier to do some advanced metaprogramming with Ruby.
|
4
|
+
|
5
|
+
Ruby takes after Smalltalk's message sending paradigm.
|
6
|
+
This is a powerful abstraction, and allows for some pretty awesome metaprogramming.
|
7
|
+
By default, `Object#send` only allows you to send one message...
|
8
|
+
MultiSend provides a way to extend that, making it even more powerful.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'multi_send'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install multi_send
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
### `MultiSend.array(object, *messages)`
|
29
|
+
|
30
|
+
This works much like `Object#send`.
|
31
|
+
MultiSend will send each item in order to the object, so the following lines are equivalent:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
(5 + 5).even?.to_s
|
35
|
+
5.send("+", 5).send(:even?).send(:to_s)
|
36
|
+
MultiSend.array(5, ["+", 5], :even?, :to_s)
|
37
|
+
# with refinements/monkey patches:
|
38
|
+
5.send_array(["+", 5], :even?, :to_s)
|
39
|
+
```
|
40
|
+
|
41
|
+
### `MultiSend.hash(object, **messages)`
|
42
|
+
|
43
|
+
This sends the `**messages` hash to object. Each key is a message, and the key's value is the argument.
|
44
|
+
If the key's value is an array, then it works with all of them.
|
45
|
+
So the following is equivalent:
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
"hello".split("").include?("h")
|
49
|
+
"hello".send(:split, "").send(:include?, "h")
|
50
|
+
MultiSend.hash("hello", split: "", include?: "h")
|
51
|
+
# with refinements,
|
52
|
+
"hello".send_hash(split: "", include?: "h")
|
53
|
+
```
|
54
|
+
|
55
|
+
### `MultiSend.do(object, [*messages|**messages])`
|
56
|
+
|
57
|
+
This one determines whether or not the `messages` parameter is a hash or an array, and delegates to the correct method above.
|
58
|
+
|
59
|
+
### Refinements/Monkey Patches
|
60
|
+
|
61
|
+
You can enable the refinements via `using MultiSend`.
|
62
|
+
If you want them available application-wide, then `require 'multi_send/object'` to enable the monkey patches.
|
63
|
+
|
64
|
+
## Development
|
65
|
+
|
66
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
67
|
+
|
68
|
+
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
1. Fork it ( https://github.com/get-vitamin-c/multi_send/fork )
|
73
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
74
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
75
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
76
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "multi_send"
|
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/lib/multi_send.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require "multi_send/version"
|
2
|
+
|
3
|
+
module MultiSend
|
4
|
+
# automagically figure out whether to send as a hash or array.
|
5
|
+
# >> 5.multi_send( :days, :ago, , :to_date, [:eql?, 5.days.ago.to_date] )
|
6
|
+
# => true
|
7
|
+
def self.do(object, *messages)
|
8
|
+
if messages.length == 1 && messages[0].is_a?(Hash)
|
9
|
+
MultiSend.hash(object, messages[0])
|
10
|
+
else
|
11
|
+
MultiSend.array(object, *messages)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Sends an array of messages to self. Used like:
|
16
|
+
# 5.send_array(:days, :ago)
|
17
|
+
# => (datetime object)
|
18
|
+
# Can specify arguments by using a nested array:
|
19
|
+
# 5.send_array( [ :+, 8 ], [ :*, 9] )
|
20
|
+
# => 77
|
21
|
+
def self.array(object, *messages)
|
22
|
+
messages.reduce(object) do |obj, message|
|
23
|
+
obj.__send__(*message)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Sends a hash of messages to self. The keys in the hash are the message to
|
28
|
+
# send to self and the values are the arguments. Used like:
|
29
|
+
# 5.send_hash( :+ => 5, eql?: 10 )
|
30
|
+
# => true
|
31
|
+
# Multiple arguments can be sent as an array:
|
32
|
+
# 5.send_hash( :+ => 5, send: [:eql?, 10] )
|
33
|
+
def self.hash(object, messages = {})
|
34
|
+
MultiSend.array(object, *messages.map { |message, args| [message, *args] })
|
35
|
+
end
|
36
|
+
|
37
|
+
# If you anticipate wanting to use MultiSend a lot, you can invoke the new
|
38
|
+
# `using` method in a class, module, or namespace. Then, instead of doing a
|
39
|
+
# `MultiSend.do(obj, messages)` you can just do `obj.multi_send(messages)`.
|
40
|
+
refine Object do
|
41
|
+
def send_array(*messages)
|
42
|
+
MultiSend.array(self, *messages)
|
43
|
+
end
|
44
|
+
|
45
|
+
def send_hash(messages = {})
|
46
|
+
MultiSend.hash(self, messages)
|
47
|
+
end
|
48
|
+
|
49
|
+
def multi_send(*messages)
|
50
|
+
MultiSend.do(self, *messages)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/multi_send.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'multi_send/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "multi_send"
|
8
|
+
spec.version = MultiSend::VERSION
|
9
|
+
spec.authors = ["Matt Parsons"]
|
10
|
+
spec.email = ["parsonsmatt@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{MultiSend makes it easy to send a bunch of messages to an object.}
|
13
|
+
spec.homepage = "https://www.github.com/get-vitamin-c/multi_send"
|
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.8"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: multi_send
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matt Parsons
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-11 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.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description:
|
56
|
+
email:
|
57
|
+
- parsonsmatt@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/multi_send.rb
|
73
|
+
- lib/multi_send/object.rb
|
74
|
+
- lib/multi_send/version.rb
|
75
|
+
- multi_send.gemspec
|
76
|
+
homepage: https://www.github.com/get-vitamin-c/multi_send
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.4.6
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: MultiSend makes it easy to send a bunch of messages to an object.
|
100
|
+
test_files: []
|