dslable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,94 @@
1
+ # encoding: utf-8
2
+ require "generators/generators"
3
+ require "erb"
4
+ require 'active_support/inflector'
5
+ require "dslable_dsl"
6
+
7
+ module Dslable::Generators::ProductCodes
8
+ class Dsl
9
+ DSL_TEMPLATE =<<-EOF
10
+ # encoding: utf-8
11
+ require '<%=gem_name%>_dsl_model'
12
+
13
+ module <%=gem_name_camel%>
14
+ class Dsl
15
+ attr_accessor :<%=gem_name%>
16
+
17
+ # String Define
18
+ [<%=string_fields%>].each do |f|
19
+ define_method f do |value|
20
+ eval "@<%=gem_name%>.#\{f.to_s} = '#\{value}'", binding
21
+ end
22
+ end
23
+
24
+ # Array/Hash Define
25
+ [<%=array_hash_fields%>].each do |f|
26
+ define_method f do |value|
27
+ eval "@<%=gem_name%>.#\{f.to_s} = #\{value}", binding
28
+ end
29
+ end
30
+
31
+ def initialize
32
+ @<%=gem_name%> = <%=gem_name_camel%>::DslModel.new
33
+ <%=set_defaults%>
34
+ end
35
+ end
36
+ end
37
+ EOF
38
+
39
+ attr_accessor :dsl
40
+
41
+ #== initialize dsl
42
+ #=== Params
43
+ #- _dsl: input from dsl
44
+ def initialize(_dsl)
45
+ raise InvalidDslError.new("dsl not allow nil") if _dsl.nil?
46
+ @dsl = _dsl
47
+ end
48
+
49
+ def generate
50
+ dsl_src = adapt_template(@dsl.camelized_gem_name, get_string_fields, get_array_hash_fields, get_set_defaults)
51
+ generate_dsl_src dsl_src
52
+ end
53
+
54
+ private
55
+ def get_string_fields
56
+ fields = []
57
+ @dsl.fields.each do |field|
58
+ fields << ":#{field._field_name}" if field._args._klass == String
59
+ end
60
+ fields.join(", ")
61
+ end
62
+
63
+ def get_array_hash_fields
64
+ fields = []
65
+ @dsl.fields.each do |field|
66
+ fields << ":#{field._field_name}" unless field._args._klass == String
67
+ end
68
+ fields.join(", ")
69
+ end
70
+
71
+ def get_set_defaults
72
+ set_defaults = []
73
+ gem_name = @dsl._gem_name
74
+ @dsl.fields.each_with_index do |field, index|
75
+ next if field._args._default_value.nil?
76
+ default_value = field._args._klass == String ? "'#{field._args._default_value}'" : field._args._default_value
77
+ set_defaults << " @#{gem_name}.#{field._field_name} = #{default_value}"
78
+ end
79
+ set_defaults.join("\n")
80
+ end
81
+
82
+ def adapt_template(gem_name_camel, string_fields, array_hash_fields, set_defaults)
83
+ gem_name = @dsl._gem_name
84
+ erb = ERB.new(DSL_TEMPLATE)
85
+ erb.result(binding)
86
+ end
87
+
88
+ def generate_dsl_src(dsl_src)
89
+ File.open("./lib/#{@dsl._gem_name}_dsl.rb", "w") {|f|f.puts dsl_src}
90
+ end
91
+ end
92
+
93
+ class InvalidDslError < StandardError;end
94
+ end
@@ -0,0 +1,63 @@
1
+ # encoding: utf-8
2
+ require "generators/generators"
3
+ require "erb"
4
+ require 'active_support/inflector'
5
+ require "dslable_dsl"
6
+
7
+ module Dslable::Generators::ProductCodes
8
+ class DslModel
9
+ DSL_MODEL_TEMPLATE =<<-EOF
10
+ # encoding: utf-8
11
+ require 'active_model'
12
+
13
+ module <%=gem_name_camel%>
14
+ class DslModel
15
+ include ActiveModel::Model
16
+
17
+ <%=fields%>
18
+ end
19
+ end
20
+ EOF
21
+
22
+ attr_accessor :dsl
23
+
24
+ #== initialize dsl model
25
+ #=== Params
26
+ #- _dsl: input from dsl
27
+ def initialize(_dsl)
28
+ raise InvalidDslError.new("dsl not allow nil") if _dsl.nil?
29
+ @dsl = _dsl
30
+ end
31
+
32
+ def generate
33
+ dsl_model_src = adapt_template(@dsl.camelized_gem_name, get_fields)
34
+ generate_dsl_model_src dsl_model_src
35
+ end
36
+
37
+ private
38
+ def get_fields
39
+ fields = []
40
+ @dsl.fields.each do |field|
41
+ field_codes = []
42
+ field_codes << " # #{field._desc}"
43
+ field_codes << " attr_accessor :#{field._field_name}"
44
+ field_codes << " validates :#{field._field_name}, :presence => true" if field._args._required
45
+ field_codes << ""
46
+ fields << field_codes.join("\n")
47
+ end
48
+ fields.join("\n")
49
+ end
50
+
51
+ def adapt_template(gem_name_camel, fields)
52
+ gem_name = @dsl._gem_name
53
+ erb = ERB.new(DSL_MODEL_TEMPLATE)
54
+ erb.result(binding)
55
+ end
56
+
57
+ def generate_dsl_model_src(dsl_model_src)
58
+ File.open("./lib/#{@dsl._gem_name}_dsl_model.rb", "w") {|f|f.puts dsl_model_src}
59
+ end
60
+ end
61
+
62
+ class InvalidDslError < StandardError;end
63
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ require "generators/generators"
3
+
4
+ module Dslable::Generators::RSpec
5
+ # Dslable::Generators::RSpec RSpec Template Generator(only execute 'rspec --init')
6
+ class SpecTemplate
7
+ attr_accessor :dsl
8
+
9
+ #== initialize generate rspec template
10
+ #=== Params
11
+ #- _dsl: input from dsl
12
+ def initialize(_dsl)
13
+ @dsl = _dsl
14
+ end
15
+
16
+ #== generate rspec template
17
+ def generate
18
+ `rspec --init`
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ require "generators/generators"
3
+
4
+ module Dslable::Generators::RSpec
5
+ # Dslable::Generators::RSpec Specs Generator specs
6
+ class Specs
7
+ attr_accessor :dsl
8
+
9
+ #== initialize generate specs
10
+ #=== Params
11
+ #- _dsl: input from dsl
12
+ def initialize(_dsl)
13
+ @dsl = _dsl
14
+ end
15
+
16
+ #== generate specs
17
+ def generate
18
+ generate_core
19
+ end
20
+
21
+ def generate_core
22
+ core_class_name = "#{@dsl.camelized_gem_name}::Core"
23
+ core_class_file_name = "#{@dsl._gem_name}_core"
24
+ `piccolo e #{core_class_name} #{core_class_file_name} init execute`
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+ require "generators/generators"
3
+ require "dslable_dsl"
4
+
5
+ module Dslable::Generators::Settings
6
+ class Gemfile
7
+ GEMFILE_TEMPLATE =<<-EOF
8
+ source 'https://rubygems.org'
9
+
10
+ gemspec
11
+ gem "rspec", "~> 2.14.1"
12
+ gem "thor", "~> 0.18.1"
13
+ gem "simplecov", "~> 0.8.2"
14
+ gem "activesupport", "~> 4.0.1"
15
+ gem "activemodel", "~> 4.0.2"
16
+ EOF
17
+
18
+ attr_accessor :dsl
19
+
20
+ #== initialize dsl model
21
+ #=== Params
22
+ #- _dsl: input from dsl
23
+ def initialize(_dsl)
24
+ raise InvalidDslError.new("dsl not allow nil") if _dsl.nil?
25
+ @dsl = _dsl
26
+ end
27
+
28
+ def generate
29
+ File.open("./Gemfile", "w") {|f|f.puts GEMFILE_TEMPLATE}
30
+ end
31
+ end
32
+ class InvalidDslError < StandardError;end
33
+ end
@@ -0,0 +1,262 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+ require "dslable_args"
4
+
5
+ describe Dslable::Args do
6
+ context :desc do
7
+ cases = [
8
+ {
9
+ case_no: 1,
10
+ case_title: "valid description",
11
+ input: "this is descripotion",
12
+ expected: "this is descripotion"
13
+ },
14
+ {
15
+ case_no: 2,
16
+ case_title: "empty description",
17
+ input: "",
18
+ expected: ""
19
+ },
20
+ {
21
+ case_no: 3,
22
+ case_title: "nil description",
23
+ input: nil,
24
+ expected: ""
25
+ },
26
+ ]
27
+
28
+ cases.each do |c|
29
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
30
+ begin
31
+ case_before c
32
+
33
+ # -- given --
34
+ dslable_args = Dslable::Args.new
35
+
36
+ # -- when --
37
+ dslable_args.desc c[:input]
38
+
39
+ # -- then --
40
+ actual = dslable_args._desc
41
+ expect(actual).to eq(c[:expected])
42
+ ensure
43
+ case_after c
44
+ end
45
+ end
46
+
47
+ def case_before(c)
48
+ # implement each case before
49
+ end
50
+
51
+ def case_after(c)
52
+ # implement each case after
53
+ end
54
+ end
55
+ end
56
+
57
+ context :args_name do
58
+ cases = [
59
+ {
60
+ case_no: 1,
61
+ case_title: "valid dsl_args name",
62
+ input: "abcdefghijklmnopqrstuvwxyz01234567891_",
63
+ expected: "abcdefghijklmnopqrstuvwxyz01234567891_"
64
+ },
65
+ {
66
+ case_no: 2,
67
+ case_title: "empty dsl_args name",
68
+ input: "",
69
+ expect_error: true
70
+ },
71
+ {
72
+ case_no: 3,
73
+ case_title: "nil dsl_args name",
74
+ input: nil,
75
+ expect_error: true
76
+ },
77
+ {
78
+ case_no: 4,
79
+ case_title: "invalid dsl_args name contain space",
80
+ input: "method name",
81
+ expect_error: true
82
+ },
83
+ ]
84
+
85
+ cases.each do |c|
86
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
87
+ begin
88
+ case_before c
89
+
90
+ # -- given --
91
+ dslable_args = Dslable::Args.new
92
+
93
+ # -- when --
94
+ if c[:expect_error]
95
+ lambda {dslable_args.args_name c[:input]}.should raise_error(Dslable::InvalidArgsError)
96
+ next
97
+ end
98
+ dslable_args.args_name c[:input]
99
+ actual = dslable_args._args_name
100
+
101
+ # -- then --
102
+ expect(actual).to eq(c[:expected])
103
+ ensure
104
+ case_after c
105
+ end
106
+ end
107
+
108
+ def case_before(c)
109
+ # implement each case before
110
+ end
111
+
112
+ def case_after(c)
113
+ # implement each case after
114
+ end
115
+ end
116
+ end
117
+
118
+ context :klass do
119
+ cases = [
120
+ {
121
+ case_no: 1,
122
+ case_title: "valid klass String",
123
+ input: String,
124
+ expected: String
125
+ },
126
+ {
127
+ case_no: 2,
128
+ case_title: "valid klass Array",
129
+ input: Array,
130
+ expected: Array
131
+ },
132
+ {
133
+ case_no: 3,
134
+ case_title: "valid klass Hash",
135
+ input: Hash,
136
+ expected: Hash
137
+ },
138
+ {
139
+ case_no: 4,
140
+ case_title: "invalid klass Hash",
141
+ input: Fixnum,
142
+ expect_error: true
143
+ },
144
+ {
145
+ case_no: 5,
146
+ case_title: "nil klass",
147
+ input: nil,
148
+ expect_error: true
149
+ },
150
+ ]
151
+
152
+ cases.each do |c|
153
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
154
+ begin
155
+ case_before c
156
+
157
+ # -- given --
158
+ dslable_args = Dslable::Args.new
159
+
160
+ # -- when --
161
+ if c[:expect_error]
162
+ lambda {dslable_args.klass c[:input]}.should raise_error(Dslable::InvalidArgsError)
163
+ next
164
+ end
165
+ dslable_args.klass c[:input]
166
+ actual = dslable_args._klass
167
+
168
+ # -- then --
169
+ expect(actual).to eq(c[:expected])
170
+ ensure
171
+ case_after c
172
+ end
173
+ end
174
+
175
+ def case_before(c)
176
+ # implement each case before
177
+ end
178
+
179
+ def case_after(c)
180
+ # implement each case after
181
+ end
182
+ end
183
+ end
184
+
185
+ context :required do
186
+ cases = [
187
+ {
188
+ case_no: 1,
189
+ case_title: "valid required",
190
+ expected: true
191
+ },
192
+ ]
193
+
194
+ cases.each do |c|
195
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
196
+ begin
197
+ case_before c
198
+
199
+ # -- given --
200
+ dslable_args = Dslable::Args.new
201
+
202
+ # -- when --
203
+ dslable_args.required
204
+ actual = dslable_args._required
205
+
206
+ # -- then --
207
+ expect(actual).to eq(c[:expected])
208
+ ensure
209
+ case_after c
210
+ end
211
+ end
212
+
213
+ def case_before(c)
214
+ # implement each case before
215
+ end
216
+
217
+ def case_after(c)
218
+ # implement each case after
219
+ end
220
+ end
221
+ end
222
+
223
+ context :default_value do
224
+ cases = [
225
+ {
226
+ case_no: 1,
227
+ case_title: "valid default",
228
+ input: "valid default",
229
+ expected: "valid default"
230
+ },
231
+ ]
232
+
233
+ cases.each do |c|
234
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
235
+ begin
236
+ case_before c
237
+
238
+ # -- given --
239
+ dslable_args = Dslable::Args.new
240
+
241
+ # -- when --
242
+ dslable_args.default_value c[:input]
243
+ actual = dslable_args._default_value
244
+
245
+ # -- then --
246
+ expect(actual).to eq(c[:expected])
247
+ ensure
248
+ case_after c
249
+ end
250
+ end
251
+
252
+ def case_before(c)
253
+ # implement each case before
254
+ end
255
+
256
+ def case_after(c)
257
+ # implement each case after
258
+ end
259
+ end
260
+ end
261
+
262
+ end