docile 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p385@docile"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.18.8 (stable)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ else
29
+ # If the environment file has not yet been created, use the RVM CLI to select.
30
+ rvm --create "$environment_id" || {
31
+ echo "Failed to create RVM environment '${environment_id}'."
32
+ return 1
33
+ }
34
+ fi
35
+
36
+ # If you use bundler, this might be useful to you:
37
+ # if [[ -s Gemfile ]] && {
38
+ # ! builtin command -v bundle >/dev/null ||
39
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
40
+ # }
41
+ # then
42
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
43
+ # gem install bundler
44
+ # fi
45
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
46
+ # then
47
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
48
+ # fi
data/HISTORY.md ADDED
@@ -0,0 +1,14 @@
1
+ # HISTORY
2
+
3
+ ## [v1.0.2 (Apr 1, 2013)](http://github.com/ms-ati/docile/compare/v1.0.1...v1.0.2)
4
+
5
+ - allow passing parameters to DSL blocks (thanks @dslh!)
6
+
7
+ ## [v1.0.1 (Nov 29, 2012)](http://github.com/ms-ati/docile/compare/v1.0.0...v1.0.1)
8
+
9
+ - relaxed rspec and rake dependencies to allow newer versions
10
+ - fixes to documentation
11
+
12
+ ## [v1.0.0 (Oct 29, 2012)](http://github.com/ms-ati/docile/compare/1b225c8a27...v1.0.0)
13
+
14
+ - Initial Feature Set
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2012 Marc Siegel
1
+ Copyright (c) 2012-2013 Marc Siegel
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy of
4
4
  this software and associated documentation files (the "Software"), to deal in
data/README.md CHANGED
@@ -81,6 +81,64 @@ It's just that easy!
81
81
 
82
82
  [2]: http://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern "Builder Pattern"
83
83
 
84
+ ## Block parameters
85
+
86
+ Parameters can be passed to the DSL block.
87
+
88
+ Supposing you want to make some sort of cheap [Sinatra][3] knockoff:
89
+ ```ruby
90
+ @last_request = nil
91
+ respond '/path' do |request|
92
+ puts "Request received: #{request}"
93
+ @last_request = request
94
+ end
95
+
96
+ def ride bike
97
+ # Play with your new bike
98
+ end
99
+
100
+ respond '/new_bike' do |bike|
101
+ ride(bike)
102
+ end
103
+ ```
104
+
105
+ You'd put together a dispatcher something like this:
106
+ ```ruby
107
+ require 'singleton'
108
+
109
+ class DispatchScope
110
+ def a_method_you_can_call_from_inside_the_block
111
+ :useful_huh?
112
+ end
113
+ end
114
+
115
+ class MessageDispatch
116
+ include Singleton
117
+
118
+ def initialize
119
+ @responders = {}
120
+ end
121
+
122
+ def add_responder path, &block
123
+ @responders[path] = block
124
+ end
125
+
126
+ def dispatch path, request
127
+ Docile.dsl_eval(DispatchScope.new, request, &@responders[path])
128
+ end
129
+ end
130
+
131
+ def respond path, &handler
132
+ MessageDispatch.instance.add_responder path, handler
133
+ end
134
+
135
+ def send_request path, request
136
+ MessageDispatch.instance.dispatch path, request
137
+ end
138
+ ```
139
+
140
+ [3]: http://www.sinatrarb.com "Sinatra"
141
+
84
142
  ## Features
85
143
 
86
144
  1. method lookup falls back from the DSL object to the block's context
