tbpgr_utils 0.0.55 → 0.0.56
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +66 -1
- data/lib/eval_helper/ternary_operator.rb +64 -0
- data/lib/eval_helper.rb +1 -0
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/eval_helper/ternary_operator_spec.rb +89 -0
- metadata +15 -12
data/README.md
CHANGED
@@ -59,8 +59,9 @@ Or install it yourself as:
|
|
59
59
|
|[EndERB.apply](#enderbapply) |for single template script using __END__ and DATA |
|
60
60
|
|[EvalHelper#if_code](#evalhelperif_code) |create if strings, for eval |
|
61
61
|
|[EvalHelper#if_code_after](#evalhelperif_code_after) |create after-if strings, for eval |
|
62
|
+
|[EvalHelper#ternary_operator](#evalhelperternary_operator) |create ternary operator strings, for eval |
|
62
63
|
|[EvalHelper#unless_code](#evalhelperunless_code) |create unless strings, for eval |
|
63
|
-
|[EvalHelper#
|
64
|
+
|[EvalHelper#unless_code_after](#evalhelperunless_code_after) |create after-unless strings, for eval |
|
64
65
|
|[TbpgrUtils File.insert_bom](#fileinsert_bom) |insert BOM to UTF-8 File |
|
65
66
|
|[Ghostable module](#ghostable) |help to create ghost method(dynamic method define by ussing method_missing + pattern-method-name) |
|
66
67
|
|[TbpgrUtils Kernel#bulk_define_methods](#kernelbulk_define_methods) |define methods to classes. methods have simple return value. |
|
@@ -1461,6 +1462,69 @@ EvalHelperTest.new.hoge(hash) # => return 'default'
|
|
1461
1462
|
|
1462
1463
|
[back to list](#list)
|
1463
1464
|
|
1465
|
+
### EvalHelper#ternary_operator
|
1466
|
+
true case
|
1467
|
+
|
1468
|
+
~~~ruby
|
1469
|
+
require 'eval_helper'
|
1470
|
+
|
1471
|
+
class EvalHelperTernaryTest
|
1472
|
+
include EvalHelper
|
1473
|
+
|
1474
|
+
def hoge(hash)
|
1475
|
+
msg = hash[:input]
|
1476
|
+
code = \
|
1477
|
+
if hash[:ret]
|
1478
|
+
ternary_operator(hash[:cond], hash[:true_case], hash[:false_case], hash[:ret])
|
1479
|
+
else
|
1480
|
+
ternary_operator(hash[:cond], hash[:true_case], hash[:false_case])
|
1481
|
+
end
|
1482
|
+
instance_eval code
|
1483
|
+
end
|
1484
|
+
end
|
1485
|
+
|
1486
|
+
hash = {
|
1487
|
+
input: "test",
|
1488
|
+
cond: "msg == 'test'",
|
1489
|
+
true_case: "true",
|
1490
|
+
false_case: "false",
|
1491
|
+
ret: "ret",
|
1492
|
+
}
|
1493
|
+
EvalHelperTernaryTest.new.hoge(hash) # => return 'true'
|
1494
|
+
~~~
|
1495
|
+
|
1496
|
+
false case
|
1497
|
+
|
1498
|
+
~~~ruby
|
1499
|
+
require 'eval_helper'
|
1500
|
+
|
1501
|
+
class EvalHelperTernaryTest
|
1502
|
+
include EvalHelper
|
1503
|
+
|
1504
|
+
def hoge(hash)
|
1505
|
+
msg = hash[:input]
|
1506
|
+
code = \
|
1507
|
+
if hash[:ret]
|
1508
|
+
ternary_operator(hash[:cond], hash[:true_case], hash[:false_case], hash[:ret])
|
1509
|
+
else
|
1510
|
+
ternary_operator(hash[:cond], hash[:true_case], hash[:false_case])
|
1511
|
+
end
|
1512
|
+
instance_eval code
|
1513
|
+
end
|
1514
|
+
end
|
1515
|
+
|
1516
|
+
hash = {
|
1517
|
+
input: "not_test",
|
1518
|
+
cond: "msg == 'test'",
|
1519
|
+
true_case: "true",
|
1520
|
+
false_case: "false",
|
1521
|
+
ret: "ret",
|
1522
|
+
}
|
1523
|
+
EvalHelperTernaryTest.new.hoge(hash) # => return 'false'
|
1524
|
+
~~~
|
1525
|
+
|
1526
|
+
[back to list](#list)
|
1527
|
+
|
1464
1528
|
### EvalHelper#unless_code
|
1465
1529
|
|
1466
1530
|
unless case
|
@@ -2069,6 +2133,7 @@ if you are Sublime Text2 user, you can use snippet for TbpgrUtils.
|
|
2069
2133
|
https://github.com/tbpgr/tbpgr_utils_snippets
|
2070
2134
|
|
2071
2135
|
## History
|
2136
|
+
* version 0.0.56 : add EvalHelper#toternary_operator
|
2072
2137
|
* version 0.0.55 : add EvalHelper#unless_code_after
|
2073
2138
|
* version 0.0.54 : add EvalHelper#unless_code
|
2074
2139
|
* version 0.0.53 : add EvalHelper#if_code_after
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module EvalHelper
|
4
|
+
# create unless strings, for eval
|
5
|
+
#
|
6
|
+
# ==== Examples
|
7
|
+
#
|
8
|
+
# true case
|
9
|
+
#
|
10
|
+
# class EvalHelperTernaryTest
|
11
|
+
# include EvalHelper
|
12
|
+
#
|
13
|
+
# def hoge(hash)
|
14
|
+
# msg = hash[:input]
|
15
|
+
# code = \
|
16
|
+
# if hash[:ret]
|
17
|
+
# ternary_operator(hash[:cond], hash[:true_case], hash[:false_case], hash[:ret])
|
18
|
+
# else
|
19
|
+
# ternary_operator(hash[:cond], hash[:true_case], hash[:false_case])
|
20
|
+
# end
|
21
|
+
# instance_eval code
|
22
|
+
# end
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# hash = {
|
26
|
+
# input: "test",
|
27
|
+
# cond: "msg == 'test'",
|
28
|
+
# true_case: "true",
|
29
|
+
# false_case: "false",
|
30
|
+
# ret: "ret",
|
31
|
+
# }
|
32
|
+
# EvalHelperTernaryTest.new.hoge(hash) # => return 'true'
|
33
|
+
#
|
34
|
+
# false case
|
35
|
+
#
|
36
|
+
# class EvalHelperTernaryTest
|
37
|
+
# include EvalHelper
|
38
|
+
#
|
39
|
+
# def hoge(hash)
|
40
|
+
# msg = hash[:input]
|
41
|
+
# code = \
|
42
|
+
# if hash[:ret]
|
43
|
+
# ternary_operator(hash[:cond], hash[:true_case], hash[:false_case], hash[:ret])
|
44
|
+
# else
|
45
|
+
# ternary_operator(hash[:cond], hash[:true_case], hash[:false_case])
|
46
|
+
# end
|
47
|
+
# instance_eval code
|
48
|
+
# end
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# hash = {
|
52
|
+
# input: "not_test",
|
53
|
+
# cond: "msg == 'test'",
|
54
|
+
# true_case: "true",
|
55
|
+
# false_case: "false",
|
56
|
+
# ret: "ret",
|
57
|
+
# }
|
58
|
+
# EvalHelperTernaryTest.new.hoge(hash) # => return 'false'
|
59
|
+
#
|
60
|
+
def ternary_operator(condition, true_case, false_case, ret = nil)
|
61
|
+
ret = ret.nil? ? '' : "#{ret} = "
|
62
|
+
"#{ret}#{condition} ? #{true_case} : #{false_case}"
|
63
|
+
end
|
64
|
+
end
|
data/lib/eval_helper.rb
CHANGED
data/lib/tbpgr_utils/version.rb
CHANGED
@@ -0,0 +1,89 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'eval_helper'
|
4
|
+
|
5
|
+
describe 'EvalHelper' do
|
6
|
+
context :ternary_operator do
|
7
|
+
class EvalHelperTernaryTest
|
8
|
+
include EvalHelper
|
9
|
+
|
10
|
+
def hoge(hash)
|
11
|
+
msg = hash[:input]
|
12
|
+
code = \
|
13
|
+
if hash[:ret]
|
14
|
+
ternary_operator(hash[:cond], hash[:true_case], hash[:false_case], hash[:ret])
|
15
|
+
else
|
16
|
+
ternary_operator(hash[:cond], hash[:true_case], hash[:false_case])
|
17
|
+
end
|
18
|
+
instance_eval code
|
19
|
+
end
|
20
|
+
end
|
21
|
+
cases = [
|
22
|
+
{
|
23
|
+
case_no: 1,
|
24
|
+
case_title: 'true case',
|
25
|
+
input: 'test',
|
26
|
+
cond: "msg == 'test'",
|
27
|
+
true_case: 'true',
|
28
|
+
false_case: 'false',
|
29
|
+
expected: true,
|
30
|
+
},
|
31
|
+
{
|
32
|
+
case_no: 2,
|
33
|
+
case_title: 'false case',
|
34
|
+
input: 'not_test',
|
35
|
+
cond: "msg == 'test'",
|
36
|
+
true_case: 'true',
|
37
|
+
false_case: 'false',
|
38
|
+
expected: false,
|
39
|
+
},
|
40
|
+
{
|
41
|
+
case_no: 3,
|
42
|
+
case_title: 'true case with ret option',
|
43
|
+
input: 'test',
|
44
|
+
cond: "msg == 'test'",
|
45
|
+
true_case: 'true',
|
46
|
+
false_case: 'false',
|
47
|
+
ret: 'result',
|
48
|
+
expected: true,
|
49
|
+
},
|
50
|
+
{
|
51
|
+
case_no: 4,
|
52
|
+
case_title: 'false case with ret option',
|
53
|
+
input: 'not test',
|
54
|
+
cond: "msg == 'test'",
|
55
|
+
true_case: 'true',
|
56
|
+
false_case: 'false',
|
57
|
+
ret: 'result',
|
58
|
+
expected: false,
|
59
|
+
},
|
60
|
+
]
|
61
|
+
|
62
|
+
cases.each do |c|
|
63
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
64
|
+
begin
|
65
|
+
case_before c
|
66
|
+
|
67
|
+
# -- given --
|
68
|
+
eval_helper = EvalHelperTernaryTest.new
|
69
|
+
|
70
|
+
# -- when --
|
71
|
+
actual = eval_helper.hoge(c)
|
72
|
+
|
73
|
+
# -- then --
|
74
|
+
expect(actual).to eq(c[:expected])
|
75
|
+
ensure
|
76
|
+
case_after c
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def case_before(c)
|
81
|
+
# implement each case before
|
82
|
+
end
|
83
|
+
|
84
|
+
def case_after(c)
|
85
|
+
# implement each case after
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
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.56
|
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-03-
|
12
|
+
date: 2014-03-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &27379200 !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: *27379200
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &27378912 !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: *27378912
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &27378684 !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: *27378684
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &27378360 !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: *27378360
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: simplecov
|
60
|
-
requirement: &
|
60
|
+
requirement: &27378060 !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: *27378060
|
69
69
|
description: Utilities
|
70
70
|
email:
|
71
71
|
- tbpgr@tbpgr.jp
|
@@ -88,6 +88,7 @@ files:
|
|
88
88
|
- lib/eval_helper.rb
|
89
89
|
- lib/eval_helper/if_code.rb
|
90
90
|
- lib/eval_helper/if_code_after.rb
|
91
|
+
- lib/eval_helper/ternary_operator.rb
|
91
92
|
- lib/eval_helper/unless_code.rb
|
92
93
|
- lib/eval_helper/unless_code_after.rb
|
93
94
|
- lib/ghostable.rb
|
@@ -153,6 +154,7 @@ files:
|
|
153
154
|
- spec/attributes_initializable_spec.rb
|
154
155
|
- spec/eval_helper/if_code_after_spec.rb
|
155
156
|
- spec/eval_helper/if_code_spec.rb
|
157
|
+
- spec/eval_helper/ternary_operator_spec.rb
|
156
158
|
- spec/eval_helper/unless_code_after_spec.rb
|
157
159
|
- spec/eval_helper/unless_code_spec.rb
|
158
160
|
- spec/ghostable_spec.rb
|
@@ -238,6 +240,7 @@ test_files:
|
|
238
240
|
- spec/attributes_initializable_spec.rb
|
239
241
|
- spec/eval_helper/if_code_after_spec.rb
|
240
242
|
- spec/eval_helper/if_code_spec.rb
|
243
|
+
- spec/eval_helper/ternary_operator_spec.rb
|
241
244
|
- spec/eval_helper/unless_code_after_spec.rb
|
242
245
|
- spec/eval_helper/unless_code_spec.rb
|
243
246
|
- spec/ghostable_spec.rb
|