tbpgr_utils 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +34 -0
- data/lib/open_classes/array.rb +23 -17
- data/lib/open_classes/kernel.rb +26 -0
- data/lib/open_classes/object.rb +51 -20
- data/lib/open_classes/string.rb +71 -57
- data/lib/tbpgr_utils.rb +1 -0
- data/lib/tbpgr_utils/version.rb +6 -6
- data/spec/kernel_spec.rb +106 -0
- metadata +13 -10
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -28,6 +28,8 @@ Or install it yourself as:
|
|
28
28
|
|AttributesInitializable::ClassMethods.attr_accessor_init|generate attr_accessors + initializer|
|
29
29
|
|Templatable module|get result from template + placeholder|
|
30
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|
|
31
33
|
|
32
34
|
### Array#together
|
33
35
|
~~~ruby
|
@@ -230,7 +232,39 @@ sample.contain_hoge? "test_hige_test" # => return false
|
|
230
232
|
sample.contain_hige? "test_hige_test" # => return true
|
231
233
|
~~~
|
232
234
|
|
235
|
+
### Kernel#print_eval
|
236
|
+
This method for sample code. for manual, for blog-entry's-snippet ...etc.
|
237
|
+
|
238
|
+
~~~ruby
|
239
|
+
print_eval 8/4, binding # => 8/4 # => 2
|
240
|
+
|
241
|
+
message = 'msg'
|
242
|
+
print_eval "hoge-#{message}", binding # => "hoge-#{message}" # => "hoge-msg"
|
243
|
+
~~~
|
244
|
+
|
245
|
+
output
|
246
|
+
~~~
|
247
|
+
8/4 # => 2"hoge-#{message}" # => "hoge-msg"
|
248
|
+
~~~
|
249
|
+
|
250
|
+
### Kernel#puts_eval
|
251
|
+
This method for sample code. for manual, for blog-entry's-snippet ...etc.
|
252
|
+
|
253
|
+
~~~ruby
|
254
|
+
puts_eval 8/4, binding
|
255
|
+
|
256
|
+
message = 'msg'
|
257
|
+
puts_eval "hoge-#{message}", binding # => "hoge-#{message}" # => "hoge-msg"
|
258
|
+
~~~
|
259
|
+
|
260
|
+
output
|
261
|
+
~~~
|
262
|
+
8/4 # => 2
|
263
|
+
"hoge-#{message}" # => "hoge-msg"
|
264
|
+
~~~
|
265
|
+
|
233
266
|
## History
|
267
|
+
* version 0.0.7 : add Kernel#print_eval, Kernel#puts_eval
|
234
268
|
* version 0.0.6 : add Ghostable
|
235
269
|
* version 0.0.5 : add Templatable
|
236
270
|
* version 0.0.4 : AttributesInitializable::ClassMethods.attr_accessor_init
|
data/lib/open_classes/array.rb
CHANGED
@@ -1,17 +1,23 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
#
|
4
|
-
class Array
|
5
|
-
#
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Array
|
4
|
+
class Array
|
5
|
+
# Arrays loop together
|
6
|
+
#
|
7
|
+
# alpha = %w{one two three}
|
8
|
+
# numbers = %w{1 2 3}
|
9
|
+
# [alpha, numbers].together do |first, second|
|
10
|
+
# print "#{first}:#{second}\n" # => output one:1, two:2, three:3
|
11
|
+
# end
|
12
|
+
def together
|
13
|
+
each { |f|fail TypeError, "you have to use [Array1, Array2, ...] | #{f.class} is invalid" unless f.class == Array }
|
14
|
+
first.each_with_index do |i_v, i|
|
15
|
+
eval_each = []
|
16
|
+
each_with_index do |j_v, j|
|
17
|
+
eval_each << "self[#{j}][#{i}]"
|
18
|
+
end
|
19
|
+
eval_each_str = eval_each.join(',')
|
20
|
+
instance_eval "yield(#{eval_each_str})"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Kernel
|
4
|
+
module Kernel
|
5
|
+
# Print code + eval result
|
6
|
+
#
|
7
|
+
# print_eval 8/4, binding # => 8/4 # => 2
|
8
|
+
# message = 'msg';print_eval "hoge-#{message}", binding # => "hoge-#{message}" # => "hoge-msg"
|
9
|
+
def print_eval(code, binding)
|
10
|
+
print exec_eval code, binding
|
11
|
+
end
|
12
|
+
|
13
|
+
# Puts code + eval result
|
14
|
+
#
|
15
|
+
# puts_eval 8/4, binding # => 8/4 # => 2\n
|
16
|
+
# message = 'msg';puts_eval "hoge-#{message}", binding # => "hoge-#{message}" # => "hoge-msg"\n
|
17
|
+
def puts_eval(code, binding)
|
18
|
+
puts exec_eval code, binding
|
19
|
+
end
|
20
|
+
|
21
|
+
def exec_eval(code, binding)
|
22
|
+
ret = eval code, binding
|
23
|
+
"#{code} # => #{ret.inspect}"
|
24
|
+
end
|
25
|
+
private :exec_eval
|
26
|
+
end
|
data/lib/open_classes/object.rb
CHANGED
@@ -1,20 +1,51 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
|
4
|
-
class Object
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Object
|
4
|
+
class Object
|
5
|
+
# Check boolean type
|
6
|
+
#
|
7
|
+
# boolean? true # => true
|
8
|
+
# boolean? false # => true
|
9
|
+
# boolean? nil # => false
|
10
|
+
# boolean? 'true' # => false
|
11
|
+
# boolean? 'false' # => false
|
12
|
+
# boolean? '' # => false
|
13
|
+
def boolean?
|
14
|
+
self.is_a?(TrueClass) || self.is_a?(FalseClass)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Get self define methods.
|
18
|
+
#
|
19
|
+
# class SampleClass < String
|
20
|
+
# def public_hello
|
21
|
+
# "public hello"
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# protected
|
25
|
+
#
|
26
|
+
# def protected_hello
|
27
|
+
# "protected hello"
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# private
|
31
|
+
#
|
32
|
+
# def private_hello
|
33
|
+
# "private hello"
|
34
|
+
# end
|
35
|
+
# end
|
36
|
+
#
|
37
|
+
# SampleClass.new.my_methods # => [:public_hello, :protected_hello, :private_hello]
|
38
|
+
def my_methods
|
39
|
+
public_methods(false) + protected_methods(false) + private_methods(false)
|
40
|
+
end
|
41
|
+
|
42
|
+
# If self match any one of args, return true.
|
43
|
+
#
|
44
|
+
# "hoge".any_of? %w{hoge hige} # => true
|
45
|
+
# "hige".any_of? %w{hoge hige} # => true
|
46
|
+
# "hege".any_of? %w{hoge hige} # => false
|
47
|
+
def any_of?(*args)
|
48
|
+
args.each {|value|return true if self == value}
|
49
|
+
false
|
50
|
+
end
|
51
|
+
end
|
data/lib/open_classes/string.rb
CHANGED
@@ -1,57 +1,71 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
#
|
4
|
-
class String
|
5
|
-
#
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
max_sizes
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# String
|
4
|
+
class String
|
5
|
+
# Justify pipe using table format
|
6
|
+
#
|
7
|
+
# before justify
|
8
|
+
#
|
9
|
+
# |* first name|* family name|
|
10
|
+
# |eiichiro|oda|
|
11
|
+
# |akira|toriyama|
|
12
|
+
# |yusei|matsui|
|
13
|
+
#
|
14
|
+
# after justify
|
15
|
+
#
|
16
|
+
# |* first name|* family name|
|
17
|
+
# |eiichiro |oda |
|
18
|
+
# |akira |toriyama |
|
19
|
+
# |yusei |matsui |
|
20
|
+
def justify_table
|
21
|
+
return self if self.empty?
|
22
|
+
max_sizes = get_column_maxes
|
23
|
+
return self if max_sizes.nil?
|
24
|
+
justify_lines max_sizes
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def get_column_maxes
|
30
|
+
max_sizes = []
|
31
|
+
each_line do |line|
|
32
|
+
return nil unless table? line
|
33
|
+
columns = get_columuns(line)
|
34
|
+
max_sizes = get_column_max(columns, max_sizes)
|
35
|
+
end
|
36
|
+
max_sizes
|
37
|
+
end
|
38
|
+
|
39
|
+
def justify_lines(max_sizes)
|
40
|
+
ret = []
|
41
|
+
each_line do |line|
|
42
|
+
columns = get_columuns(line)
|
43
|
+
line_ret = []
|
44
|
+
columns.each_with_index do |column, cnt|
|
45
|
+
line_ret << column.ljust(max_sizes[cnt])
|
46
|
+
end
|
47
|
+
ret << "|#{line_ret.join('|')}|"
|
48
|
+
end
|
49
|
+
ret.join("\n") + "\n"
|
50
|
+
end
|
51
|
+
|
52
|
+
def get_columuns(line)
|
53
|
+
line.split('|')[1..-2]
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_column_max(columns, max_sizes)
|
57
|
+
columns.each_with_index do |column, index|
|
58
|
+
current_size = column.size
|
59
|
+
if max_sizes[index].nil?
|
60
|
+
max_sizes << current_size
|
61
|
+
next
|
62
|
+
end
|
63
|
+
max_sizes[index] = current_size if current_size > max_sizes[index]
|
64
|
+
end
|
65
|
+
max_sizes
|
66
|
+
end
|
67
|
+
|
68
|
+
def table?(text)
|
69
|
+
text.count('|') > 0
|
70
|
+
end
|
71
|
+
end
|
data/lib/tbpgr_utils.rb
CHANGED
data/lib/tbpgr_utils/version.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
# Tbpgr Utilities
|
4
|
-
module TbpgrUtils
|
5
|
-
VERSION = '0.0.
|
6
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Tbpgr Utilities
|
4
|
+
module TbpgrUtils
|
5
|
+
VERSION = '0.0.7'
|
6
|
+
end
|
data/spec/kernel_spec.rb
ADDED
@@ -0,0 +1,106 @@
|
|
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
|
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.7
|
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-
|
12
|
+
date: 2014-01-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &29606124 !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: *29606124
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &29605836 !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: *29605836
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &29605608 !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: *29605608
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &29605284 !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: *
|
57
|
+
version_requirements: *29605284
|
58
58
|
description: Utilities
|
59
59
|
email:
|
60
60
|
- tbpgr@tbpgr.jp
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- lib/attributes_initializable.rb
|
72
72
|
- lib/ghostable.rb
|
73
73
|
- lib/open_classes/array.rb
|
74
|
+
- lib/open_classes/kernel.rb
|
74
75
|
- lib/open_classes/object.rb
|
75
76
|
- lib/open_classes/string.rb
|
76
77
|
- lib/tbpgr_utils.rb
|
@@ -79,6 +80,7 @@ files:
|
|
79
80
|
- spec/array_spec.rb
|
80
81
|
- spec/attributes_initializable_spec.rb
|
81
82
|
- spec/ghostable_spec.rb
|
83
|
+
- spec/kernel_spec.rb
|
82
84
|
- spec/object_spec.rb
|
83
85
|
- spec/spec_helper.rb
|
84
86
|
- spec/string_spec.rb
|
@@ -113,6 +115,7 @@ test_files:
|
|
113
115
|
- spec/array_spec.rb
|
114
116
|
- spec/attributes_initializable_spec.rb
|
115
117
|
- spec/ghostable_spec.rb
|
118
|
+
- spec/kernel_spec.rb
|
116
119
|
- spec/object_spec.rb
|
117
120
|
- spec/spec_helper.rb
|
118
121
|
- spec/string_spec.rb
|