auser-dslify 0.0.7 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Ari Lerner
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.
@@ -0,0 +1,33 @@
1
+ = dslify
2
+
3
+ Dslify, born out of a need for improvement on Dslify
4
+
5
+ Add dsl accessors to any class.
6
+
7
+ Usage:
8
+ class MyClass
9
+ include Dslify
10
+
11
+ dsl_methods :award, :people
12
+ end
13
+
14
+ mc = MyClass.new
15
+ mc.award "Tony Award"
16
+ mc.people ["Bob", "Frank", "Ben"]
17
+
18
+ You can set defaults as well:
19
+ class MyClass
20
+ default_options :award => "Tony Award"
21
+ end
22
+
23
+ Finally, if your tree of available accessors runs higher and longer than
24
+ just 1 file, for instance, if you use Parenting, you can set forwarders to
25
+ forward the query up the chain
26
+
27
+ class MyClass
28
+ forwards_to :parent
29
+ end
30
+
31
+ == Copyright
32
+
33
+ Copyright (c) 2009 Ari Lerner. See LICENSE for details.
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 1
4
+ :patch: 0
@@ -1,7 +1,78 @@
1
- $:.unshift(File.dirname(__FILE__)) unless
2
- $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
-
4
- Dir["#{File.dirname(__FILE__)}/dslify/*.rb"].each {|f| require f }
5
-
6
1
  module Dslify
