tbpgr_utils 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -21,15 +21,16 @@ Or install it yourself as:
21
21
  | class/module/method| mean|
22
22
  |:-----------|:------------|
23
23
  |Array#together|loop all arrays by block|
24
+ |AttributesInitializable::ClassMethods.attr_accessor_init|generate attr_accessors + initializer|
25
+ |Ghostable module|help to create ghost method(dynamic method define by ussing method_missing + pattern-method-name)|
26
+ |Kernel#bulk_define_methods|define methods to classes. methods have simple return value.|
27
+ |Kernel#print_eval|Print code + eval result|
28
+ |Kernel#puts_eval|Puts code + eval result|
24
29
  |Object#any_of?|if self match any one of items, return true|
25
30
  |Object#boolean?|data type check for boolean|
26
31
  |Object#my_methods|return public/protected/private self define methods|
27
32
  |String#justify_table|justify pipe format table string|
28
- |AttributesInitializable::ClassMethods.attr_accessor_init|generate attr_accessors + initializer|
29
33
  |Templatable module|get result from template + placeholder|
30
- |Ghostable module|help to create ghost method(dynamic method define by ussing method_missing + pattern-method-name)|
31
- |Kernel#print_eval|Print code + eval result|
32
- |Kernel#puts_eval|Puts code + eval result|
33
34
 
34
35
  ### Array#together
35
36
  ~~~ruby
@@ -42,69 +43,6 @@ numbers = %w{1 2 3}
42
43
  end
43
44
  ~~~
44
45
 
45
- ### Object#any_of?
46
- ~~~ruby
47
- require 'tbpgr_utils'
48
-
49
- p 'hoge'.any_of? 'hoge', 'hige' # =>true
50
- p 'hoge'.any_of?(*%w{hoge hige}) # =>true
51
- p 'hige'.any_of? 'hoge', 'hige' # =>true
52
- p 'hege'.any_of? 'hoge', 'hige' # =>false
53
- p 1.any_of? 1, 2, 3 # =>true
54
- p 4.any_of? 1, 2, 3 # =>false
55
- ~~~
56
-
57
- ### Object#boolean?
58
- ~~~ruby
59
- require 'tbpgr_utils'
60
-
61
- p true.boolean? # =>true
62
- p false.boolean? # =>true
63
- p nil.boolean? # =>false
64
- p "".boolean? # =>false
65
- p "true".boolean? # =>false
66
- ~~~
67
-
68
- ### Object#my_methods
69
- ~~~ruby
70
- require 'tbpgr_utils'
71
-
72
- class Hoge
73
- def hgoe
74
- end
75
-
76
- protected
77
- def hige
78
- end
79
-
80
- private
81
- def hege
82
- end
83
- end
84
-
85
- p Hoge.new.my_methods # =>[:hoge, :hige, :hege]
86
- ~~~
87
-
88
- ### String#justify_table
89
- ~~~ruby
90
- require 'tbpgr_utils'
91
-
92
- str =<<-EOS
93
- |* hogehogehoge|* hege|* hige|
94
- |test|tester|testest|
95
- |test|tester|aaaaaaaaaaaaaaaaaaaaaaatestest|
96
- EOS
97
-
98
- puts str.justify_table
99
- ~~~
100
-
101
- output
102
- ~~~
103
- |* hogehogehoge|* hage|* hige |
104
- |test |tester|testest |
105
- |test |tester|aaaaaaaaaaaaaaaaaaaaaaatestest|
106
- ~~~
107
-
108
46
  ### AttributesInitializable::ClassMethods.attr_accessor_init
109
47
  ~~~ruby
110
48
  require 'attributes_initializable'
@@ -150,42 +88,6 @@ p atr_sample2.atr1 # => atr1
150
88
  p atr_sample2.atr2 # => atr2
151
89
  ~~~
152
90
 
153
- ### Templatable
154
- * include Templatable
155
- * set template by here-document
156
- * in template, parameter must name 'placeholders[:xxxxx]'. xxxxx is your favorite name.
157
- * when create instance, you must set materials to create template. after, you can get this value from @materials.
158
- * you must create manufactured_xxx methods. xxx is each-placeholder name.
159
- * you can get result by 'result' method.
160
-
161
- ~~~ruby
162
- require 'templatable'
163
-
164
- class TemplateUser
165
- include Templatable
166
- template <<-EOS
167
- line1:<%=placeholders[:hoge]%>
168
- line2:<%=placeholders[:hige]%>
169
- EOS
170
-
171
- def manufactured_hoge
172
- "hoge-#{@materials}"
173
- end
174
-
175
- def manufactured_hige
176
- "hige-#{@materials}"
177
- end
178
- end
179
-
180
- p TemplateUser.new('sample').result
181
- ~~~
182
-
183
- output
184
- ~~~
185
- line1:hoge-sample
186
- line2:hige-sample
187
- ~~~
188
-
189
91
  ### Ghostable