@@ -102,7 +160,7 @@ Or, read the code hosted on *github.com*: [Docile Code](https://github.com/ms-at
102
160
 
103
161
  ## Status
104
162
 
105
- Version 1.0 works on [all ruby versions](https://github.com/ms-ati/docile/blob/master/.travis.yml).
163
+ Version 1.0.x works on [all ruby versions since 1.8.7](https://github.com/ms-ati/docile/blob/master/.travis.yml).
106
164
 
107
165
  ## Note on Patches/Pull Requests
108
166
 
@@ -117,4 +175,4 @@ Version 1.0 works on [all ruby versions](https://github.com/ms-ati/docile/blob/m
117
175
 
118
176
  ## Copyright
119
177
 
120
- Copyright (c) 2012 Marc Siegel. See LICENSE for details.
178
+ Copyright (c) 2012-2013 Marc Siegel. See LICENSE for details.
data/lib/docile.rb CHANGED
@@ -15,14 +15,15 @@ module Docile
15
15
  # #=> [1, 3]
16
16
  #
17
17
  # @param dsl [Object] an object whose methods represent a DSL
18
+ # @param args [Array] arguments to be passed to the block
18
19
  # @param block [Proc] a block to execute in the DSL context
19
20
  # @return [Object] the dsl object, after execution of the block
20
- def dsl_eval(dsl, &block)
21
+ def dsl_eval(dsl, *args, &block)
21
22
  block_context = eval("self", block.binding)
22
23
  proxy_context = FallbackContextProxy.new(dsl, block_context)
23
24
  begin
24
25
  block_context.instance_variables.each { |ivar| proxy_context.instance_variable_set(ivar, block_context.instance_variable_get(ivar)) }
25
- proxy_context.instance_eval(&block)
26
+ proxy_context.instance_exec(*args,&block)
26
27
  ensure
27
28
  block_context.instance_variables.each { |ivar| block_context.instance_variable_set(ivar, proxy_context.instance_variable_get(ivar)) }
28
29
  end
@@ -2,7 +2,7 @@ require 'set'
2
2
 
3
3
  module Docile
4
4
  class FallbackContextProxy
5
- NON_PROXIED_METHODS = Set[:object_id, :__send__, :__id__, :==, :equal?, :"!", :"!=", :instance_eval,
5
+ NON_PROXIED_METHODS = Set[:object_id, :__send__, :__id__, :==, :equal?, :"!", :"!=", :instance_exec,
6
6
  :instance_variables, :instance_variable_get, :instance_variable_set,
7
7
  :remove_instance_variable]
8
8
 
@@ -50,4 +50,4 @@ module Docile
50
50
  end
51
51
  end
52
52
  end
53
- end
53
+ end
@@ -1,3 +1,3 @@
1
1
  module Docile
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
data/spec/docile_spec.rb CHANGED
@@ -45,12 +45,46 @@ describe Docile do
45
45
  def inner(&block)
46
46
  Docile.dsl_eval(InnerDSL.new, &block)
47
47
  end
48
+
49
+ def inner_with_params(param,&block)
50
+ Docile.dsl_eval(InnerDSL.new, param, :foo, &block)
51
+ end
48
52
  end
49
53
 
50
54
  def outer(&block)
51
55
  Docile.dsl_eval(OuterDSL.new, &block)
52
56
  end
53
57
 
58
+ def parameterized(*args,&block)
59
+ Docile.dsl_eval(OuterDSL.new, *args, &block)
60
+ end
61
+
62
+ context "parameters" do
63
+ it "should pass parameters to the block" do
64
+ parameterized(1,2,3) do |x,y,z|
65
+ x.should == 1
66
+ y.should == 2
67
+ z.should == 3
68
+ end
69
+ end
70
+
71
+ it "should find parameters before methods" do
72
+ parameterized(1) { |a| a.should == 1 }
73
+ end
74
+
75
+ it "should find outer parameters in inner dsl scope" do
76
+ parameterized(1,2,3) do |a,b,c|
77
+ inner_with_params(c) do |d,e|
78
+ a.should == 1
79
+ b.should == 2
80
+ c.should == 3
81
+ d.should == c
82
+ e.should == :foo
83
+ end
84
+ end
85
+ end
86
+ end
87
+
54
88
  context "methods" do
55
89
  it "should find method of outer dsl in outer dsl scope" do
56
90
  outer { a.should == 'a' }
@@ -111,6 +145,65 @@ describe Docile do
111
145
  end
112
146
  end
113
147
 
148
+ class DispatchScope
149
+ def params
150
+ { :a => 1, :b => 2, :c => 3 }
151
+ end
152
+ end
153
+
154
+ class MessageDispatch
155
+ include Singleton
156
+
157
+ def initialize
158
+ @responders = {}
159
+ end
160
+
161
+ def add_responder path, &block
162
+ @responders[path] = block
163
+ end
164
+
165
+ def dispatch path, request
166
+ Docile.dsl_eval(DispatchScope.new, request, &@responders[path])
167
+ end
168
+ end
169
+
170
+ def respond path, &block
171
+ MessageDispatch.instance.add_responder path, &block
172
+ end
173
+
174
+ def send_request path, request
175
+ MessageDispatch.instance.dispatch path, request
176
+ end
177
+
178
+ it "should handle the dispatch pattern" do
179
+ @first = @second = nil
180
+ respond '/path' do |request|
181
+ @first = request
182
+ end
183
+
184
+ respond '/new_bike' do |bike|
185
+ @second = "Got a new #{bike}"
186
+ end
187
+
188
+ def x(y) ; "Got a #{y}"; end
189
+ respond '/third' do |third|
190
+ x(third).should == 'Got a third thing'
191
+ end
192
+
193
+ fourth = nil
194
+ respond '/params' do |arg|
195
+ fourth = params[arg]
196
+ end
197
+
198
+ send_request '/path', 1
199
+ send_request '/new_bike', 'ten speed'
200
+ send_request '/third', 'third thing'
201
+ send_request '/params', :b
202
+
203
+ @first.should == 1
204
+ @second.should == 'Got a new ten speed'
205
+ fourth.should == 2
206
+ end
114
207
  end
115
208
 
116
- end
209
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'rspec'
3
+ require 'singleton'
3
4
 
4
5
  test_dir = File.dirname(__FILE__)
5
6
  $LOAD_PATH.unshift test_dir unless $LOAD_PATH.include?(test_dir)
@@ -7,4 +8,4 @@ $LOAD_PATH.unshift test_dir unless $LOAD_PATH.include?(test_dir)
7
8
  lib_dir = File.join(File.dirname(test_dir), 'lib')
8
9
  $LOAD_PATH.unshift lib_dir unless $LOAD_PATH.include?(lib_dir)
9
10
 
10
- require 'docile'
11
+ require 'docile'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: docile
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-29 00:00:00.000000000 Z
12
+ date: 2013-04-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -100,9 +100,11 @@ extensions: []
100
100
  extra_rdoc_files: []
101
101
  files:
102
102
  - .gitignore
103
+ - .rvmrc
103
104
  - .travis.yml
104
105
  - .yardopts
105
106
  - Gemfile
107
+ - HISTORY.md
106
108
  - LICENSE
107
109
  - README.md
108
110
  - Rakefile
@@ -126,7 +128,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
126
128
  version: '0'
127
129
  segments:
128
130
  - 0
129
- hash: -2020809152847392489
131
+ hash: -106941105642538059
130
132
  required_rubygems_version: !ruby/object:Gem::Requirement
131
133
  none: false
132
134
  requirements:
@@ -135,10 +137,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
135
137
  version: '0'
136
138
  segments:
137
139
  - 0
138
- hash: -2020809152847392489
140
+ hash: -106941105642538059
139
141
  requirements: []
140
142
  rubyforge_project: docile
141
- rubygems_version: 1.8.24
143
+ rubygems_version: 1.8.25
142
144
  signing_key:
143
145
  specification_version: 3
144
146
  summary: Docile keeps your Ruby DSL's tame and well-behaved