2
+ def self.included(base)
3
+ base.send :include, InstanceMethods
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def default_options(hsh={})
9
+ (@_dsl_options ||= {}).merge! hsh
10
+ set_default_options(@_dsl_options)
11
+ end
12
+
13
+ def dsl_options
14
+ @_dsl_options ||= {}
15
+ end
16
+ def options
17
+ dsl_options
18
+ end
19
+
20
+ def dsl_methods(*syms)
21
+ syms.each {|sym| set_default_options({sym => nil}) }
22
+ end
23
+
24
+ def set_default_options(new_options)
25
+ new_options.each do |k,v|
26
+ dsl_options[k] = v
27
+ class_eval define_dsl_method_str(k)
28
+ end
29
+ end
30
+
31
+ def define_dsl_method_str(k)
32
+ <<-EOE
33
+ def #{k}(n=nil)
34
+ if n.nil?
35
+ fetch(:#{k})
36
+ else
37
+ self.#{k}=n
38
+ end
39
+ end
40
+ def #{k}=(n)
41
+ dsl_options[:#{k}] = n
42
+ end
43
+ def fetch(k)
44
+ dsl_options[k]
45
+ end
46
+ EOE
47
+ end
48
+
49
+ def inherited(subclass)
50
+ subclass.set_default_options(dsl_options)
51
+ end
52
+ end
53
+ module InstanceMethods
54
+ def dsl_options
55
+ @dsl_options ||= self.class.dsl_options.clone
56
+ end
57
+ def set_vars_from_options(hsh={})
58
+ hsh.each do |k,v|
59
+ instance_eval self.class.define_dsl_method_str(k) unless self.respond_to?(k)
60
+ self.send k, v
61
+ end
62
+ end
63
+
64
+ def set_default_options(hsh={})
65
+ self.class.set_default_options(hsh)
66
+ end
67
+
68
+ def method_missing(m,*a,&block)
69
+ if m.to_s[-1..-1] == '?'
70
+ t = m.to_s.gsub(/\?/, '').to_sym
71
+ warn "DEPRECATED: Dslify will no longer support ? methods. Fix yo code.: #{m}"
72
+ respond_to?(t) && !self.send(t, *a, &block).nil?
73
+ else
74
+ super
75
+ end
76
+ end
77
+ end
7
78
  end
@@ -0,0 +1,232 @@
1
+ require "#{File.dirname(__FILE__)}/test_helper"
2
+
3
+ class Quickie
4
+ include Dslify
5
+ def initialize(&block)
6
+ instance_eval &block if block
7
+ end
8
+ end
9
+
10
+ class QuickieTest < Test::Unit::TestCase
11
+ context "setting" do
12
+ before do
13
+ Quickie.class_eval do
14
+ dsl_methods :bank, :snobs, :author, :gilligans_island
15
+ end
16
+ @q = Quickie.new
17
+ end
18
+ it "should be able to set methods on self" do
19
+ assert_nothing_raised do
20
+ @q.bank "bobs"
21
+ end
22
+ end
23
+ it "should set and then retrieve the same value back" do
24
+ @q.snobs "are mean"
25
+ assert_equal @q.snobs, "are mean"
26
+ end
27
+ it "should set and retrieve values back with an = sign" do
28
+ @q.author = ["Ari Lerner"]
29
+ assert_equal @q.author, ["Ari Lerner"]
30
+ end
31
+ it "should set these values in the h Hash on the object" do
32
+ assert_raise NoMethodError do
33
+ @q.movies "can be fun"
34
+ end
35
+ end
36
+ it "should set multiple keys with set_vars_from_options" do
37
+ @q.set_vars_from_options({:a => "a", :b => "b"})
38
+ assert_equal @q.a, "a"
39
+ assert_equal @q.b, "b"
40
+ end
41
+ it "should set methods even when they are called with a block" do
42
+ @q.author Quickie.new do
43
+ end
44
+ assert_equal @q.author.class, Quickie
45
+ end
46
+ end
47
+
48
+ context "calling methods on an instance" do
49
+ setup do
50
+ class Detective
51
+ include Dslify
52
+ attr_reader :snooped
53
+ def snoop(*n)
54
+ @snooped = "done!"
55
+ end
56
+ end
57
+ @d= Detective.new
58
+ end
59
+ should "Call the method snoop with set_vars_from_options" do
60
+ @d.set_vars_from_options(:snoop => true)
61
+ assert @d.snooped
62
+ end
63
+ end
64
+
65
+ context "default options" do
66
+ setup do
67
+ class Bang
68
+ include Dslify
69
+ default_options(
70
+ :says => 'vmrun'
71
+ )
72
+ def initialize(opts={}, &block)
73
+ instance_eval &block if block
74
+ end
75
+ end
76
+ @bang = Bang.new
77
+ end
78
+
79
+ should "overwrite the default dsl option in instance_eval" do
80
+ assert_equal @bang.says, "vmrun"
81
+ @bang = Bang.new do
82
+ says "snake"
83
+ end
84
+ assert_equal @bang.says, "snake"
85
+ end
86
+ end
87
+
88
+
89
+ context "with inheritance and classes" do
90
+ before do
91
+ class Pop
92
+ include Dslify
93
+ default_options :name => "pop", :flavor=>'cherry'
94
+ def initialize(o={})
95
+ set_vars_from_options(o)
96
+ end
97
+
98
+ def real_method
99
+ "the real deal, no magic"
100
+ end
101
+ end
102
+
103
+ class Foo < Pop
104
+ default_options :name=>'fooey'
105
+ end
106
+
107
+ class Bar < Pop
108
+ default_options :name=>'pangy', :taste => "spicy"
109
+ end
110
+
111
+ class Dad < Pop
112
+ end
113
+
114
+ class Grandad < Dad
115
+ end
116
+
117
+ class Defaults < Pop
118
+ default_options(
119
+ :global_default => "red_rum"
120
+ )
121
+ end
122
+
123
+ @pop = Pop.new
124
+ @foo = Foo.new
125
+ @bar = Bar.new
126
+ end
127
+ it "should take the default options set on the class" do
128
+ assert_equal @pop.dsl_options[:name], "pop"
129
+ assert_equal @pop.name, "pop"
130
+ end
131
+ it "should allow us to add defaults on the instance by calling dsl_options" do
132
+ # QuickieTest::Pop.name == "Cinnamon"
133
+ @poptart = Pop.new :name => "Cinnamon"
134
+ assert_equal @poptart.name, "Cinnamon"
135
+ end
136
+ it "should take the default options on a second class that inherits from the base" do
137
+ assert_equal @foo.name, "fooey"
138
+ end
139
+ it "should take the default options on a third inheriting class" do
140
+ assert_equal @bar.name, "pangy"
141
+ end
142
+ it "should not add a method not in the default_options" do
143
+ assert_equal @bar.respond_to?(:boat), false
144
+ end
145
+ it "should return the original default options test" do
146
+ assert_equal @bar.dsl_options[:taste], "spicy"
147
+ assert_equal @bar.dsl_options[:name], "pangy"
148
+ end
149
+ it "should set the default options of the child to the superclass's if it doesn't exist" do
150
+ # QuickieTest::Dad => QuickieTest::Pop
151
+ d = Dad.new
152
+ assert Pop.new.name == 'pop'
153
+ assert_equal "pop", d.name
154
+ d.name "Frankenstein"
155
+ assert_equal d.name, "Frankenstein"
156
+ end
157
+ it "should raise if the method isn't found on itself, the parent or in the rest of the method missing chain" do
158
+ assert_raise NoMethodError do
159
+ Class.new.sanitorium
160
+ end
161
+ end
162
+ it "should be able to reach the grandparent through the chain of dsify-ed classes" do
163
+ # QuickieTest::Grandad => QuickieTest::Dad => QuickieTest::Pop
164
+ assert Dad.method_defined?(:name)
165
+ assert Dad.new.flavor == 'cherry'
166
+ assert Grandad.new.name, "pop"
167
+ end
168
+ # it "should be able to take a method that responds to an object" do
169
+ # class Tanks
170
+ # include Dslify
171
+ # forwards_to :parent
172
+ # def initialize(obj)
173
+ # @parent = obj
174
+ # end
175
+ # def parent
176
+ # @parent
177
+ # end
178
+ # end
179
+ # t = Tanks.new(@bar)
180
+ # # QuickieTest::Tanks => Object => #<QuickieTest::Bar>
181
+ # assert_equal t.taste, @bar.taste
182
+ # end
183
+ end
184
+ context "methods" do
185
+ setup do
186
+ class MrDanger
187
+ include Dslify
188
+ default_options :where_to => "The Grand Canyon"
189
+ def initialize(o={})
190
+ set_vars_from_options(o)
191
+ end
192
+ def where_to(*a)
193
+ "New York City"
194
+ end
195
+ end
196
+ end
197
+
198
+ should "not override the method where_to" do
199
+ assert_equal MrDanger.new.where_to, "New York City"
200
+ end
201
+ should "not override the method where_to when called with set_vars_from_options" do
202
+ assert_equal MrDanger.new(:where_to => "Bank of America").where_to, "New York City"
203
+ end
204
+ end
205
+
206
+ context "when calling with a block" do
207
+ setup do
208
+ class ToddTheSquare
209
+ include Dslify
210
+ default_options :provider => :vmrun, :t => :nothing
211
+
212
+ def provider(&block)
213
+ instance_eval &block if block
214
+ end
215
+ end
216
+ end
217
+
218
+ should "should not evaluate the block" do
219
+ tts = ToddTheSquare.new
220
+ assert_equal tts.t, :nothing
221
+ end
222
+
223
+ should "should evaluate the block" do
224
+ tts = ToddTheSquare.new
225
+ tts.provider do
226
+ self.t = :something
227
+ end
228
+ assert_equal tts.t, :something
229
+ end
230
+ end
231
+
232
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require "context"
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ require 'dslify'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auser-dslify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ari Lerner
@@ -9,69 +9,32 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-29 00:00:00 -07:00
12
+ date: 2009-05-14 00:00:00 -07:00
13
13
  default_executable:
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
16
- name: hoe
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: 1.8.0
24
- version:
25
- description: Easily add DSL-like calls to any class
26
- email:
27
- - arilerner@mac.com
14
+ dependencies: []
15
+
16
+ description:
17
+ email: arilerner@mac.com
28
18
  executables: []
29
19
 
30
20
  extensions: []
31
21
 
32
22
  extra_rdoc_files:
33
- - History.txt
34
- - Manifest.txt
35
- - PostInstall.txt
36
- - README.txt
37
- - website/index.txt
23
+ - README.rdoc
24
+ - LICENSE
38
25
  files:
39
- - History.txt
40
- - Manifest.txt
41
- - PostInstall.txt
42
- - README.txt
43
- - Rakefile
44
- - config/hoe.rb
45
- - config/requirements.rb
46
- - dslify.gemspec
26
+ - README.rdoc
27
+ - VERSION.yml
47
28
  - lib/dslify.rb
48
- - lib/dslify/dslify.rb
49
- - lib/dslify/version.rb
50
- - script/console
51
- - script/destroy
52
- - script/generate
53
- - script/txt2html
54
- - setup.rb
55
- - tasks/deployment.rake
56
- - tasks/environment.rake
57
- - tasks/website.rake
58
- - test/test_dslify.rb
59
- - website/index.html
60
- - website/index.txt
61
- - website/javascripts/rounded_corners_lite.inc.js
62
- - website/stylesheets/screen.css
63
- - website/template.html.erb
29
+ - test/dslify_test.rb
30
+ - test/test_helper.rb
31
+ - LICENSE
64
32
  has_rdoc: true
65
- homepage: http://dslify.rubyforge.org
66
- post_install_message: |-
67
- Thanks for installing dslify!
68
-
69
- For more information on dslify, see http://dslify.rubyforge.org
70
-
71
- Ari Lerner
33
+ homepage: http://github.com/auser/dslify
34
+ post_install_message:
72
35
  rdoc_options:
73
- - --main
74
- - README.txt
36
+ - --inline-source
37
+ - --charset=UTF-8
75
38
  require_paths:
76
39
  - lib
77
40
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -88,10 +51,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
88
51
  version:
89
52
  requirements: []
90
53
 
91
- rubyforge_project: dslify
54
+ rubyforge_project:
92
55
  rubygems_version: 1.2.0
93
56
  signing_key:
94
57
  specification_version: 3
95
- summary: Easily add DSL-like calls to any class
96
- test_files:
97
- - test/test_dslify.rb
58
+ summary: TODO
59
+ test_files: []
60
+