tbpgr_utils 0.0.51 → 0.0.52
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/README.md +50 -0
- data/lib/eval_helper/if_code.rb +57 -0
- data/lib/eval_helper.rb +5 -0
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/eval_helper/if_code_spec.rb +64 -0
- metadata +16 -12
data/README.md
CHANGED
@@ -57,6 +57,7 @@ Or install it yourself as:
|
|
57
57
|
|[AttributesInitializable::ClassMethods.attr_reader_init](#attributesinitializableclassmethodsattr_reader_init) |generate attr_reader + initializer |
|
58
58
|
|[AttributesInitializable::ClassMethods.attr_writer init](#attributesinitializableclassmethodsattr_writer_init) |generate attr_writer + initializer |
|
59
59
|
|[EndERB.apply](#enderbapply) |for single template script using __END__ and DATA |
|
60
|
+
|[EvalHelper#if_code](#evalhelperif_code) |create if strings, for eval |
|
60
61
|
|[TbpgrUtils File.insert_bom](#fileinsert_bom) |insert BOM to UTF-8 File |
|
61
62
|
|[Ghostable module](#ghostable) |help to create ghost method(dynamic method define by ussing method_missing + pattern-method-name) |
|
62
63
|
|[TbpgrUtils Kernel#bulk_define_methods](#kernelbulk_define_methods) |define methods to classes. methods have simple return value. |
|
@@ -1356,6 +1357,54 @@ output
|
|
1356
1357
|
|
1357
1358
|
[back to list](#list)
|
1358
1359
|
|
1360
|
+
### EvalHelper#if_code
|
1361
|
+
|
1362
|
+
if case
|
1363
|
+
|
1364
|
+
~~~ruby
|
1365
|
+
class EvalHelperTest
|
1366
|
+
include EvalHelper
|
1367
|
+
|
1368
|
+
def hoge(hash)
|
1369
|
+
msg = hash[:input]
|
1370
|
+
code = if_code(hash[:if_cond], hash[:if_proc], hash[:else_proc])
|
1371
|
+
instance_eval code
|
1372
|
+
end
|
1373
|
+
end
|
1374
|
+
|
1375
|
+
hash = {
|
1376
|
+
input: "test",
|
1377
|
+
if_cond: "msg == 'test'",
|
1378
|
+
if_proc: "true",
|
1379
|
+
else_proc: "false",
|
1380
|
+
}
|
1381
|
+
EvalHelperTest.new.hoge(hash) # => return true
|
1382
|
+
~~~
|
1383
|
+
|
1384
|
+
else case
|
1385
|
+
|
1386
|
+
~~~ruby
|
1387
|
+
class EvalHelperTest
|
1388
|
+
include EvalHelper
|
1389
|
+
|
1390
|
+
def hoge(hash)
|
1391
|
+
msg = hash[:input]
|
1392
|
+
code = if_code(hash[:if_cond], hash[:if_proc], hash[:else_proc])
|
1393
|
+
instance_eval code
|
1394
|
+
end
|
1395
|
+
end
|
1396
|
+
|
1397
|
+
hash = {
|
1398
|
+
input: "not_test",
|
1399
|
+
if_cond: "msg == 'test'",
|
1400
|
+
if_proc: "true",
|
1401
|
+
else_proc: "false",
|
1402
|
+
}
|
1403
|
+
EvalHelperTest.new.hoge(hash) # => return false
|
1404
|
+
~~~
|
1405
|
+
|
1406
|
+
[back to list](#list)
|
1407
|
+
|
1359
1408
|
### MetasyntacticVariable
|
1360
1409
|
* META variable
|
1361
1410
|
|
@@ -1863,6 +1912,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
|
1863
1912
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
1864
1913
|
|
1865
1914
|
## History
|
1915
|
+
* version 0.0.51 : add EvalHelper#if_code
|
1866
1916
|
* version 0.0.50 : add String#to_markdown_heading
|
1867
1917
|
* version 0.0.49 : add String#to_tab_heading
|
1868
1918
|
* version 0.0.48 : add String#to_space4_heading
|
@@ -0,0 +1,57 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module EvalHelper
|
4
|
+
# create if strings, for eval
|
5
|
+
#
|
6
|
+
# ==== Examples
|
7
|
+
#
|
8
|
+
# if case
|
9
|
+
#
|
10
|
+
# class EvalHelperTest
|
11
|
+
# include EvalHelper
|
12
|
+
#
|
13
|
+
# def hoge(hash)
|
14
|
+
# msg = hash[:input]
|
15
|
+
# code = if_code(hash[:if_cond], hash[:if_proc], hash[:else_proc])
|
16
|
+
# instance_eval code
|
17
|
+
# end
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# hash = {
|
21
|
+
# input: "test",
|
22
|
+
# if_cond: "msg == 'test'",
|
23
|
+
# if_proc: "true",
|
24
|
+
# else_proc: "false",
|
25
|
+
# }
|
26
|
+
# EvalHelperTest.new.hoge(hash) # => return true
|
27
|
+
#
|
28
|
+
# else case
|
29
|
+
#
|
30
|
+
# class EvalHelperTest
|
31
|
+
# include EvalHelper
|
32
|
+
#
|
33
|
+
# def hoge(hash)
|
34
|
+
# msg = hash[:input]
|
35
|
+
# code = if_code(hash[:if_cond], hash[:if_proc], hash[:else_proc])
|
36
|
+
# instance_eval code
|
37
|
+
# end
|
38
|
+
# end
|
39
|
+
#
|
40
|
+
# hash = {
|
41
|
+
# input: "not_test",
|
42
|
+
# if_cond: "msg == 'test'",
|
43
|
+
# if_proc: "true",
|
44
|
+
# else_proc: "false",
|
45
|
+
# }
|
46
|
+
# EvalHelperTest.new.hoge(hash) # => return false
|
47
|
+
#
|
48
|
+
def if_code(condition, if_proc, else_proc)
|
49
|
+
<<-EOS
|
50
|
+
if #{condition}
|
51
|
+
#{if_proc}
|
52
|
+
else
|
53
|
+
#{else_proc}
|
54
|
+
end
|
55
|
+
EOS
|
56
|
+
end
|
57
|
+
end
|
data/lib/eval_helper.rb
ADDED
data/lib/tbpgr_utils/version.rb
CHANGED
@@ -0,0 +1,64 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'eval_helper'
|
4
|
+
|
5
|
+
describe 'EvalHelper' do
|
6
|
+
context :if_code do
|
7
|
+
class EvalHelperTest
|
8
|
+
include EvalHelper
|
9
|
+
|
10
|
+
def hoge(hash)
|
11
|
+
msg = hash[:input]
|
12
|
+
code = if_code(hash[:if_cond], hash[:if_proc], hash[:else_proc])
|
13
|
+
instance_eval code
|
14
|
+
end
|
15
|
+
end
|
16
|
+
cases = [
|
17
|
+
{
|
18
|
+
case_no: 1,
|
19
|
+
case_title: 'if case',
|
20
|
+
input: 'test',
|
21
|
+
if_cond: "msg == 'test'",
|
22
|
+
if_proc: 'true',
|
23
|
+
else_proc: 'false',
|
24
|
+
expected: true,
|
25
|
+
},
|
26
|
+
{
|
27
|
+
case_no: 2,
|
28
|
+
case_title: 'else case',
|
29
|
+
input: 'not_test',
|
30
|
+
if_cond: "msg == 'test'",
|
31
|
+
if_proc: 'true',
|
32
|
+
else_proc: 'false',
|
33
|
+
expected: false,
|
34
|
+
},
|
35
|
+
]
|
36
|
+
|
37
|
+
cases.each do |c|
|
38
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
39
|
+
begin
|
40
|
+
case_before c
|
41
|
+
|
42
|
+
# -- given --
|
43
|
+
eval_helper = EvalHelperTest.new
|
44
|
+
|
45
|
+
# -- when --
|
46
|
+
actual = eval_helper.hoge(c)
|
47
|
+
|
48
|
+
# -- then --
|
49
|
+
expect(actual).to eq(c[:expected])
|
50
|
+
ensure
|
51
|
+
case_after c
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def case_before(c)
|
56
|
+
# implement each case before
|
57
|
+
end
|
58
|
+
|
59
|
+
def case_after(c)
|
60
|
+
# implement each case after
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
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.
|
4
|
+
version: 0.0.52
|
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-02-
|
12
|
+
date: 2014-02-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &26331132 !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: *
|
24
|
+
version_requirements: *26331132
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &26330844 !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: *
|
35
|
+
version_requirements: *26330844
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &26330616 !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: *
|
46
|
+
version_requirements: *26330616
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &26330292 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ~>
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 2.14.1
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *26330292
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: simplecov
|
60
|
-
requirement: &
|
60
|
+
requirement: &26329992 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,7 +65,7 @@ dependencies:
|
|
65
65
|
version: 0.8.2
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *26329992
|
69
69
|
description: Utilities
|
70
70
|
email:
|
71
71
|
- tbpgr@tbpgr.jp
|
@@ -85,6 +85,8 @@ files:
|
|
85
85
|
- lib/attributes_hashable.rb
|
86
86
|
- lib/attributes_initializable.rb
|
87
87
|
- lib/end_erb.rb
|
88
|
+
- lib/eval_helper.rb
|
89
|
+
- lib/eval_helper/if_code.rb
|
88
90
|
- lib/ghostable.rb
|
89
91
|
- lib/metasyntactic_variable.rb
|
90
92
|
- lib/open_classes/array.rb
|
@@ -146,6 +148,7 @@ files:
|
|
146
148
|
- rubocop-todo.yml
|
147
149
|
- spec/attributes_hashable_spec.rb
|
148
150
|
- spec/attributes_initializable_spec.rb
|
151
|
+
- spec/eval_helper/if_code_spec.rb
|
149
152
|
- spec/ghostable_spec.rb
|
150
153
|
- spec/metasyntactic_variable_spec.rb
|
151
154
|
- spec/open_classes/array/together_at_spec.rb
|
@@ -226,6 +229,7 @@ summary: Utilities
|
|
226
229
|
test_files:
|
227
230
|
- spec/attributes_hashable_spec.rb
|
228
231
|
- spec/attributes_initializable_spec.rb
|
232
|
+
- spec/eval_helper/if_code_spec.rb
|
229
233
|
- spec/ghostable_spec.rb
|
230
234
|
- spec/metasyntactic_variable_spec.rb
|
231
235
|
- spec/open_classes/array/together_at_spec.rb
|