190
92
  * include Ghostable
191
93
  * create ghost method by using Ghostable::ghost_method
@@ -232,6 +134,54 @@ sample.contain_hoge? "test_hige_test" # => return false
232
134
  sample.contain_hige? "test_hige_test" # => return true
233
135
  ~~~
234
136
 
137
+ ### Kernel#bulk_define_methods
138
+ Define methods to classes. Methods have simple return value.
139
+
140
+ ~~~ruby
141
+ bulk_define_methods [NilClass, FalseClass], :blank?, true
142
+ bulk_define_methods [TrueClass, Numeric], "blank?", false
143
+
144
+ puts nil.blank? # => true
145
+ puts false.blank? # => true
146
+ puts true.blank? # => false
147
+ puts 1.blank? # => false
148
+
149
+ bulk_define_methods [NilClass, FalseClass], [:blank?, :present?], [true, false]
150
+ bulk_define_methods [TrueClass, Numeric], [:blank?, :present?], [false, true]
151
+
152
+ puts nil.blank? # => true
153
+ puts nil.present? # => false
154
+ puts false.blank? # => true
155
+ puts false.present? # => false
156
+ puts true.blank? # => false
157
+ puts true.present? # => true
158
+ puts 1.blank? # => false
159
+ puts 1.present? # => true
160
+ ~~~
161
+
162
+ if you don't use bulk_define_methods, followinng code is same mean.
163
+ ~~~ruby
164
+ class NilClass
165
+ def blank?
166
+ true
167
+ end
168
+
169
+ def present?
170
+ false
171
+ end
172
+ end
173
+
174
+ class FalseClass
175
+ def blank?
176
+ true
177
+ end
178
+
179
+ def present?
180
+ false
181
+ end
182
+ end
183
+ ~~~
184
+
235
185
  ### Kernel#print_eval
236
186
  This method for sample code. for manual, for blog-entry's-snippet ...etc.
237
187
 
@@ -263,7 +213,107 @@ output
263
213
  "hoge-#{message}" # => "hoge-msg"
264
214
  ~~~
265
215
 
216
+ ### Object#any_of?
217
+ ~~~ruby
218
+ require 'tbpgr_utils'
219
+
220
+ p 'hoge'.any_of? 'hoge', 'hige' # =>true
221
+ p 'hoge'.any_of?(*%w{hoge hige}) # =>true
222
+ p 'hige'.any_of? 'hoge', 'hige' # =>true
223
+ p 'hege'.any_of? 'hoge', 'hige' # =>false
224
+ p 1.any_of? 1, 2, 3 # =>true
225
+ p 4.any_of? 1, 2, 3 # =>false
226
+ ~~~
227
+
228
+ ### Object#boolean?
229
+ ~~~ruby
230
+ require 'tbpgr_utils'
231
+
232
+ p true.boolean? # =>true
233
+ p false.boolean? # =>true
234
+ p nil.boolean? # =>false
235
+ p "".boolean? # =>false
236
+ p "true".boolean? # =>false
237
+ ~~~
238
+
239
+ ### Object#my_methods
240
+ ~~~ruby
241
+ require 'tbpgr_utils'
242
+
243
+ class Hoge
244
+ def hgoe
245
+ end
246
+
247
+ protected
248
+ def hige
249
+ end
250
+
251
+ private
252
+ def hege
253
+ end
254
+ end
255
+
256
+ p Hoge.new.my_methods # =>[:hoge, :hige, :hege]
257
+ ~~~
258
+
259
+ ### String#justify_table
260
+ ~~~ruby
261
+ require 'tbpgr_utils'
262
+
263
+ str =<<-EOS
264
+ |* hogehogehoge|* hege|* hige|
265
+ |test|tester|testest|
266
+ |test|tester|aaaaaaaaaaaaaaaaaaaaaaatestest|
267
+ EOS
268
+
269
+ puts str.justify_table
270
+ ~~~
271
+
272
+ output
273
+ ~~~
274
+ |* hogehogehoge|* hage|* hige |
275
+ |test |tester|testest |
276
+ |test |tester|aaaaaaaaaaaaaaaaaaaaaaatestest|
277
+ ~~~
278
+
279
+ ### Templatable
280
+ * include Templatable
281
+ * set template by here-document
282
+ * in template, parameter must name 'placeholders[:xxxxx]'. xxxxx is your favorite name.
283
+ * when create instance, you must set materials to create template. after, you can get this value from @materials.
284
+ * you must create manufactured_xxx methods. xxx is each-placeholder name.
285
+ * you can get result by 'result' method.
286
+
287
+ ~~~ruby
288
+ require 'templatable'
289
+
290
+ class TemplateUser
291
+ include Templatable
292
+ template <<-EOS
293
+ line1:<%=placeholders[:hoge]%>
294
+ line2:<%=placeholders[:hige]%>
295
+ EOS
296
+
297
+ def manufactured_hoge
298
+ "hoge-#{@materials}"
299
+ end
300
+
301
+ def manufactured_hige
302
+ "hige-#{@materials}"
303
+ end
304
+ end
305
+
306
+ p TemplateUser.new('sample').result
307
+ ~~~
308
+
309
+ output
310
+ ~~~
311
+ line1:hoge-sample
312
+ line2:hige-sample
313
+ ~~~
314
+
266
315
  ## History
316
+ * version 0.0.8 : add Kernel#bulk_define_methods
267
317
  * version 0.0.7 : add Kernel#print_eval, Kernel#puts_eval
268
318
  * version 0.0.6 : add Ghostable
269
319
  * version 0.0.5 : add Templatable
@@ -18,9 +18,81 @@ module Kernel
18
18
  puts exec_eval code, binding
19
19
  end
20
20
 
21
+ # define methods to classes. methods have simple return value.
22
+ #
23
+ # bulk_define_methods [NilClass, FalseClass], :blank?, true
24
+ # bulk_define_methods [TrueClass, Numeric], "blank?", false
25
+ #
26
+ # puts nil.blank? # => true
27
+ # puts false.blank? # => true
28
+ # puts true.blank? # => false
29
+ # puts 1.blank? # => false
30
+ #
31
+ # bulk_define_methods [NilClass, FalseClass], [:blank?, :present?], [true, false]
32
+ # bulk_define_methods [TrueClass, Numeric], [:blank?, :present?], [false, true]
33
+ #
34
+ # puts nil.blank? # => true
35
+ # puts nil.present? # => false
36
+ # puts false.blank? # => true
37
+ # puts false.present? # => false
38
+ # puts true.blank? # => false
39
+ # puts true.present? # => true
40
+ # puts 1.blank? # => false
41
+ # puts 1.present? # => true
42
+ #
43
+ # bulk_define_methods NilClass, :blank?, true is same as following code
44
+ #
45
+ # class NilClass
46
+ # def blank?
47
+ # true
48
+ # end
49
+ # end
50
+ def bulk_define_methods(classes, methods, constants)
51
+ validate_bulk_define_classes!(classes)
52
+ validate_bulk_define_methods!(methods)
53
+ size = get_classes_size(classes)
54
+ tmp_classes = fill_same_values(classes, size)
55
+ tmp_methods = fill_same_values(methods, size)
56
+ tmp_constants = fill_same_values(constants, size)
57
+ tmp_classes.each do |klass|
58
+ define_method_constants(klass, tmp_methods, tmp_constants)
59
+ end
60
+ end
61
+
62
+ private
63
+ def get_classes_size(classes)
64
+ size = classes.is_a?(Array) ? classes.size : 1
65
+ end
66
+
67
+ def fill_same_values(ary, size)
68
+ return ary if ary.is_a?(Array)
69
+ tmp = []
70
+ size.times {tmp << ary}
71
+ tmp
72
+ end
73
+
74
+ def validate_bulk_define_classes!(classes)
75
+ return if classes.class.any_of?(Array, Class)
76
+ fail TypeError, "invalid type #{a.class}. you have to use Array or Class or String or Symbol"
77
+ end
78
+
79
+ def validate_bulk_define_methods!(methods)
80
+ return if methods.class.any_of?(Array, String, Symbol)
81
+ fail TypeError, "invalid type #{a.class}. you have to use Array or Class or String or Symbol"
82
+ end
83
+
84
+ def define_method_constants(klass, methods, constants)
85
+ methods.each_with_index do |m, i|
86
+ klass.class_eval do
87
+ define_method m do |*args|
88
+ constants[i]
89
+ end
90
+ end
91
+ end
92
+ end
93
+
21
94
  def exec_eval(code, binding)
