dsl_accessor 0.4.0 → 0.4.1
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/Gemfile +3 -0
- data/README +161 -127
- data/Rakefile +1 -52
- data/dsl_accessor.gemspec +25 -0
- data/lib/dsl_accessor.rb +2 -0
- data/lib/dsl_accessor/accessor.rb +36 -12
- data/lib/dsl_accessor/auto_declare.rb +41 -0
- data/lib/dsl_accessor/version.rb +4 -0
- data/spec/accessor_spec.rb +0 -2
- data/spec/auto_declared_spec.rb +123 -0
- data/spec/setter_spec.rb +24 -0
- data/spec/spec_helper.rb +1 -1
- data/tasks/dsl_accessor_tasks.rake +4 -0
- metadata +60 -16
data/Gemfile
ADDED
data/README
CHANGED
@@ -1,172 +1,206 @@
|
|
1
1
|
DslAccessor
|
2
2
|
===========
|
3
3
|
|
4
|
-
This plugin gives hybrid accessor class methods to classes by DSL like definition,
|
5
|
-
here hybrid means getter and setter. The accessor method acts as getter method
|
6
|
-
if no argments given, otherwise it acts as setter one with the arguments.
|
4
|
+
This plugin gives hybrid accessor class methods to classes by DSL like definition,
|
5
|
+
here hybrid means getter and setter. The accessor method acts as getter method
|
6
|
+
if no argments given, otherwise it acts as setter one with the arguments.
|
7
|
+
|
8
|
+
|
9
|
+
Install
|
10
|
+
=======
|
11
|
+
|
12
|
+
gem install dsl_accessor
|
7
13
|
|
8
14
|
|
9
15
|
Usage
|
10
16
|
=====
|
11
17
|
|
12
|
-
|
13
|
-
|
14
|
-
|
18
|
+
class Foo
|
19
|
+
dsl_accessor "<METHOD NAME>" (, default_value)
|
20
|
+
end
|
15
21
|
|
16
22
|
|
17
23
|
Example
|
18
24
|
=======
|
19
25
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
This code gives 'greeting' class method to Foo class.
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
26
|
+
class Foo
|
27
|
+
dsl_accessor :greeting
|
28
|
+
end
|
29
|
+
|
30
|
+
This code gives 'greeting' class method to Foo class.
|
31
|
+
|
32
|
+
Foo.greeting # means getter, and the default value is nil.
|
33
|
+
=> nil
|
34
|
+
|
35
|
+
Foo.greeting "I'm Foo." # means setter with given arguments
|
36
|
+
=> "I'm Foo."
|
37
|
+
|
38
|
+
Foo.greeting
|
39
|
+
=> "I'm Foo."
|
34
40
|
|
35
41
|
|
36
42
|
Difference
|
37
43
|
==========
|
38
44
|
|
39
|
-
I
|
40
|
-
Although the difference is just whether we needs '=' operation or not,
|
41
|
-
it makes a large different on class definition especially subclass.
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
We must write redundant code represented by "self." to distinguish
|
52
|
-
a local variable and a class method when we use 'cattr_accessor'.
|
53
|
-
This is ugly and boring work.
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
There are no longer redundant prefix code like "self." and "set_".
|
64
|
-
|
45
|
+
I am convinced that you want to propose me to use 'cattr_accessor'.
|
46
|
+
Although the difference is just whether we needs '=' operation or not,
|
47
|
+
it makes a large different on class definition especially subclass.
|
48
|
+
|
49
|
+
class Foo
|
50
|
+
cattr_accessor :greeting
|
51
|
+
end
|
52
|
+
|
53
|
+
class Bar < Foo
|
54
|
+
self.greeting = "I am bar."
|
55
|
+
end
|
56
|
+
|
57
|
+
We must write redundant code represented by "self." to distinguish
|
58
|
+
a local variable and a class method when we use 'cattr_accessor'.
|
59
|
+
This is ugly and boring work.
|
60
|
+
|
61
|
+
class Foo
|
62
|
+
dsl_accessor :greeting
|
63
|
+
end
|
64
|
+
|
65
|
+
class Bar < Foo
|
66
|
+
greeting "I am bar."
|
67
|
+
end
|
68
|
+
|
69
|
+
There are no longer redundant prefix code like "self." and "set_".
|
70
|
+
How about this dsl-like coding with simple declaration?
|
65
71
|
|
66
72
|
|
67
73
|
Special Options
|
68
74
|
===============
|
69
75
|
|
70
|
-
'dsl_accessor' method can take two options, those are :writer and :default.
|
71
|
-
"writer" option means callback method used when setter is executed.
|
72
|
-
"default" option means default static value or proc that creates some value.
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
Note that "User.primary_key" return a String by setter proc.
|
76
|
+
'dsl_accessor' method can take two options, those are :writer and :default.
|
77
|
+
"writer" option means callback method used when setter is executed.
|
78
|
+
"default" option means default static value or proc that creates some value.
|
79
|
+
|
80
|
+
class PseudoAR
|
81
|
+
dsl_accessor :primary_key, :default=>"id", :writer=>proc{|value| value.to_s}
|
82
|
+
dsl_accessor :table_name, :default=>proc{|klass| klass.name.demodulize.underscore.pluralize}
|
83
|
+
end
|
84
|
+
|
85
|
+
class Item < PseudoAR
|
86
|
+
end
|
87
|
+
|
88
|
+
class User < PseudoAR
|
89
|
+
primary_key :user_code
|
90
|
+
table_name :user_table
|
91
|
+
end
|
92
|
+
|
93
|
+
Item.primary_key # => "id"
|
94
|
+
Item.table_name # => "items"
|
95
|
+
User.primary_key # => "user_code"
|
96
|
+
User.table_name # => :user_table
|
97
|
+
|
98
|
+
Note that "User.primary_key" return a String by setter proc.
|
93
99
|
|
94
100
|
|
95
101
|
Instance Method
|
96
102
|
===============
|
97
103
|
|
98
|
-
"instance" option automatically defines its instance method
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
end
|
103
|
-
|
104
|
-
Search.url # => "http://localhost/"
|
105
|
-
Search.new.url # => "http://localhost/"
|
106
|
-
|
107
|
-
and it uses @options instance variable with special value :options
|
108
|
-
|
109
|
-
class Window
|
110
|
-
dsl_accessor :width, :default=>640, :instance=>:options
|
111
|
-
def initialize(options = {})
|
112
|
-
@options = options
|
104
|
+
"instance" option automatically defines its instance method
|
105
|
+
|
106
|
+
class Search
|
107
|
+
dsl_accessor :url, :instance=>true, :default=>"http://localhost/"
|
113
108
|
end
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
109
|
+
|
110
|
+
Search.url # => "http://localhost/"
|
111
|
+
Search.new.url # => "http://localhost/"
|
112
|
+
|
113
|
+
and it uses @options instance variable with special value :options
|
114
|
+
|
115
|
+
class Window
|
116
|
+
dsl_accessor :width, :default=>640, :instance=>:options
|
117
|
+
def initialize(options = {})
|
118
|
+
@options = options
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
Window.width # => 640
|
123
|
+
Window.new.width # => 640
|
124
|
+
|
125
|
+
window = Window.new(:width=>320)
|
126
|
+
window.width # =>320
|
127
127
|
|
128
128
|
|
129
129
|
Auto declared mode
|
130
130
|
==================
|
131
131
|
|
132
|
-
|
133
|
-
In
|
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
|
132
|
+
It was removed at version 0.4.
|
133
|
+
In 0.4.1 or higher, use dsl_accessor block instead.
|
142
134
|
|
143
|
-
Foo.new.foo # => 1
|
144
135
|
|
145
|
-
|
136
|
+
with block
|
137
|
+
==========
|
146
138
|
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
139
|
+
dsl_accessor method accepts block for auto declared mode.
|
140
|
+
In this mode, we can define methods like dsl.
|
141
|
+
|
142
|
+
[NOTE]
|
143
|
+
1. This affects only methods with a block and no other args.
|
144
|
+
|
145
|
+
class Foo
|
146
|
+
dsl_accessor do
|
147
|
+
foo {1} # Foo.foo is defined
|
148
|
+
bar(a) # NoMethodError
|
149
|
+
baz(a) {2} # NoMethodError
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
2. When :instance is passed with block, it affects instance methods.
|
154
|
+
|
155
|
+
class Foo
|
156
|
+
dsl_accessor :instance do
|
157
|
+
foo {1} # Foo#foo is defined
|
158
|
+
bar(a) # NoMethodError (same as class)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
3. This will damage on your class cause it easily updates existing methods.
|
163
|
+
|
164
|
+
Foo.name # => 'Foo'
|
165
|
+
class Foo
|
166
|
+
dsl_accessor do
|
167
|
+
name {1}
|
168
|
+
end
|
169
|
+
end
|
170
|
+
Foo.name # => 1
|
171
|
+
|
172
|
+
|
173
|
+
Although there is a risk on above, it helps you when many one-lined methods exist.
|
174
|
+
|
175
|
+
class Foo
|
176
|
+
def last
|
177
|
+
num_pages
|
178
|
+
end
|
179
|
+
|
180
|
+
def first?
|
181
|
+
page == 1
|
182
|
+
end
|
183
|
+
|
184
|
+
def offset
|
185
|
+
model.proxy_options[:offset]
|
186
|
+
end
|
151
187
|
end
|
152
|
-
|
153
|
-
|
154
|
-
|
188
|
+
|
189
|
+
Refactored with dsl_accessor
|
190
|
+
|
191
|
+
class Foo
|
192
|
+
dsl_accessor :instance do
|
193
|
+
last {num_pages}
|
194
|
+
first? {page == 1}
|
195
|
+
offset {model.proxy_options[:offset]}
|
196
|
+
end
|
155
197
|
end
|
156
198
|
|
157
|
-
def offset
|
158
|
-
model.proxy_options[:offset]
|
159
|
-
end
|
160
|
-
end
|
161
199
|
|
162
|
-
|
163
|
-
|
164
|
-
dsl_accessor
|
165
|
-
last {num_pages}
|
166
|
-
first? {page == 1}
|
167
|
-
offset {model.proxy_options[:offset]}
|
168
|
-
end
|
200
|
+
Homepage
|
201
|
+
========
|
169
202
|
|
203
|
+
http://github.com/maiha/dsl_accessor
|
170
204
|
|
171
205
|
|
172
206
|
Author
|
data/Rakefile
CHANGED
@@ -1,52 +1 @@
|
|
1
|
-
require
|
2
|
-
require 'rake/gempackagetask'
|
3
|
-
|
4
|
-
GEM_NAME = "dsl_accessor"
|
5
|
-
AUTHOR = "maiha"
|
6
|
-
EMAIL = "maiha@wota.jp"
|
7
|
-
HOMEPAGE = "http://github.com/maiha/dsl_accessor"
|
8
|
-
SUMMARY = "This plugin gives hybrid accessor class methods to classes by DSL like definition"
|
9
|
-
GEM_VERSION = "0.4.0"
|
10
|
-
|
11
|
-
spec = Gem::Specification.new do |s|
|
12
|
-
s.rubyforge_project = 'asakusarb'
|
13
|
-
s.executables = []
|
14
|
-
s.name = GEM_NAME
|
15
|
-
s.version = GEM_VERSION
|
16
|
-
s.platform = Gem::Platform::RUBY
|
17
|
-
s.has_rdoc = true
|
18
|
-
s.extra_rdoc_files = ["README", "MIT-LICENSE"]
|
19
|
-
s.summary = SUMMARY
|
20
|
-
s.description = s.summary
|
21
|
-
s.author = AUTHOR
|
22
|
-
s.email = EMAIL
|
23
|
-
s.homepage = HOMEPAGE
|
24
|
-
s.require_path = 'lib'
|
25
|
-
s.add_dependency('optionize', '>= 0.1.0')
|
26
|
-
s.files = %w(MIT-LICENSE README Rakefile) + Dir.glob("{lib,spec,core_ext}/**/*")
|
27
|
-
end
|
28
|
-
|
29
|
-
Rake::GemPackageTask.new(spec) do |pkg|
|
30
|
-
pkg.gem_spec = spec
|
31
|
-
end
|
32
|
-
|
33
|
-
desc "Install the gem"
|
34
|
-
task :install do
|
35
|
-
Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
|
36
|
-
end
|
37
|
-
|
38
|
-
desc "Uninstall the gem"
|
39
|
-
task :uninstall do
|
40
|
-
Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
|
41
|
-
end
|
42
|
-
|
43
|
-
desc "Create a gemspec file"
|
44
|
-
task :gemspec do
|
45
|
-
File.open("#{GEM_NAME}.gemspec", "w") do |file|
|
46
|
-
file.puts spec.to_ruby
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
require 'spec/rake/spectask'
|
51
|
-
desc 'Default: run spec examples'
|
52
|
-
task :default => 'spec'
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "dsl_accessor/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "dsl_accessor"
|
7
|
+
s.version = DslAccessor::VERSION
|
8
|
+
s.authors = ["maiha"]
|
9
|
+
s.email = ["maiha@wota.jp"]
|
10
|
+
s.homepage = "https://github.com/maiha/dsl_accessor"
|
11
|
+
s.summary = %q{This plugin gives hybrid accessor class methods to classes by DSL like definition}
|
12
|
+
s.description = %q{This plugin gives hybrid accessor class methods to classes by DSL like definition}
|
13
|
+
|
14
|
+
s.rubyforge_project = "dsl_accessor"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "optionize", ">= 0.1.0"
|
22
|
+
s.add_dependency "blankslate", ">= 2.1.2"
|
23
|
+
|
24
|
+
s.add_development_dependency "rspec"
|
25
|
+
end
|
data/lib/dsl_accessor.rb
CHANGED
@@ -2,6 +2,8 @@ unless Module.new.respond_to?(:delegate)
|
|
2
2
|
require File.dirname(__FILE__) + "/../core_ext/module/delegation"
|
3
3
|
end
|
4
4
|
|
5
|
+
require File.dirname(__FILE__) + '/dsl_accessor/version'
|
6
|
+
require File.dirname(__FILE__) + '/dsl_accessor/auto_declare'
|
5
7
|
require File.dirname(__FILE__) + '/dsl_accessor/accessor'
|
6
8
|
require File.dirname(__FILE__) + '/dsl_accessor/stores'
|
7
9
|
|
@@ -1,25 +1,37 @@
|
|
1
1
|
require 'optionize'
|
2
2
|
|
3
3
|
module DslAccessor
|
4
|
-
def dsl_accessor_reader(key, *args)
|
4
|
+
def dsl_accessor_reader(key, *args, &block)
|
5
5
|
key = key.to_s
|
6
|
-
if args.empty?
|
6
|
+
if !args.empty? or block_given?
|
7
|
+
# setter method
|
8
|
+
dsl_accessor_writer(key, *args, &block)
|
9
|
+
else
|
7
10
|
# getter method
|
8
11
|
if !dsl_accessor_key?(key)
|
12
|
+
# load default value
|
9
13
|
default = dsl_accessor_get("#{key}_default")
|
10
14
|
value = default ? default.call : nil
|
11
15
|
dsl_accessor_writer(key, value)
|
12
16
|
end
|
13
17
|
dsl_accessor_get(key)
|
14
|
-
else
|
15
|
-
# setter method
|
16
|
-
dsl_accessor_writer(key, *args)
|
17
18
|
end
|
18
19
|
end
|
19
20
|
|
20
|
-
def dsl_accessor_writer(key, *args)
|
21
|
+
def dsl_accessor_writer(key, *args, &block)
|
21
22
|
case args.size
|
23
|
+
when 0
|
24
|
+
unless block_given?
|
25
|
+
raise ArgumentError, "'#{key}=' expected one argument or block, but nothing passed"
|
26
|
+
end
|
27
|
+
writer = dsl_accessor_get("#{key}_writer")
|
28
|
+
value = writer ? writer.call(block) : block
|
29
|
+
dsl_accessor_set("#{key}", value)
|
22
30
|
when 1
|
31
|
+
if block_given?
|
32
|
+
raise ArgumentError, "'#{key}=' got both arg and block, specify only one of them"
|
33
|
+
end
|
34
|
+
|
23
35
|
writer = dsl_accessor_get("#{key}_writer")
|
24
36
|
value = writer ? writer.call(*args) : args.first
|
25
37
|
dsl_accessor_set("#{key}", value)
|
@@ -32,8 +44,20 @@ module DslAccessor
|
|
32
44
|
opts = Optionize.new(args, :name, :default)
|
33
45
|
name = opts.name
|
34
46
|
|
35
|
-
if
|
36
|
-
|
47
|
+
if block
|
48
|
+
case name
|
49
|
+
when :class, NilClass
|
50
|
+
AutoDeclare::DefineClassMethod.new(self, &block)
|
51
|
+
when :instance
|
52
|
+
AutoDeclare::DefineInstanceMethod.new(self, &block)
|
53
|
+
else
|
54
|
+
raise ArgumentError, "dsl_accessor block expects :class or :instance for arg, but got #{name.inspect}"
|
55
|
+
end
|
56
|
+
return
|
57
|
+
end
|
58
|
+
|
59
|
+
if !name
|
60
|
+
raise ArgumentError, "dsl_accessor expects at least one arg"
|
37
61
|
end
|
38
62
|
|
39
63
|
writer =
|
@@ -82,11 +106,11 @@ module DslAccessor
|
|
82
106
|
end
|
83
107
|
|
84
108
|
instance_eval <<-EOS
|
85
|
-
def #{name}(*args)
|
86
|
-
dsl_accessor_reader("#{name}", *args)
|
109
|
+
def #{name}(*args, &block)
|
110
|
+
dsl_accessor_reader("#{name}", *args, &block)
|
87
111
|
end
|
88
|
-
def #{name}=(*args)
|
89
|
-
dsl_accessor_writer("#{name}", *args)
|
112
|
+
def #{name}=(*args, &block)
|
113
|
+
dsl_accessor_writer("#{name}", *args, &block)
|
90
114
|
end
|
91
115
|
EOS
|
92
116
|
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'blankslate'
|
2
|
+
|
3
|
+
module DslAccessor
|
4
|
+
module AutoDeclare
|
5
|
+
class DefineClassMethod < BlankSlate
|
6
|
+
def initialize(context, &block)
|
7
|
+
@context = context
|
8
|
+
instance_eval(&block)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
def method_missing(name, *args, &block)
|
13
|
+
if args.empty? and block
|
14
|
+
meta_class = (class << @context; self; end)
|
15
|
+
meta_class.class_eval{ define_method(name, &block) }
|
16
|
+
else
|
17
|
+
@context.__send__(name, *args, &block)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class DefineInstanceMethod < BlankSlate
|
23
|
+
def initialize(klass, &block)
|
24
|
+
@klass = klass
|
25
|
+
instance_eval(&block)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
def method_missing(name, *args, &block)
|
30
|
+
if args.empty? and block
|
31
|
+
@klass.class_eval{ define_method(name, &block) }
|
32
|
+
else
|
33
|
+
raise NameError, "undefined local variable or method `#{name}'"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
__END__
|
data/spec/accessor_spec.rb
CHANGED
@@ -6,7 +6,6 @@ describe DslAccessor do
|
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
|
-
|
10
9
|
describe "dsl_accessor(:foo)" do
|
11
10
|
before do
|
12
11
|
@klass = new_class { dsl_accessor :foo }
|
@@ -42,7 +41,6 @@ describe "dsl_accessor(:foo)" do
|
|
42
41
|
end
|
43
42
|
end
|
44
43
|
|
45
|
-
|
46
44
|
describe "dsl_accessor(:foo, 1)" do
|
47
45
|
before do
|
48
46
|
@klass = new_class { dsl_accessor :foo, 1 }
|
@@ -0,0 +1,123 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), "spec_helper" )
|
2
|
+
|
3
|
+
describe DslAccessor do
|
4
|
+
before do
|
5
|
+
Object.send(:remove_const, :Foo) if Object.const_defined?(:Foo)
|
6
|
+
Foo = Class.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def dsl_accessor(*args, &block)
|
10
|
+
Foo.dsl_accessor(*args, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
######################################################################
|
14
|
+
### Class Methods
|
15
|
+
|
16
|
+
describe "dsl_accessor(&block)" do
|
17
|
+
context " should raise NameError when" do
|
18
|
+
def dsl_accessor(*args, &block)
|
19
|
+
lambda { super }.should raise_error(NameError)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "foo" do
|
23
|
+
dsl_accessor { foo }
|
24
|
+
end
|
25
|
+
|
26
|
+
it "foo(1)" do
|
27
|
+
dsl_accessor { foo(1) }
|
28
|
+
end
|
29
|
+
|
30
|
+
it "foo(1,2)" do
|
31
|
+
dsl_accessor { foo(1,2) }
|
32
|
+
end
|
33
|
+
|
34
|
+
it "foo(1) {}" do
|
35
|
+
dsl_accessor { foo(1) {} }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context " should define class method 'foo' when" do
|
40
|
+
def dsl_accessor(*args, &block)
|
41
|
+
super
|
42
|
+
Foo.should respond_to(:foo)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "foo {}" do
|
46
|
+
dsl_accessor { foo {} }
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should overwrite existing class methods such as 'name'" do
|
51
|
+
Foo.dsl_accessor {
|
52
|
+
name { 1 }
|
53
|
+
}
|
54
|
+
Foo.name.should == 1
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should invoke the method in valid context" do
|
58
|
+
Foo.should_receive(:bar) { 2 }
|
59
|
+
dsl_accessor { foo { bar } }
|
60
|
+
Foo.foo.should == 2
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
######################################################################
|
65
|
+
### Instance Methods
|
66
|
+
|
67
|
+
describe "dsl_accessor(:instance, &block)" do
|
68
|
+
context " should raise NameError when" do
|
69
|
+
def dsl_accessor(*args, &block)
|
70
|
+
lambda { super }.should raise_error(NameError)
|
71
|
+
end
|
72
|
+
|
73
|
+
it "foo" do
|
74
|
+
dsl_accessor(:instance) { foo }
|
75
|
+
end
|
76
|
+
|
77
|
+
it "foo(1)" do
|
78
|
+
dsl_accessor(:instance) { foo(1) }
|
79
|
+
end
|
80
|
+
|
81
|
+
it "foo(1,2)" do
|
82
|
+
dsl_accessor(:instance) { foo(1,2) }
|
83
|
+
end
|
84
|
+
|
85
|
+
it "foo(1) {}" do
|
86
|
+
dsl_accessor(:instance) { foo(1) {} }
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
context " should define instance method 'foo' when" do
|
91
|
+
def dsl_accessor(*args, &block)
|
92
|
+
super
|
93
|
+
Foo.new.should respond_to(:foo)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "foo {}" do
|
97
|
+
dsl_accessor(:instance) { foo {} }
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should define instance method" do
|
102
|
+
Foo.dsl_accessor(:instance) {
|
103
|
+
foo { 'xxx' }
|
104
|
+
}
|
105
|
+
Foo.new.foo.should == 'xxx'
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should orverwrite existed instance methods even if those are important like 'id'" do
|
109
|
+
Foo.new.object_id.should be_kind_of(Integer)
|
110
|
+
Foo.dsl_accessor(:instance) {
|
111
|
+
id { 'xxx' }
|
112
|
+
}
|
113
|
+
Foo.new.id.should == 'xxx'
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should invoke the method in valid context" do
|
117
|
+
Foo.any_instance.should_receive(:bar) { 2 }
|
118
|
+
dsl_accessor(:instance) { foo { bar } }
|
119
|
+
Foo.new.foo.should == 2
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
data/spec/setter_spec.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), "spec_helper" )
|
2
|
+
|
3
|
+
describe "dsl_accessor :foo" do
|
4
|
+
before do
|
5
|
+
@klass = new_class { dsl_accessor :foo }
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should accept foo(1)" do
|
9
|
+
@klass.foo 1
|
10
|
+
@klass.foo.should == 1
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should reject foo(1, &block)" do
|
14
|
+
lambda {
|
15
|
+
@klass.foo(2) { 3 }
|
16
|
+
}.should raise_error(ArgumentError)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should accept foo(&block)" do
|
20
|
+
@klass.foo { 4 }
|
21
|
+
@klass.foo.should be_kind_of(Proc)
|
22
|
+
@klass.foo.call.should == 4
|
23
|
+
end
|
24
|
+
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dsl_accessor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 13
|
4
5
|
prerelease: false
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 4
|
8
|
-
-
|
9
|
-
version: 0.4.
|
9
|
+
- 1
|
10
|
+
version: 0.4.1
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- maiha
|
@@ -14,16 +15,18 @@ autorequire:
|
|
14
15
|
bindir: bin
|
15
16
|
cert_chain: []
|
16
17
|
|
17
|
-
date:
|
18
|
+
date: 2012-05-10 00:00:00 +09:00
|
18
19
|
default_executable:
|
19
20
|
dependencies:
|
20
21
|
- !ruby/object:Gem::Dependency
|
21
22
|
name: optionize
|
22
23
|
prerelease: false
|
23
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
24
26
|
requirements:
|
25
27
|
- - ">="
|
26
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 27
|
27
30
|
segments:
|
28
31
|
- 0
|
29
32
|
- 1
|
@@ -31,32 +34,69 @@ dependencies:
|
|
31
34
|
version: 0.1.0
|
32
35
|
type: :runtime
|
33
36
|
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: blankslate
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 15
|
46
|
+
segments:
|
47
|
+
- 2
|
48
|
+
- 1
|
49
|
+
- 2
|
50
|
+
version: 2.1.2
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rspec
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 3
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
type: :development
|
66
|
+
version_requirements: *id003
|
34
67
|
description: This plugin gives hybrid accessor class methods to classes by DSL like definition
|
35
|
-
email:
|
68
|
+
email:
|
69
|
+
- maiha@wota.jp
|
36
70
|
executables: []
|
37
71
|
|
38
72
|
extensions: []
|
39
73
|
|
40
|
-
extra_rdoc_files:
|
41
|
-
|
42
|
-
- MIT-LICENSE
|
74
|
+
extra_rdoc_files: []
|
75
|
+
|
43
76
|
files:
|
77
|
+
- Gemfile
|
44
78
|
- MIT-LICENSE
|
45
79
|
- README
|
46
80
|
- Rakefile
|
47
|
-
-
|
48
|
-
-
|
81
|
+
- core_ext/module/delegation.rb
|
82
|
+
- dsl_accessor.gemspec
|
49
83
|
- lib/dsl_accessor.rb
|
84
|
+
- lib/dsl_accessor/accessor.rb
|
85
|
+
- lib/dsl_accessor/auto_declare.rb
|
86
|
+
- lib/dsl_accessor/stores.rb
|
87
|
+
- lib/dsl_accessor/version.rb
|
50
88
|
- spec/accessor_spec.rb
|
51
|
-
- spec/
|
89
|
+
- spec/auto_declared_spec.rb
|
90
|
+
- spec/default_spec.rb
|
52
91
|
- spec/inherit_spec.rb
|
53
|
-
- spec/module_spec.rb
|
54
92
|
- spec/instance_spec.rb
|
55
|
-
- spec/
|
93
|
+
- spec/module_spec.rb
|
94
|
+
- spec/setter_spec.rb
|
56
95
|
- spec/spec_helper.rb
|
57
|
-
-
|
96
|
+
- spec/writer_spec.rb
|
97
|
+
- tasks/dsl_accessor_tasks.rake
|
58
98
|
has_rdoc: true
|
59
|
-
homepage:
|
99
|
+
homepage: https://github.com/maiha/dsl_accessor
|
60
100
|
licenses: []
|
61
101
|
|
62
102
|
post_install_message:
|
@@ -65,23 +105,27 @@ rdoc_options: []
|
|
65
105
|
require_paths:
|
66
106
|
- lib
|
67
107
|
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
68
109
|
requirements:
|
69
110
|
- - ">="
|
70
111
|
- !ruby/object:Gem::Version
|
112
|
+
hash: 3
|
71
113
|
segments:
|
72
114
|
- 0
|
73
115
|
version: "0"
|
74
116
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
75
118
|
requirements:
|
76
119
|
- - ">="
|
77
120
|
- !ruby/object:Gem::Version
|
121
|
+
hash: 3
|
78
122
|
segments:
|
79
123
|
- 0
|
80
124
|
version: "0"
|
81
125
|
requirements: []
|
82
126
|
|
83
|
-
rubyforge_project:
|
84
|
-
rubygems_version: 1.3.
|
127
|
+
rubyforge_project: dsl_accessor
|
128
|
+
rubygems_version: 1.3.7
|
85
129
|
signing_key:
|
86
130
|
specification_version: 3
|
87
131
|
summary: This plugin gives hybrid accessor class methods to classes by DSL like definition
|