tbpgr_utils 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -26,6 +26,7 @@ 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
30
 
30
31
  ### Array#together
31
32
  ~~~ruby
@@ -146,7 +147,44 @@ p atr_sample2.atr1 # => atr1
146
147
  p atr_sample2.atr2 # => atr2
147
148
  ~~~
148
149
 
150
+ ### Templatable
151
+ * include Templatable
152
+ * set template by here-document
153
+ * in template, parameter must name 'placeholders[:xxxxx]'. xxxxx is your favorite name.
154
+ * when create instance, you must set materials to create template. after, you can get this value from @materials.
155
+ * you must create manufactured_xxx methods. xxx is each-placeholder name.
156
+ * you can get result by 'result' method.
157
+
158
+ ~~~ruby
159
+ require 'templatable'
160
+
161
+ class TemplateUser
162
+ include Templatable
163
+ template <<-EOS
164
+ line1:<%=placeholders[:hoge]%>
165
+ line2:<%=placeholders[:hige]%>
166
+ EOS
167
+
168
+ def manufactured_hoge
169
+ "hoge-#{@materials}"
170
+ end
171
+
172
+ def manufactured_hige
173
+ "hige-#{@materials}"
174
+ end
175
+ end
176
+
177
+ p TemplateUser.new('sample').result
178
+ ~~~
179
+
180
+ output
181
+ ~~~
182
+ line1:hoge-sample
183
+ line2:hige-sample
184
+ ~~~
185
+
149
186
  ## History
187
+ * version 0.0.5 : add Templatable
150
188
  * version 0.0.4 : AttributesInitializable::ClassMethods.attr_accessor_init
151
189
  * version 0.0.3 : add Object#any_of?
152
190
  * version 0.0.2 : loop all arrays by block.
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Tbpgr Utilities
4
4
  module TbpgrUtils
5
- VERSION = '0.0.4'
5
+ VERSION = '0.0.5'
6
6
  end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ require "erb"
3
+ require "active_support"
4
+
5
+ module Templatable
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ def template(template)
10
+ define_method :get_template do
11
+ template
12
+ end
13
+
14
+ define_method :get_placeholders do
15
+ ret = template.scan /<%=placeholders\[:(.*)\]%>/
16
+ ret_hash = {}
17
+ ret.each {|v| ret_hash[v.first.to_sym] = v.first}
18
+ ret_hash
19
+ end
20
+ end
21
+ end
22
+
23
+ def initialize(materials)
24
+ @materials = materials
25
+ end
26
+
27
+ def result
28
+ placeholders = get_placeholders
29
+ placeholders.each do |key, value|
30
+ placeholders[key] = method("manufactured_#{value}").call
31
+ end
32
+ ERB.new(get_template).result(binding)
33
+ end
34
+ end
@@ -0,0 +1,65 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+ require "templatable"
4
+
5
+ describe Templatable do
6
+ context :result do
7
+ class TemplateUser
8
+ include Templatable
9
+ template <<-EOS
10
+ line1:<%=placeholders[:hoge]%>
11
+ line2:<%=placeholders[:hige]%>
12
+ EOS
13
+
14
+ def manufactured_hoge
15
+ "hoge-#{@materials}"
16
+ end
17
+
18
+ def manufactured_hige
19
+ "hige-#{@materials}"
20
+ end
21
+ end
22
+
23
+ RESULT_CASE1 = <<-EOS
24
+ line1:hoge-sample
25
+ line2:hige-sample
26
+ EOS
27
+
28
+ cases = [
29
+ {
30
+ case_no: 1,
31
+ case_title: "valid case",
32
+ klass: TemplateUser,
33
+ input: 'sample',
34
+ expected: RESULT_CASE1,
35
+ },
36
+ ]
37
+
38
+ cases.each do |c|
39
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
40
+ begin
41
+ case_before c
42
+
43
+ # -- given --
44
+ # nothing
45
+
46
+ # -- when --
47
+ actual = c[:klass].new(c[:input]).result
48
+
49
+ # -- then --
50
+ expect(actual).to eq(c[:expected])
51
+ ensure
52
+ case_after c
53
+ end
54
+ end
55
+
56
+ def case_before(c)
57
+ # implement each case before
58
+ end
59
+
60
+ def case_after(c)
61
+ # implement each case after
62
+ end
63
+ end
64
+ end
65
+ 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
4
+ version: 0.0.5
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-08 00:00:00.000000000 Z
12
+ date: 2014-01-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &28935036 !ruby/object:Gem::Requirement
16
+ requirement: &24536892 !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: *28935036
24
+ version_requirements: *24536892
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &28934616 !ruby/object:Gem::Requirement
27
+ requirement: &24536604 !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: *28934616
35
+ version_requirements: *24536604
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &28934316 !ruby/object:Gem::Requirement
38
+ requirement: &24536376 !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: *28934316
46
+ version_requirements: *24536376
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &28933788 !ruby/object:Gem::Requirement
49
+ requirement: &24536052 !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: *28933788
57
+ version_requirements: *24536052
58
58
  description: Utilities
59
59
  email:
60
60
  - tbpgr@tbpgr.jp
@@ -74,11 +74,13 @@ files:
74
74
  - lib/open_classes/string.rb
75
75
  - lib/tbpgr_utils.rb
76
76
  - lib/tbpgr_utils/version.rb
77
+ - lib/templatable.rb
77
78
  - spec/array_spec.rb
78
79
  - spec/attributes_initializable_spec.rb
79
80
  - spec/object_spec.rb
80
81
  - spec/spec_helper.rb
81
82
  - spec/string_spec.rb
83
+ - spec/templatable_spec.rb
82
84
  - tbpgr_utils.gemspec
83
85
  homepage: https://github.com/tbpgr/tbpgr_utils
84
86
  licenses:
@@ -111,4 +113,5 @@ test_files:
111
113
  - spec/object_spec.rb
112
114
  - spec/spec_helper.rb
113
115
  - spec/string_spec.rb
116
+ - spec/templatable_spec.rb
114
117
  has_rdoc: