tbpgr_utils 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +50 -2
- data/lib/ghostable.rb +28 -0
- data/lib/tbpgr_utils/version.rb +1 -1
- data/spec/ghostable_spec.rb +80 -0
- data/spec/spec_helper.rb +2 -0
- metadata +13 -10
data/README.md
CHANGED
@@ -18,7 +18,7 @@ Or install it yourself as:
|
|
18
18
|
|
19
19
|
## Usage
|
20
20
|
### List
|
21
|
-
|
|
21
|
+
| class/module/method| mean|
|
22
22
|
|:-----------|:------------|
|
23
23
|
|Array#together|loop all arrays by block|
|
24
24
|
|Object#any_of?|if self match any one of items, return true|
|
@@ -26,7 +26,8 @@ Or install it yourself as:
|
|
26
26
|
|Object#my_methods|return public/protected/private self define methods|
|
27
27
|
|String#justify_table|justify pipe format table string|
|
28
28
|
|AttributesInitializable::ClassMethods.attr_accessor_init|generate attr_accessors + initializer|
|
29
|
-
|Templatable|get result from template + placeholder|
|
29
|
+
|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)|
|
30
31
|
|
31
32
|
### Array#together
|
32
33
|
~~~ruby
|
@@ -183,7 +184,54 @@ line1:hoge-sample
|
|
183
184
|
line2:hige-sample
|
184
185
|
~~~
|
185
186
|
|
187
|
+
### Ghostable
|
188
|
+
* include Ghostable
|
189
|
+
* create ghost method by using Ghostable::ghost_method
|
190
|
+
* ghost_method first_args = method_name_pattern
|
191
|
+
* ghost_method second_args = method_base_name Symbol(using in Ghostable internal logic)
|
192
|
+
* ghost_method third = block. this block is main logic. block can use args[method_name, *args, &block]
|
193
|
+
|
194
|
+
sample ghost method define module.
|
195
|
+
~~~ruby
|
196
|
+
module Checkable
|
197
|
+
include Ghostable
|
198
|
+
ghost_method /check_range_.*\?$/, :check_range do |method_name, *args, &block|
|
199
|
+
method_name.to_s =~ /(check_range_)(\d+)(_to_)(\d*)/
|
200
|
+
from = $2.to_i
|
201
|
+
to = $4.to_i
|
202
|
+
value = args.first
|
203
|
+
(from..to).include? value
|
204
|
+
end
|
205
|
+
|
206
|
+
ghost_method /^contain_.*\?$/, :check_contain do |method_name, *args, &block|
|
207
|
+
method_name.to_s =~ /^(contain_)(.*)(\?)/
|
208
|
+
word = $2
|
209
|
+
value = args.first
|
210
|
+
value.include? word
|
211
|
+
end
|
212
|
+
end
|
213
|
+
~~~
|
214
|
+
|
215
|
+
* use ghost method
|
216
|
+
|
217
|
+
sample ghost method use class
|
218
|
+
~~~ruby
|
219
|
+
class SampleChecker
|
220
|
+
include Checkable
|
221
|
+
end
|
222
|
+
|
223
|
+
sample = SampleChecker.new
|
224
|
+
sample.check_range_3_to_5?(4) # => return true
|
225
|
+
sample.check_range_3_to_5?(6) # => return false
|
226
|
+
sample.check_range_3_to_6?(6) # => return true
|
227
|
+
|
228
|
+
sample.contain_hoge? "test_hoge_test" # => return true
|
229
|
+
sample.contain_hoge? "test_hige_test" # => return false
|
230
|
+
sample.contain_hige? "test_hige_test" # => return true
|
231
|
+
~~~
|
232
|
+
|
186
233
|
## History
|
234
|
+
* version 0.0.6 : add Ghostable
|
187
235
|
* version 0.0.5 : add Templatable
|
188
236
|
* version 0.0.4 : AttributesInitializable::ClassMethods.attr_accessor_init
|
189
237
|
* version 0.0.3 : add Object#any_of?
|
data/lib/ghostable.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "active_support/concern"
|
3
|
+
|
4
|
+
module Ghostable
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def ghost_method(pattern, basename, &ghost_block)
|
9
|
+
define_method basename do |method_name, *args, &block|
|
10
|
+
self.class.class_eval {ghost_block.call(method_name, *args, &block)}
|
11
|
+
end
|
12
|
+
|
13
|
+
@patterns ||= {}
|
14
|
+
@patterns[basename] = pattern
|
15
|
+
tmp_patterns = @patterns.dup
|
16
|
+
define_method :get_patterns do
|
17
|
+
tmp_patterns
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def method_missing(method_name, *args, &block)
|
23
|
+
get_patterns.each do |basename, pattern|
|
24
|
+
return method(basename).call(method_name, *args, &block) if method_name.to_s.match /#{pattern}/
|
25
|
+
end
|
26
|
+
super
|
27
|
+
end
|
28
|
+
end
|
data/lib/tbpgr_utils/version.rb
CHANGED
@@ -0,0 +1,80 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "spec_helper"
|
3
|
+
require "ghostable"
|
4
|
+
|
5
|
+
describe Ghostable do
|
6
|
+
context :ghost_method do
|
7
|
+
module Checkable
|
8
|
+
include Ghostable
|
9
|
+
ghost_method /check_range_.*\?$/, :check_range do |method_name, *args, &block|
|
10
|
+
method_name.to_s =~ /(check_range_)(\d+)(_to_)(\d*)/
|
11
|
+
from = $2.to_i
|
12
|
+
to = $4.to_i
|
13
|
+
value = args.first
|
14
|
+
(from..to).include? value
|
15
|
+
end
|
16
|
+
|
17
|
+
ghost_method /^contain_.*\?$/, :check_contain do |method_name, *args, &block|
|
18
|
+
method_name.to_s =~ /^(contain_)(.*)(\?)/
|
19
|
+
word = $2
|
20
|
+
value = args.first
|
21
|
+
value.include? word
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class SampleChecker
|
26
|
+
include Checkable
|
27
|
+
end
|
28
|
+
|
29
|
+
cases = [
|
30
|
+
{
|
31
|
+
case_no: 1,
|
32
|
+
case_title: "valid case",
|
33
|
+
klass: SampleChecker,
|
34
|
+
methods: [:check_range_3_to_5?, :check_range_3_to_5? ,:contain_hoge?, :contain_hoge?],
|
35
|
+
values: [3, 6, "testhogetest", "testhigetest"],
|
36
|
+
expecteds: [true, false, true, false],
|
37
|
+
},
|
38
|
+
{
|
39
|
+
case_no: 2,
|
40
|
+
case_title: "method_missing case",
|
41
|
+
klass: SampleChecker,
|
42
|
+
methods: [:not_match_method],
|
43
|
+
values: [nil],
|
44
|
+
expect_error: true
|
45
|
+
},
|
46
|
+
]
|
47
|
+
|
48
|
+
cases.each do |c|
|
49
|
+
it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
|
50
|
+
begin
|
51
|
+
case_before c
|
52
|
+
|
53
|
+
# -- given --
|
54
|
+
instance = c[:klass].new
|
55
|
+
|
56
|
+
c[:methods].each_with_index do |method_name, index|
|
57
|
+
if c[:expect_error]
|
58
|
+
lambda { instance.send(method_name, c[:values][index]) }.should raise_error(StandardError)
|
59
|
+
next
|
60
|
+
end
|
61
|
+
# -- when --
|
62
|
+
actual = instance.send(method_name, c[:values][index])
|
63
|
+
# -- then --
|
64
|
+
expect(actual).to eq(c[:expecteds][index])
|
65
|
+
end
|
66
|
+
ensure
|
67
|
+
case_after c
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def case_before(c)
|
72
|
+
# implement each case before
|
73
|
+
end
|
74
|
+
|
75
|
+
def case_after(c)
|
76
|
+
# implement each case after
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/spec/spec_helper.rb
CHANGED
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.6
|
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-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &28366236 !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: *28366236
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: bundler
|
27
|
-
requirement: &
|
27
|
+
requirement: &28365948 !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: *28365948
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rake
|
38
|
-
requirement: &
|
38
|
+
requirement: &28365720 !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: *28365720
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: rspec
|
49
|
-
requirement: &
|
49
|
+
requirement: &28365396 !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: *28365396
|
58
58
|
description: Utilities
|
59
59
|
email:
|
60
60
|
- tbpgr@tbpgr.jp
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- README.md
|
70
70
|
- Rakefile
|
71
71
|
- lib/attributes_initializable.rb
|
72
|
+
- lib/ghostable.rb
|
72
73
|
- lib/open_classes/array.rb
|
73
74
|
- lib/open_classes/object.rb
|
74
75
|
- lib/open_classes/string.rb
|
@@ -77,6 +78,7 @@ files:
|
|
77
78
|
- lib/templatable.rb
|
78
79
|
- spec/array_spec.rb
|
79
80
|
- spec/attributes_initializable_spec.rb
|
81
|
+
- spec/ghostable_spec.rb
|
80
82
|
- spec/object_spec.rb
|
81
83
|
- spec/spec_helper.rb
|
82
84
|
- spec/string_spec.rb
|
@@ -110,6 +112,7 @@ summary: Utilities
|
|
110
112
|
test_files:
|
111
113
|
- spec/array_spec.rb
|
112
114
|
- spec/attributes_initializable_spec.rb
|
115
|
+
- spec/ghostable_spec.rb
|
113
116
|
- spec/object_spec.rb
|
114
117
|
- spec/spec_helper.rb
|
115
118
|
- spec/string_spec.rb
|