maiha-dsl_accessor 0.2.2 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README +45 -1
- data/Rakefile +53 -0
- data/core_ext/class/dsl_accessor.rb +24 -2
- data/spec/auto_declared_spec.rb +106 -0
- data/spec/spec_helper.rb +5 -0
- metadata +16 -13
- data/init.rb +0 -3
- data/install.rb +0 -1
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 [maiha@wota.jp]
|
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
CHANGED
@@ -126,6 +126,50 @@ Install
|
|
126
126
|
git://github.com/maiha/dsl_accessor.git
|
127
127
|
|
128
128
|
|
129
|
+
Auto declared mode
|
130
|
+
==================
|
131
|
+
|
132
|
+
Calling dsl_accessor without args enters auto declared mode.
|
133
|
+
In this mode, a method missing means instance method creation.
|
134
|
+
This affects only methods with a block and no other args.
|
135
|
+
|
136
|
+
class Foo
|
137
|
+
dsl_accessor # auto declared mode
|
138
|
+
foo{1} # define :foo
|
139
|
+
bar(a) # NoMethodError
|
140
|
+
baz(a){2} # NoMethodError
|
141
|
+
end
|
142
|
+
|
143
|
+
Foo.new.foo # => 1
|
144
|
+
|
145
|
+
This is useful when you have many methods those are one lined methods.
|
146
|
+
|
147
|
+
[without auto delared mode]
|
148
|
+
class Foo
|
149
|
+
def last
|
150
|
+
num_pages
|
151
|
+
end
|
152
|
+
|
153
|
+
def first?
|
154
|
+
page == 1
|
155
|
+
end
|
156
|
+
|
157
|
+
def offset
|
158
|
+
model.proxy_options[:offset]
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
[with auto delared mode]
|
163
|
+
class Foo
|
164
|
+
dsl_accessor
|
165
|
+
last {num_pages}
|
166
|
+
first? {page == 1}
|
167
|
+
offset {model.proxy_options[:offset]}
|
168
|
+
end
|
169
|
+
|
170
|
+
|
171
|
+
|
129
172
|
Author
|
130
173
|
======
|
131
|
-
Maiha <
|
174
|
+
Maiha <maiha@wota.jp>
|
175
|
+
|
data/Rakefile
CHANGED
@@ -20,3 +20,56 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
20
20
|
rdoc.rdoc_files.include('README')
|
21
21
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
22
|
end
|
23
|
+
|
24
|
+
|
25
|
+
######################################################################
|
26
|
+
### for gem
|
27
|
+
|
28
|
+
require 'rubygems'
|
29
|
+
require 'rake/gempackagetask'
|
30
|
+
|
31
|
+
GEM_NAME = "dsl_accessor"
|
32
|
+
AUTHOR = "maiha"
|
33
|
+
EMAIL = "maiha@wota.jp"
|
34
|
+
HOMEPAGE = "http://github.com/maiha/dsl_accessor"
|
35
|
+
SUMMARY = "This plugin gives hybrid accessor class methods to classes by DSL like definition"
|
36
|
+
GEM_VERSION = "0.3.0"
|
37
|
+
|
38
|
+
spec = Gem::Specification.new do |s|
|
39
|
+
# s.rubyforge_project = 'merb'
|
40
|
+
s.name = GEM_NAME
|
41
|
+
s.version = GEM_VERSION
|
42
|
+
s.platform = Gem::Platform::RUBY
|
43
|
+
s.has_rdoc = true
|
44
|
+
s.extra_rdoc_files = ["README", "LICENSE"]
|
45
|
+
s.summary = SUMMARY
|
46
|
+
s.description = s.summary
|
47
|
+
s.author = AUTHOR
|
48
|
+
s.email = EMAIL
|
49
|
+
s.homepage = HOMEPAGE
|
50
|
+
s.require_path = 'lib'
|
51
|
+
s.files = %w(LICENSE README Rakefile) + Dir.glob("{core_ext,lib,spec,tasks,test}/**/*")
|
52
|
+
s.add_dependency('activesupport', '>= 2.0.0')
|
53
|
+
end
|
54
|
+
|
55
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
56
|
+
pkg.gem_spec = spec
|
57
|
+
end
|
58
|
+
|
59
|
+
desc "Install the gem"
|
60
|
+
task :install do
|
61
|
+
Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
|
62
|
+
end
|
63
|
+
|
64
|
+
desc "Uninstall the gem"
|
65
|
+
task :uninstall do
|
66
|
+
Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
|
67
|
+
end
|
68
|
+
|
69
|
+
desc "Create a gemspec file"
|
70
|
+
task :gemspec do
|
71
|
+
File.open("#{GEM_NAME}.gemspec", "w") do |file|
|
72
|
+
file.puts spec.to_ruby
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
@@ -1,8 +1,13 @@
|
|
1
1
|
require 'active_support'
|
2
2
|
|
3
|
-
|
4
|
-
def dsl_accessor(
|
3
|
+
module DslAccessor
|
4
|
+
def dsl_accessor(*args)
|
5
5
|
options = args.last.is_a?(Hash) ? args.pop : {}
|
6
|
+
|
7
|
+
# mark auto_declared
|
8
|
+
name = args.shift or
|
9
|
+
return @dsl_accessor_auto_declared = true
|
10
|
+
|
6
11
|
options[:default] = args.shift unless args.empty?
|
7
12
|
|
8
13
|
case options[:instance]
|
@@ -69,5 +74,22 @@ class Class
|
|
69
74
|
end
|
70
75
|
end
|
71
76
|
end
|
77
|
+
|
78
|
+
private
|
79
|
+
def dsl_accessor_auto_declared?
|
80
|
+
!!@dsl_accessor_auto_declared
|
81
|
+
end
|
82
|
+
|
83
|
+
def method_missing(*args, &block)
|
84
|
+
if dsl_accessor_auto_declared? and args.size == 1 and block
|
85
|
+
define_method(*args, &block)
|
86
|
+
else
|
87
|
+
super
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
72
91
|
end
|
73
92
|
|
93
|
+
class Class
|
94
|
+
include DslAccessor
|
95
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), "spec_helper" )
|
2
|
+
|
3
|
+
describe DslAccessor do
|
4
|
+
before(:each) do
|
5
|
+
Object.send(:remove_const, :Foo) if Object.const_defined?(:Foo)
|
6
|
+
class Foo
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def foo_should_not_be_defined
|
11
|
+
Foo .should_not respond_to(:foo)
|
12
|
+
Foo.new.should_not respond_to(:foo)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should provide .dsl_accessor_auto_declared as private method" do
|
16
|
+
Class.private_methods.should include("dsl_accessor_auto_declared?")
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should provide .dsl_accessor" do
|
20
|
+
Class.should respond_to(:dsl_accessor)
|
21
|
+
end
|
22
|
+
|
23
|
+
describe " should raise NoMethodError when we call a method " do
|
24
|
+
it "without arugments" do
|
25
|
+
foo_should_not_be_defined
|
26
|
+
lambda { Foo.foo }.should raise_error(NoMethodError)
|
27
|
+
foo_should_not_be_defined
|
28
|
+
end
|
29
|
+
it "with arguments" do
|
30
|
+
foo_should_not_be_defined
|
31
|
+
lambda { Foo.foo(1) }.should raise_error(NoMethodError)
|
32
|
+
lambda { Foo.foo(1, 2) }.should raise_error(NoMethodError)
|
33
|
+
foo_should_not_be_defined
|
34
|
+
end
|
35
|
+
it "with block" do
|
36
|
+
foo_should_not_be_defined
|
37
|
+
lambda { Foo.foo{} }.should raise_error(NoMethodError)
|
38
|
+
foo_should_not_be_defined
|
39
|
+
end
|
40
|
+
it "with arguments and block" do
|
41
|
+
foo_should_not_be_defined
|
42
|
+
lambda { Foo.foo(1){} }.should raise_error(NoMethodError)
|
43
|
+
foo_should_not_be_defined
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe ".dsl_accessor without arguments" do
|
48
|
+
it "should be accepted" do
|
49
|
+
lambda {
|
50
|
+
Foo.dsl_accessor
|
51
|
+
}.should_not raise_error
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should mark auto_declared" do
|
55
|
+
Foo.send(:dsl_accessor_auto_declared?).should be_false
|
56
|
+
Foo.dsl_accessor
|
57
|
+
Foo.send(:dsl_accessor_auto_declared?).should be_true
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "auto_declared" do
|
62
|
+
before(:each) do
|
63
|
+
class Foo
|
64
|
+
dsl_accessor
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should raise NoMethodError if unknown method with args is called" do
|
69
|
+
lambda {
|
70
|
+
Foo.foo(1)
|
71
|
+
}.should raise_error(NoMethodError)
|
72
|
+
end
|
73
|
+
|
74
|
+
it "should raise NoMethodError when unknown method is called with args and block" do
|
75
|
+
lambda {
|
76
|
+
Foo.foo(1){}
|
77
|
+
}.should raise_error(NoMethodError)
|
78
|
+
end
|
79
|
+
|
80
|
+
describe " when unknown method is called with a block" do
|
81
|
+
it "should trap NoMethodError" do
|
82
|
+
lambda {
|
83
|
+
Foo.foo{}
|
84
|
+
}.should_not raise_error(NoMethodError)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should define its instance method automatically" do
|
88
|
+
foo_should_not_be_defined
|
89
|
+
Foo.foo{1}
|
90
|
+
Foo.new.should respond_to(:foo)
|
91
|
+
Foo.new.foo.should == 1
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should affect nothing to subclasses" do
|
96
|
+
class Bar < Foo
|
97
|
+
end
|
98
|
+
|
99
|
+
lambda { Bar.bar }.should raise_error(NoMethodError)
|
100
|
+
lambda { Bar.bar(1) }.should raise_error(NoMethodError)
|
101
|
+
lambda { Bar.bar(1, 2) }.should raise_error(NoMethodError)
|
102
|
+
lambda { Bar.bar{} }.should raise_error(NoMethodError)
|
103
|
+
lambda { Bar.bar(1){} }.should raise_error(NoMethodError)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: maiha-dsl_accessor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- maiha
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-01-24 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -19,30 +19,33 @@ dependencies:
|
|
19
19
|
requirements:
|
20
20
|
- - ">="
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version:
|
22
|
+
version: 2.0.0
|
23
23
|
version:
|
24
|
-
description:
|
24
|
+
description: This plugin gives hybrid accessor class methods to classes by DSL like definition
|
25
25
|
email: maiha@wota.jp
|
26
26
|
executables: []
|
27
27
|
|
28
28
|
extensions: []
|
29
29
|
|
30
|
-
extra_rdoc_files:
|
31
|
-
|
30
|
+
extra_rdoc_files:
|
31
|
+
- README
|
32
|
+
- LICENSE
|
32
33
|
files:
|
34
|
+
- LICENSE
|
33
35
|
- README
|
34
36
|
- Rakefile
|
37
|
+
- core_ext/class
|
35
38
|
- core_ext/class/dsl_accessor.rb
|
36
|
-
- init.rb
|
37
|
-
- install.rb
|
38
39
|
- lib/dsl_accessor.rb
|
40
|
+
- spec/auto_declared_spec.rb
|
41
|
+
- spec/spec_helper.rb
|
39
42
|
- tasks/dsl_accessor_tasks.rake
|
43
|
+
- test/instance_test.rb
|
40
44
|
- test/default_test.rb
|
45
|
+
- test/writer_test.rb
|
46
|
+
- test/test_helper.rb
|
41
47
|
- test/dsl_accessor_test.rb
|
42
48
|
- test/instance_options_test.rb
|
43
|
-
- test/instance_test.rb
|
44
|
-
- test/test_helper.rb
|
45
|
-
- test/writer_test.rb
|
46
49
|
has_rdoc: true
|
47
50
|
homepage: http://github.com/maiha/dsl_accessor
|
48
51
|
post_install_message:
|
@@ -65,9 +68,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
68
|
requirements: []
|
66
69
|
|
67
70
|
rubyforge_project:
|
68
|
-
rubygems_version: 1.0
|
71
|
+
rubygems_version: 1.2.0
|
69
72
|
signing_key:
|
70
73
|
specification_version: 2
|
71
|
-
summary:
|
74
|
+
summary: This plugin gives hybrid accessor class methods to classes by DSL like definition
|
72
75
|
test_files: []
|
73
76
|
|
data/init.rb
DELETED
data/install.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
# Install hook code here
|