ruby-with 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +108 -0
- data/Rakefile +13 -0
- data/lib/ruby-with.rb +26 -0
- data/lib/ruby-with/global.rb +3 -0
- data/lib/ruby-with/version.rb +5 -0
- data/ruby-with.gemspec +21 -0
- data/test/test_helper.rb +9 -0
- data/test/test_with.rb +29 -0
- metadata +90 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Colin Young
|
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,108 @@
|
|
1
|
+
## A `with` keyword for Ruby
|
2
|
+
#### (That has nothing to do with python's `with` keyword)
|
3
|
+
|
4
|
+
[![Build Status](https://travis-ci.org/colinyoung/ruby-with.png?branch=master)](undefined)
|
5
|
+
|
6
|
+
`with` lets you seamlessly make any object the current context.
|
7
|
+
|
8
|
+
### Why
|
9
|
+
|
10
|
+
I'm tired of doing this:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
users.each do |user|
|
14
|
+
puts user.name
|
15
|
+
end
|
16
|
+
```
|
17
|
+
|
18
|
+
Or things like this:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
Project.new.tap do |proj|
|
22
|
+
proj.name = "hello"
|
23
|
+
proj.owner = owner
|
24
|
+
proj.created_at = Time.now
|
25
|
+
end
|
26
|
+
```
|
27
|
+
|
28
|
+
So I made it way more -- well, Ruby-ish.
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
with Project.new do
|
32
|
+
puts "A project called #{name}
|
33
|
+
was started by #{owner}
|
34
|
+
at #{created_at}."
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
### The `with` keyword
|
39
|
+
|
40
|
+
> Note: Ruby 1.9x only for now!
|
41
|
+
|
42
|
+
![](http://i.imgur.com/0FIK5.png)
|
43
|
+
|
44
|
+
To use, just call `with`:
|
45
|
+
|
46
|
+
```
|
47
|
+
users.each do |user|
|
48
|
+
with user do
|
49
|
+
puts name
|
50
|
+
end
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
54
|
+
You have two options to use ruby-with in your app:
|
55
|
+
|
56
|
+
1. Keyword-level integration (make `with` available everywhere, because why not?):
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
# In your Gemfile
|
60
|
+
gem 'ruby-with', require: 'ruby-with/global'
|
61
|
+
```
|
62
|
+
2. On a class-by-class basis:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
# In your Gemfile
|
66
|
+
gem 'ruby-with'
|
67
|
+
|
68
|
+
# MyClass
|
69
|
+
class MyClass
|
70
|
+
include RubyWith::With
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
### Advanced
|
75
|
+
|
76
|
+
You can call `with` with an optional second argument (a Hash) that will be `set` before all your code is run.
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
with Dog.new, name: 'Rue' do
|
80
|
+
puts name # => Rue
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
Or, if you need access to your current context (kind of like inverting your existing code), you can do this:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
class Dog < Pet
|
88
|
+
|
89
|
+
belongs_to :owner
|
90
|
+
|
91
|
+
attr_accessor :name
|
92
|
+
|
93
|
+
def initialize(name)
|
94
|
+
self.name = name
|
95
|
+
|
96
|
+
with owner do |dog|
|
97
|
+
dog.name = "#{dog.name} #{last_name}" # => looks like 'DogName OwnerLastName'
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
```
|
103
|
+
|
104
|
+
It's kind of a contrived example, but I'm sure it'll come in handy.
|
105
|
+
|
106
|
+
### Changelog
|
107
|
+
|
108
|
+
* **Release 0.2:** Added setters and new documentation.
|
data/Rakefile
ADDED
data/lib/ruby-with.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require "ruby-with/version"
|
2
|
+
|
3
|
+
module With
|
4
|
+
|
5
|
+
def with context, start_set_hash={}, &block
|
6
|
+
context.instance_eval do
|
7
|
+
def set(hash)
|
8
|
+
hash.map {|k,v| self.__send__("#{k}=", v) }
|
9
|
+
end
|
10
|
+
|
11
|
+
def <=(hash)
|
12
|
+
set(hash)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context.send :set, start_set_hash
|
17
|
+
|
18
|
+
context.instance_exec(self, &block)
|
19
|
+
|
20
|
+
class << context
|
21
|
+
remove_method :set
|
22
|
+
remove_method :<=
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/ruby-with.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ruby-with/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "ruby-with"
|
8
|
+
gem.version = Ruby::With::VERSION
|
9
|
+
gem.authors = ["Colin Young"]
|
10
|
+
gem.email = ["me@colinyoung.com"]
|
11
|
+
gem.description = %q{Seamlessly make any object the current context}
|
12
|
+
gem.summary = %q{A hack of instance_exec that I feel is missing from ruby}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.add_development_dependency 'minitest'
|
17
|
+
gem.add_development_dependency 'rake'
|
18
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
end
|
data/test/test_helper.rb
ADDED
data/test/test_with.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class WithTest < MiniTest::Unit::TestCase
|
4
|
+
require 'ruby-with/global'
|
5
|
+
|
6
|
+
def test_with_exists
|
7
|
+
self.must_respond_to :with, "Doesn't respond to :with"
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_with_can_invert_context
|
11
|
+
with Person.new do |context|
|
12
|
+
context.class.must_equal WithTest
|
13
|
+
self.class.must_equal Person
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_can_set_things_at_start
|
18
|
+
with Person.new, first_name: 'Rue' do
|
19
|
+
first_name.must_equal 'Rue'
|
20
|
+
|
21
|
+
set first_name: 'Adolfo'
|
22
|
+
first_name.must_equal 'Adolfo'
|
23
|
+
|
24
|
+
self <= { first_name: 'George' }
|
25
|
+
first_name.must_equal 'George'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-with
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.2'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Colin Young
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: minitest
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Seamlessly make any object the current context
|
47
|
+
email:
|
48
|
+
- me@colinyoung.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- LICENSE.txt
|
56
|
+
- README.md
|
57
|
+
- Rakefile
|
58
|
+
- lib/ruby-with.rb
|
59
|
+
- lib/ruby-with/global.rb
|
60
|
+
- lib/ruby-with/version.rb
|
61
|
+
- ruby-with.gemspec
|
62
|
+
- test/test_helper.rb
|
63
|
+
- test/test_with.rb
|
64
|
+
homepage: ''
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.8.24
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: A hack of instance_exec that I feel is missing from ruby
|
88
|
+
test_files:
|
89
|
+
- test/test_helper.rb
|
90
|
+
- test/test_with.rb
|