with_object 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.travis.yml +6 -0
- data/CHANGELOG +3 -0
- data/Gemfile +2 -0
- data/LICENSE +22 -0
- data/README.md +48 -0
- data/Rakefile +6 -0
- data/lib/with_object/version.rb +3 -0
- data/lib/with_object.rb +25 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/with_object/with_object_spec.rb +42 -0
- data/with_object.gemspec +20 -0
- metadata +88 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/CHANGELOG
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Mitch Crowe
|
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,48 @@
|
|
1
|
+
# WithObject
|
2
|
+
|
3
|
+
Ever gotten frustrated with duplication when writing something like:
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
report.update_totals
|
7
|
+
report.balance_totals
|
8
|
+
report.synchronize
|
9
|
+
report
|
10
|
+
```
|
11
|
+
|
12
|
+
Did you use `tap`, or use a single character variable name to reduce the typing?
|
13
|
+
|
14
|
+
Wouldn't you rather write:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
with report do
|
18
|
+
update_totals
|
19
|
+
balance_totals
|
20
|
+
synchronize
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
Use `with_object`!
|
25
|
+
|
26
|
+
## Installation
|
27
|
+
|
28
|
+
Add this line to your application's Gemfile:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
gem 'with_object'
|
32
|
+
```
|
33
|
+
|
34
|
+
And then execute:
|
35
|
+
|
36
|
+
$ bundle
|
37
|
+
|
38
|
+
## Usage
|
39
|
+
|
40
|
+
It's pretty simple. Just call `with` anywhere. Method calls inside the block are called on the object, and the whole expression returns the object. Privacy of methods and instance variables are maintained, and `NoMethodError` exceptions are thrown as appropriate. In short, it works the way you'd expect it to...
|
41
|
+
|
42
|
+
## Contributing
|
43
|
+
|
44
|
+
1. Fork it
|
45
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
46
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
47
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
48
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/with_object.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'with_object/version'
|
2
|
+
|
3
|
+
module WithObject
|
4
|
+
|
5
|
+
def with(object, &block)
|
6
|
+
context = WithContext.new(object)
|
7
|
+
context.instance_eval(&block)
|
8
|
+
object
|
9
|
+
end
|
10
|
+
|
11
|
+
class WithContext
|
12
|
+
|
13
|
+
def initialize(object)
|
14
|
+
@object = object
|
15
|
+
end
|
16
|
+
|
17
|
+
def method_missing(meth, *args, &block)
|
18
|
+
@object.public_send(meth, *args, &block)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
Object.send(:include, WithObject)
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe WithObject do
|
4
|
+
|
5
|
+
class Report
|
6
|
+
|
7
|
+
def allowed; end
|
8
|
+
def not_allowed; end
|
9
|
+
|
10
|
+
private :not_allowed
|
11
|
+
end
|
12
|
+
|
13
|
+
let(:report) { Report.new }
|
14
|
+
|
15
|
+
it 'returns the object' do
|
16
|
+
with(report){}.should == report
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'does not call private methods on the object' do
|
20
|
+
expect {
|
21
|
+
with(report) { not_allowed }
|
22
|
+
}.to raise_error(NoMethodError)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'calls public methods on the object' do
|
26
|
+
expect {
|
27
|
+
with(report) { allowed }
|
28
|
+
}.not_to raise_error
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'allows chaining multiple methods' do
|
32
|
+
a = "12345"
|
33
|
+
|
34
|
+
with a do
|
35
|
+
replace("54321")
|
36
|
+
chop!
|
37
|
+
end
|
38
|
+
|
39
|
+
a.should == "5432"
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
data/with_object.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/with_object/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Mitch Crowe"]
|
6
|
+
gem.email = ["crowe.mitch@gmail.com"]
|
7
|
+
gem.description = %q{Syntax sugar for calling a sequence of methods on a Ruby object}
|
8
|
+
gem.summary = %q{Syntax sugar for calling a sequence of methods on a Ruby object}
|
9
|
+
gem.homepage = "https://github.com/mcrowe/with_object"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(spec)/})
|
14
|
+
gem.name = "with_object"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = WithObject::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rake', '~> 0.9'
|
19
|
+
gem.add_development_dependency 'rspec', '~> 2.8'
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: with_object
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mitch Crowe
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-11-01 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &70185244682740 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0.9'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70185244682740
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70185244682240 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '2.8'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70185244682240
|
36
|
+
description: Syntax sugar for calling a sequence of methods on a Ruby object
|
37
|
+
email:
|
38
|
+
- crowe.mitch@gmail.com
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- .gitignore
|
44
|
+
- .rspec
|
45
|
+
- .travis.yml
|
46
|
+
- CHANGELOG
|
47
|
+
- Gemfile
|
48
|
+
- LICENSE
|
49
|
+
- README.md
|
50
|
+
- Rakefile
|
51
|
+
- lib/with_object.rb
|
52
|
+
- lib/with_object/version.rb
|
53
|
+
- spec/spec_helper.rb
|
54
|
+
- spec/with_object/with_object_spec.rb
|
55
|
+
- with_object.gemspec
|
56
|
+
homepage: https://github.com/mcrowe/with_object
|
57
|
+
licenses: []
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
hash: 1409837063552949166
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
hash: 1409837063552949166
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.8.16
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Syntax sugar for calling a sequence of methods on a Ruby object
|
86
|
+
test_files:
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
- spec/with_object/with_object_spec.rb
|