22
95
  ret = eval code, binding
23
96
  "#{code} # => #{ret.inspect}"
24
97
  end
25
- private :exec_eval
26
98
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.7'
5
+ VERSION = '0.0.8'
6
6
  end
data/spec/kernel_spec.rb CHANGED
@@ -1,106 +1,183 @@
1
- # encoding: utf-8
2
- require "spec_helper"
3
- require "open_classes/kernel"
4
-
5
- describe Kernel do
6
- def get_stdout
7
- begin
8
- eval "$stdout = StringIO.new"
9
- yield
10
- result = eval("$stdout").string
11
- ensure
12
- eval "$stdout = STDOUT"
13
- end
14
- result
15
- end
16
-
17
- context :print_eval do
18
- cases = [
19
- {
20
- case_no: 1,
21
- case_title: "no bind case",
22
- code: "8/4",
23
- expected: "8/4 # => 2",
24
- },
25
- {
26
- case_no: 2,
27
- case_title: "with bind case",
28
- code: "\"hoge-\#{message}\"",
29
- bind: "msg",
30
- expected: "\"hoge-\#{message}\" # => \"hoge-msg\"",
31
- },
32
- ]
33
-
34
- cases.each do |c|
35
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
36
- begin
37
- case_before c
38
-
39
- # -- given --
40
- message = c[:bind] if c[:bind]
41
-
42
- # -- when --
43
- actual = get_stdout { print_eval c[:code], binding }
44
-
45
- # -- then --
46
- expect(actual).to eq(c[:expected])
47
- ensure
48
- case_after c
49
- end
50
- end
51
-
52
- def case_before(c)
53
- # implement each case before
54
- end
55
-
56
- def case_after(c)
57
- # implement each case after
58
- end
59
- end
60
- end
61
-
62
- context :puts_eval do
63
- cases = [
64
- {
65
- case_no: 1,
66
- case_title: "no bind case",
67
- code: "8/4",
68
- expected: "8/4 # => 2\n",
69
- },
70
- {
71
- case_no: 2,
72
- case_title: "with bind case",
73
- code: '"hoge-#{message}"',
74
- bind: "msg",
75
- expected: '"hoge-#{message}" # => "hoge-msg"' + "\n",
76
- },
77
- ]
78
-
79
- cases.each do |c|
80
- it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
81
- begin
82
- case_before c
83
-
84
- # -- given --
85
- message = c[:bind] if c[:bind]
86
-
87
- # -- when --
88
- actual = get_stdout { puts_eval c[:code], binding }
89
-
90
- # -- then --
91
- expect(actual).to eq(c[:expected])
92
- ensure
93
- case_after c
94
- end
95
- end
96
-
97
- def case_before(c)
98
- # implement each case before
99
- end
100
-
101
- def case_after(c)
102
- # implement each case after
103
- end
104
- end
105
- end
106
- end
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+ require "open_classes/kernel"
4
+
5
+ describe Kernel do
6
+ def get_stdout
7
+ begin
8
+ eval "$stdout = StringIO.new"
9
+ yield
10
+ result = eval("$stdout").string
11
+ ensure
12
+ eval "$stdout = STDOUT"
13
+ end
14
+ result
15
+ end
16
+
17
+ context :print_eval do
18
+ cases = [
19
+ {
20
+ case_no: 1,
21
+ case_title: "no bind case",
22
+ code: "8/4",
23
+ expected: "8/4 # => 2",
24
+ },
25
+ {
26
+ case_no: 2,
27
+ case_title: "with bind case",
28
+ code: "\"hoge-\#{message}\"",
29
+ bind: "msg",
30
+ expected: "\"hoge-\#{message}\" # => \"hoge-msg\"",
31
+ },
32
+ ]
33
+
34
+ cases.each do |c|
35
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
36
+ begin
37
+ case_before c
38
+
39
+ # -- given --
40
+ message = c[:bind] if c[:bind]
41
+
42
+ # -- when --
43
+ actual = get_stdout { print_eval c[:code], binding }
44
+
45
+ # -- then --
46
+ expect(actual).to eq(c[:expected])
47
+ ensure
48
+ case_after c
49
+ end
50
+ end
51
+
52
+ def case_before(c)
53
+ # implement each case before
54
+ end
55
+
56
+ def case_after(c)
57
+ # implement each case after
58
+ end
59
+ end
60
+ end
61
+
62
+ context :puts_eval do
63
+ cases = [
64
+ {
65
+ case_no: 1,
66
+ case_title: "no bind case",
67
+ code: "8/4",
68
+ expected: "8/4 # => 2\n",
69
+ },
70
+ {
71
+ case_no: 2,
72
+ case_title: "with bind case",
73
+ code: '"hoge-#{message}"',
74
+ bind: "msg",
75
+ expected: '"hoge-#{message}" # => "hoge-msg"' + "\n",
76
+ },
77
+ ]
78
+
79
+ cases.each do |c|
80
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
81
+ begin
82
+ case_before c
83
+
84
+ # -- given --
85
+ message = c[:bind] if c[:bind]
86
+
87
+ # -- when --
88
+ actual = get_stdout { puts_eval c[:code], binding }
89
+
90
+ # -- then --
91
+ expect(actual).to eq(c[:expected])
92
+ ensure
93
+ case_after c
94
+ end
95
+ end
96
+
97
+ def case_before(c)
98
+ # implement each case before
99
+ end
100
+
101
+ def case_after(c)
102
+ # implement each case after
103
+ end
104
+ end
105
+ end
106
+
107
+ context :bulk_define_methods do
108
+ cases = [
109
+ {
110
+ case_no: 1,
111
+ case_title: "all Array case",
112
+ classes: [NilClass, FalseClass],
113
+ methods: [:blank?, :present?],
114
+ contents: [true, false],
115
+ instances: [nil, false],
116
+ expecteds: [true, false, true, false],
117
+ },
118
+ {
119
+ case_no: 2,
120
+ case_title: "Class, String, String case",
121
+ classes: NilClass,
122
+ methods: "hoge",
123
+ contents: "ret hoge",
124
+ instances: [nil],
125
+ expecteds: ["ret hoge"],
126
+ },
127
+ {
128
+ case_no: 3,
129
+ case_title: "invalid classes type case",
130
+ classes: :NilClass,
131
+ methods: "hoge",
132
+ contents: "ret hoge",
133
+ expect_error: true,
134
+ },
135
+ {
136
+ case_no: 4,
137
+ case_title: "invalid methods type case",
138
+ classes: NilClass,
139
+ methods: 1,
140
+ contents: "ret hoge",
141
+ expect_error: true,
142
+ },
143
+ ]
144
+
145
+ cases.each do |c|
146
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
147
+ begin
148
+ case_before c
149
+
150
+ # -- given --
151
+ # nothing
152
+
153
+ # -- when --
154
+ if c[:expect_error]
155
+ lambda { bulk_define_methods c[:classes], c[:methods], c[:contents] }.should raise_error(StandardError)
156
+ next
157
+ end
158
+ bulk_define_methods c[:classes], c[:methods], c[:contents]
159
+
160
+ # -- then --
161
+ method_list = c[:methods].is_a?(Array) ? c[:methods] : [c[:methods]]
162
+ cnt = 0
163
+ c[:instances].each do |instance|
164
+ method_list.each do |method_name|
165
+ expect(instance.method(method_name).call).to eq(c[:expecteds][cnt])
166
+ cnt += 1
167
+ end
168
+ end
169
+ ensure
170
+ case_after c
171
+ end
172
+ end
173
+
174
+ def case_before(c)
175
+ # implement each case before
176
+ end
177
+
178
+ def case_after(c)
179
+ # implement each case after
180
+ end
181
+ end
182
+ end
183
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tbpgr_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-14 00:00:00.000000000 Z
12
+ date: 2014-01-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &29606124 !ruby/object:Gem::Requirement
16
+ requirement: &29737188 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 4.0.1
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *29606124
24
+ version_requirements: *29737188
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &29605836 !ruby/object:Gem::Requirement
27
+ requirement: &29736900 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '1.3'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *29605836
35
+ version_requirements: *29736900
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &29605608 !ruby/object:Gem::Requirement
38
+ requirement: &29736672 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *29605608
46
+ version_requirements: *29736672
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &29605284 !ruby/object:Gem::Requirement
49
+ requirement: &29736348 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: 2.14.1
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *29605284
57
+ version_requirements: *29736348
58
58
  description: Utilities
59
59
  email:
60
60
  - tbpgr@tbpgr.jp