ampersand_x 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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +55 -0
- data/Rakefile +1 -0
- data/ampersand_x.gemspec +22 -0
- data/lib/ampersand_x.rb +36 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: c8dc9114348506c6868038f86334bdbcfa7ce59d
|
4
|
+
data.tar.gz: be20ef5f5c6d387a1c10cc9bee9dfa15b924fbc0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0d4b43bc56c5439ffad29e81195490cb3202c83515504d1bfe525f8f3a24527163f7113247dae80db9a5dfa8b82eedb84fec62fddd82767f4641bc2c0e58679e
|
7
|
+
data.tar.gz: 489aaae25816ccd58e74495b448b0cf20be878eeaae8c753fcb613e3a3e0c7affe230825cc384e3cf390229851b105d47357b681675cd1155bf8371a0d634627
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Gosha Arinich
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
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
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# &X
|
2
|
+
|
3
|
+
Ruby procs can be tiresome. &X (read as "ampersand ex") is a gem to vastly
|
4
|
+
simplify this and bring expressiveness back to your code.
|
5
|
+
|
6
|
+
You'd use &X in places where you need a simple proc, but are too lazy to
|
7
|
+
type `{ |some_object| some_object.property.another > 42 }` yourself.
|
8
|
+
|
9
|
+
There is `&:method_name`, but it wouldn't work with method chains and
|
10
|
+
arguments to each one. &X tries to fill the gap here.
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
Person = Stuct.new(:first, :age)
|
14
|
+
|
15
|
+
patrick = Person.new("Patrick", 19)
|
16
|
+
jane = Person.new("Jane", 21)
|
17
|
+
jack = Person.new("Jack", 20)
|
18
|
+
donald = Person.new("Donald", 25)
|
19
|
+
|
20
|
+
people = [patrick, jane, jack, donald]
|
21
|
+
|
22
|
+
# plain ruby
|
23
|
+
people.partition { |person| person.age > 20 }
|
24
|
+
|
25
|
+
# with &X
|
26
|
+
people.partition(&X.age > 20)
|
27
|
+
```
|
28
|
+
|
29
|
+
You can traverse a long chain of properties as well.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
# Suppose we have a Person class with +address+ property,
|
33
|
+
# which, in turn, has +city+ and +street+ properties.
|
34
|
+
# We want to find all people older than 20 living in Chicago...
|
35
|
+
|
36
|
+
# plain ruby
|
37
|
+
people.select { |person| person.age > 20 && person.address.city == "Chicago" }
|
38
|
+
|
39
|
+
# &X
|
40
|
+
people.select(&X.age > 20).select(&X.address.city == "Chicago")
|
41
|
+
```
|
42
|
+
|
43
|
+
## Installation
|
44
|
+
|
45
|
+
Add this line to your application's Gemfile:
|
46
|
+
|
47
|
+
gem 'ampersand_x'
|
48
|
+
|
49
|
+
Or install it yourself as:
|
50
|
+
|
51
|
+
$ gem install ampersand_x
|
52
|
+
|
53
|
+
## License
|
54
|
+
|
55
|
+
[MIT](LICENSE).
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
data/ampersand_x.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ampersand_x'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'ampersand_x'
|
8
|
+
spec.version = AmpersandX::VERSION
|
9
|
+
spec.authors = ['Gosha Arinich']
|
10
|
+
spec.email = ['me@goshakkk.name']
|
11
|
+
spec.summary = %q{&X brings expressiveness back to Ruby procs}
|
12
|
+
spec.homepage = 'https://github.com/goshakkk/ampersand_x'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
21
|
+
spec.add_development_dependency 'rake'
|
22
|
+
end
|
data/lib/ampersand_x.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module AmpersandX
|
2
|
+
VERSION = "0.0.1"
|
3
|
+
|
4
|
+
class Proxy
|
5
|
+
class << self
|
6
|
+
def method_missing(name, *args)
|
7
|
+
new([name, *args])
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
# Initialize a +Proxy+.
|
12
|
+
#
|
13
|
+
# @param path [Array<Array>] path array
|
14
|
+
def initialize(path = [])
|
15
|
+
@path = path
|
16
|
+
end
|
17
|
+
|
18
|
+
def method_missing(*args)
|
19
|
+
self.class.new(@path + [args])
|
20
|
+
end
|
21
|
+
|
22
|
+
# Get the actual value after calling all methods from +path+.
|
23
|
+
#
|
24
|
+
# @param x [Object] object to call path methods on
|
25
|
+
def actual(x)
|
26
|
+
@path.reduce(x) { |memo, prop| memo.send(*prop) }
|
27
|
+
end
|
28
|
+
|
29
|
+
# Convert proxy to a proc.
|
30
|
+
def to_proc
|
31
|
+
-> x { actual(x) }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
X = AmpersandX::Proxy
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ampersand_x
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Gosha Arinich
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-01-14 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.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- me@goshakkk.name
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- ampersand_x.gemspec
|
54
|
+
- lib/ampersand_x.rb
|
55
|
+
homepage: https://github.com/goshakkk/ampersand_x
|
56
|
+
licenses:
|
57
|
+
- MIT
|
58
|
+
metadata: {}
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 2.2.0
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: "&X brings expressiveness back to Ruby procs"
|
79
|
+
test_files: []
|
80
|
+
has_rdoc:
|