def_dsl 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module DefDsl
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/def_dsl.rb CHANGED
@@ -13,8 +13,10 @@ require "def_dsl/version"
13
13
  module DefDsl
14
14
 
15
15
 
16
- def def_dsl const
17
- define_dsl! self, [[const.name.to_s.underscore(self), const]]
16
+ def def_dsl *const
17
+ const.each do |const|
18
+ define_dsl! self, [[const.name.to_s.underscore(self), const]]
19
+ end
18
20
  end
19
21
  alias define_dsl def_dsl
20
22
 
@@ -32,16 +34,18 @@ module DefDsl
32
34
  who.send :include, So
33
35
  define_dsl! const if const.is_a? Class
34
36
 
37
+ meth = const.instance_eval { @dsl } || mini
38
+
35
39
  who.class_eval do
36
- define_method(mini) do |*a,&b|
37
- so mini,*a,&b
40
+ define_method(meth) do |*a,&b|
41
+ so [mini,meth],*a,&b
38
42
  end
39
43
  end
40
44
 
41
45
  # const.send :include, So if const.is_a? Module # or even class
42
46
  # who.send :include, So if who.is_a? Module # or even class
43
47
  if who.class == Module
44
- who.send :module_function, mini
48
+ who.send :module_function, meth
45
49
  who.send :module_function, :so
46
50
  who.send :module_function, :so1
47
51
  who.send :module_function, :so2
@@ -50,10 +54,18 @@ module DefDsl
50
54
  end
51
55
 
52
56
  module So
57
+ def feed_block &block
58
+ instance_eval &block
59
+ end
60
+
53
61
  def so klass_name=nil,*a,&b
54
62
  return @so if klass_name.nil?
55
63
 
56
- klass_name = klass_name.to_sym
64
+ klass_name, label = if klass_name.is_a? Array
65
+ klass_name.map &:to_sym
66
+ else
67
+ [klass_name.to_sym, klass_name.to_sym]
68
+ end
57
69
 
58
70
  if self.class == Module
59
71
  klass = self.const_get(klass_name.to_s.camelize)
@@ -62,8 +74,9 @@ module DefDsl
62
74
  end
63
75
 
64
76
  obj = klass.new(*a) #,&b)
65
- obj.instance_eval &b if b
66
- (@so ||= {}).tap { |h| h[klass_name] = h[klass_name] ? [h[klass_name], obj].flatten(1) : obj }
77
+ # obj.instance_eval &b if b
78
+ obj.feed_block &b if b
79
+ (@so ||= {}).tap { |h| h[label] = h[label] ? [h[label], obj].flatten(1) : obj }
67
80
  end
68
81
  def so1(name)
69
82
  value = @so[name]
data/spec/exmaple_spec.rb CHANGED
@@ -32,6 +32,7 @@ describe [DefDsl,DefDSL].join' or ' do
32
32
  end
33
33
  end
34
34
 
35
+
35
36
  example 'usage inside object' do
36
37
  class My
37
38
 
@@ -42,8 +43,7 @@ describe [DefDsl,DefDSL].join' or ' do
42
43
  end
43
44
 
44
45
  extend DefDSL
45
- def_dsl Dir
46
- def_dsl File
46
+ def_dsl Dir, File
47
47
 
48
48
  def initialize
49
49
  file 'root.txt'
@@ -65,4 +65,76 @@ describe [DefDsl,DefDSL].join' or ' do
65
65
  end
66
66
  My.new
67
67
  end
68
+
69
+
70
+ example 'explicit dsl method name definition' do
71
+ module My
72
+ # nice looking recursive dsl definition
73
+ class AFile < Struct.new :name; @dsl = :file end
74
+ class ADir < Struct.new :name
75
+ @dsl = :dir
76
+ ADir = ADir
77
+ AFile = AFile
78
+ end
79
+
80
+ extend DefDSL
81
+ def_dsl ADir, AFile
82
+
83
+ file 'root.txt'
84
+ dir 'dir' do
85
+ dir 'inner' do
86
+ dir 'empty'
87
+ file 'any'
88
+ end
89
+ end
90
+
91
+ so.should == {file: so1(:file), dir: so1(:dir)}
92
+
93
+ so1(:file).name.should == 'root.txt'
94
+ so1(:dir).name.should == 'dir'
95
+ (inner = so1(:dir).send(:so1,:dir)).name.should == 'inner'
96
+ inner.send(:so2,:dir).map(&:name).should == ['empty']
97
+ inner.send(:so2,:file).map(&:name).should == ['any']
98
+ end
99
+ end
100
+
101
+
102
+ example 'redefine feed_block' do
103
+ module My
104
+ # nice looking recursive dsl definition
105
+ class File < Struct.new :name
106
+ def feed_block &block
107
+ @block = block
108
+ end
109
+ end
110
+ class Dir < Struct.new :name
111
+ Dir = Dir
112
+ File = File
113
+ end
114
+
115
+ extend DefDSL
116
+ def_dsl Dir, File
117
+
118
+ file 'root.txt' do
119
+ this.block.should_be just stored.in_ivar
120
+ end
121
+ dir 'dir' do
122
+ dir 'inner' do
123
+ dir 'empty'
124
+ file 'any'
125
+ end
126
+ end
127
+
128
+ so.should == {file: so1(:file), dir: so1(:dir)}
129
+
130
+ so1(:file).name.should == 'root.txt'
131
+ so1(:dir).name.should == 'dir'
132
+ (inner = so1(:dir).send(:so1,:dir)).name.should == 'inner'
133
+ inner.send(:so2,:dir).map(&:name).should == ['empty']
134
+ inner.send(:so2,:file).map(&:name).should == ['any']
135
+
136
+ so1(:file).instance_eval { @block }.nil?.should == false
137
+ end
138
+ end
139
+
68
140
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: def_dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
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: 2013-04-18 00:00:00.000000000 Z
12
+ date: 2013-04-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -94,7 +94,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
94
94
  version: '0'
95
95
  segments:
96
96
  - 0
97
- hash: -3030536010394505708
97
+ hash: -3785499367159931257
98
98
  required_rubygems_version: !ruby/object:Gem::Requirement
99
99
  none: false
100
100
  requirements:
@@ -103,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  version: '0'
104
104
  segments:
105
105
  - 0
106
- hash: -3030536010394505708
106
+ hash: -3785499367159931257
107
107
  requirements: []
108
108
  rubyforge_project:
109
109
  rubygems_version: 1.8.25