shorthand 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.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.md +56 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/lib/shorthand.rb +45 -0
- data/test/helper.rb +10 -0
- data/test/test_shorthand.rb +63 -0
- metadata +83 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Bruce Williams
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
Shorthand
|
2
|
+
=========
|
3
|
+
|
4
|
+
Shorthand gives you an easy way to add a object-focused DSL (an OSL,
|
5
|
+
if you will) useful for configuring instances -- without dirtying up
|
6
|
+
your class implementation. This of it as running your DSL in a little
|
7
|
+
sandbox.
|
8
|
+
|
9
|
+
For example, let's say we have a class, Person:
|
10
|
+
|
11
|
+
class Person
|
12
|
+
# with a lot of accessors
|
13
|
+
end
|
14
|
+
|
15
|
+
And we'd like an API like:
|
16
|
+
|
17
|
+
Person.new do
|
18
|
+
name 'Bruce Williams'
|
19
|
+
location 'Portland, OR'
|
20
|
+
end
|
21
|
+
|
22
|
+
Provided we had `name=` and `location=` attribute writers defined on
|
23
|
+
`Person`, this would be as easy as:
|
24
|
+
|
25
|
+
class Person
|
26
|
+
include Shorthand # this line
|
27
|
+
end
|
28
|
+
|
29
|
+
If you prefer yield-style semantics, rest assured this API would work
|
30
|
+
as well:
|
31
|
+
|
32
|
+
Person.new do |person|
|
33
|
+
person.name 'Bruce Williams'
|
34
|
+
person.location 'Portland, OR'
|
35
|
+
end
|
36
|
+
|
37
|
+
You can also use the `shorthand` method after initialization:
|
38
|
+
|
39
|
+
person = Person.new('Bruce', 'Williams')
|
40
|
+
person.shorthand do
|
41
|
+
location 'Portland, OR'
|
42
|
+
zipcode '97209'
|
43
|
+
end
|
44
|
+
|
45
|
+
Want to one use one or the other? You can include the ala-carte
|
46
|
+
modules `Shorthand::Init` or `Shorthand::Method` instead.
|
47
|
+
|
48
|
+
Want to give the `shorthand` method another name (like `configure`)?
|
49
|
+
Have at it with `alias` and `alias_method`.
|
50
|
+
|
51
|
+
Copyright
|
52
|
+
---------
|
53
|
+
|
54
|
+
Copyright (c) 2010 Bruce Williams. See LICENSE for details.
|
55
|
+
|
56
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "shorthand"
|
8
|
+
gem.summary = %Q{Externalize object-focused DSLs on attribute writers}
|
9
|
+
gem.description = %Q{This provides an easy way to build simple configuration-style DSLs and run them 'sandboxed' outside the instances they configure.}
|
10
|
+
gem.email = "bruce@codefluency.com"
|
11
|
+
gem.homepage = "http://github.com/bruce/shorthand"
|
12
|
+
gem.authors = ["Bruce Williams"]
|
13
|
+
gem.add_development_dependency "shoulda", ">= 0"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "shorthand #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/shorthand.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
module Shorthand
|
2
|
+
|
3
|
+
def self.included(target_class)
|
4
|
+
target_class.instance_eval do
|
5
|
+
include Init
|
6
|
+
include Method
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module Init
|
11
|
+
def initialize(*args, &block)
|
12
|
+
super
|
13
|
+
if block
|
14
|
+
::Shorthand::Provider.new(self, &block)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
module Method
|
20
|
+
def shorthand(&block)
|
21
|
+
if block
|
22
|
+
::Shorthand::Provider.new(self, &block)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Provider
|
28
|
+
def initialize(target, &block)
|
29
|
+
@target = target
|
30
|
+
if block.arity == 1
|
31
|
+
yield self
|
32
|
+
else
|
33
|
+
instance_eval(&block)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
def method_missing(meth, *args, &block)
|
37
|
+
if @target.respond_to?(message = "#{meth}=", true)
|
38
|
+
@target.__send__(message, *args, &block)
|
39
|
+
else
|
40
|
+
super
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestShorthand < Test::Unit::TestCase
|
4
|
+
|
5
|
+
context 'instances of classes including Shorthand' do
|
6
|
+
setup do
|
7
|
+
@klass = Class.new do
|
8
|
+
include Shorthand
|
9
|
+
attr_accessor :foo
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
context "using the shorthand method" do
|
14
|
+
subject do
|
15
|
+
@klass.new
|
16
|
+
end
|
17
|
+
should 'have an method named shorthand' do
|
18
|
+
assert subject.respond_to?(:shorthand)
|
19
|
+
end
|
20
|
+
context 'when calling the writer in shorthand with instance_eval semantics' do
|
21
|
+
setup do
|
22
|
+
subject.shorthand { foo 1 }
|
23
|
+
end
|
24
|
+
should_change 'assigned value', :from => nil, :to => 1 do
|
25
|
+
subject.foo
|
26
|
+
end
|
27
|
+
end
|
28
|
+
context 'when calling the writer in shorthand with yield semantics' do
|
29
|
+
setup do
|
30
|
+
subject.shorthand { |s| s.foo 1 }
|
31
|
+
end
|
32
|
+
should_change 'assigned value', :from => nil, :to => 1 do
|
33
|
+
subject.foo
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
context "initializing with a block" do
|
38
|
+
context 'with instance_eval semantics' do
|
39
|
+
subject do
|
40
|
+
@klass.new { foo 1 }
|
41
|
+
end
|
42
|
+
should 'set foo' do
|
43
|
+
assert_equal 1, subject.foo
|
44
|
+
end
|
45
|
+
end
|
46
|
+
context 'with yield semantics' do
|
47
|
+
subject do
|
48
|
+
@klass.new { |f| f.foo 1 }
|
49
|
+
end
|
50
|
+
should 'set foo' do
|
51
|
+
assert_equal 1, subject.foo
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def shorthand(&block)
|
59
|
+
subject.shorthand(&block)
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shorthand
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Bruce Williams
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-18 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: shoulda
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :development
|
31
|
+
version_requirements: *id001
|
32
|
+
description: This provides an easy way to build simple configuration-style DSLs and run them 'sandboxed' outside the instances they configure.
|
33
|
+
email: bruce@codefluency.com
|
34
|
+
executables: []
|
35
|
+
|
36
|
+
extensions: []
|
37
|
+
|
38
|
+
extra_rdoc_files:
|
39
|
+
- LICENSE
|
40
|
+
- README.md
|
41
|
+
files:
|
42
|
+
- .document
|
43
|
+
- .gitignore
|
44
|
+
- LICENSE
|
45
|
+
- README.md
|
46
|
+
- Rakefile
|
47
|
+
- VERSION
|
48
|
+
- lib/shorthand.rb
|
49
|
+
- test/helper.rb
|
50
|
+
- test/test_shorthand.rb
|
51
|
+
has_rdoc: true
|
52
|
+
homepage: http://github.com/bruce/shorthand
|
53
|
+
licenses: []
|
54
|
+
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options:
|
57
|
+
- --charset=UTF-8
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
segments:
|
65
|
+
- 0
|
66
|
+
version: "0"
|
67
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 1.3.6
|
78
|
+
signing_key:
|
79
|
+
specification_version: 3
|
80
|
+
summary: Externalize object-focused DSLs on attribute writers
|
81
|
+
test_files:
|
82
|
+
- test/helper.rb
|
83
|
+
- test/test_shorthand.rb
|