templatable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ tudu/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+ gem "rspec", "~> 2.14.1"
5
+ gem "thor", "~> 0.18.1"
6
+ gem "simplecov", "~> 0.8.2"
7
+ gem "activesupport", "~> 4.0.1"
8
+ gem "activemodel", "~> 4.0.2"
9
+ gem "tudu", "~> 0.0.4"
10
+ gem "tbpgr_utils", "~> 0.0.5"
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 tbpgr
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,149 @@
1
+ # Templatable
2
+
3
+ Templatable is CLI tool that generate ruby-source-code using ERB template.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'templatable'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install templatable
18
+
19
+ ## Usage
20
+ ### before execute
21
+ templatable use 'templatable module' that is provided by 'tbpgr_utils' gem.
22
+
23
+ so, you must install 'tbpgr_utils' gem(=>0.0.5), before use this gem.
24
+
25
+ ### generate Templatablefile
26
+ * templatable init
27
+
28
+ output Templatablefile
29
+ ~~~ruby
30
+ # encoding: utf-8
31
+
32
+ # output_fullpath
33
+ # output_fullpath is required
34
+ # output_fullpath allow only String
35
+ # output_fullpath's default value => "output_fullpath"
36
+ output_fullpath "output_fullpath"
37
+
38
+ # class_name
39
+ # class_name is required
40
+ # class_name allow only String
41
+ # class_name's default value => "class_name"
42
+ class_name "class_name"
43
+
44
+ TEMPLATE =<<-EOS
45
+ TODO: set your template(ERB format)
46
+ EOS
47
+
48
+ # template
49
+ # template is required
50
+ # template allow only String
51
+ # template's default value => "TODO: set your template(ERB format)"
52
+ template TEMPLATE
53
+
54
+ # placeholders
55
+ # placeholders is required
56
+ # placeholders allow only String-Array
57
+ # placeholders's default value => []
58
+ placeholders []
59
+ ~~~
60
+
61
+ ### edit Templatablefile manually
62
+ ~~~ruby
63
+ # encoding: utf-8
64
+ output_fullpath "./sample_use.rb"
65
+
66
+ class_name "sample_use"
67
+
68
+ TEMPLATE =<<-EOS
69
+ line1:<%=param1%>
70
+ line2:<%=param2%>
71
+ EOS
72
+
73
+ template TEMPLATE
74
+
75
+ placeholders ["param1", "param2"]
76
+ ~~~
77
+
78
+ ### generate templatable class
79
+ * templatable execute
80
+
81
+ sample_use.rb
82
+ ~~~ruby
83
+ # encoding: utf-8
84
+ require 'templatable'
85
+
86
+ class SampleUse
87
+ include Templatable
88
+ template <<-EOS
89
+ line1:<%=placeholders[:param1]%>
90
+ line2:<%=placeholders[:param2]%>
91
+ EOS
92
+
93
+ def manufactured_param1
94
+ # TODO: implement your logic
95
+ end
96
+
97
+ def manufactured_param2
98
+ # TODO: implement your logic
99
+ end
100
+ end
101
+ ~~~
102
+
103
+ ### edit your logic
104
+ sample_use.rb
105
+ ~~~ruby
106
+ require 'templatable'
107
+
108
+ class SampleUse
109
+ include Templatable
110
+ template <<-EOS
111
+ line1:<%=placeholders[:param1]%>
112
+ line2:<%=placeholders[:param2]%>
113
+ EOS
114
+
115
+ def manufactured_param1
116
+ "ret1-#{@materials}"
117
+ end
118
+
119
+ def manufactured_param2
120
+ "ret2-#{@materials}"
121
+ end
122
+ end
123
+ ~~~
124
+
125
+ ## use your class
126
+ ~~~ruby
127
+ puts SampleUse.new("input").result
128
+ ~~~
129
+
130
+ output
131
+ ~~~
132
+ line1:ret1-input
133
+ line2:ret2-input
134
+ ~~~
135
+
136
+ ## Notes
137
+ * Templatable output code use 'tbpgr_utils' gem(=>0.0.5).
138
+ * if you want to generate RSpec-spec for this class, use 'rspec_piccolo' gem.
139
+
140
+ ## History
141
+ * version 0.0.1 : first release.
142
+
143
+ ## Contributing
144
+
145
+ 1. Fork it
146
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
147
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
148
+ 4. Push to the branch (`git push origin my-new-feature`)
149
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require "templatable_core"
5
+ require "templatable/version"
6
+ require "thor"
7
+
8
+ module Templatable
9
+ #= Templatable CLI
10
+ class CLI < Thor
11
+ class_option :help, :type => :boolean, :aliases => '-h', :desc => 'help message.'
12
+ class_option :version, :type => :boolean, :desc => 'version'
13
+
14
+ desc "execute", "generate parameter-template apply class."
15
+ def execute
16
+ Templatable::Core.new.execute
17
+ end
18
+
19
+ desc "init", "generate Templatablefile"
20
+ def init
21
+ Templatable::Core.new.init
22
+ end
23
+
24
+ desc "version", "version"
25
+ def version
26
+ p Templatable::VERSION
27
+ end
28
+ end
29
+ end
30
+
31
+ Templatable::CLI.start(ARGV)
@@ -0,0 +1,3 @@
1
+ module Templatable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,97 @@
1
+ # encoding: utf-8
2
+ require 'templatable_dsl'
3
+ require "active_support"
4
+ require "erb"
5
+
6
+ module Templatable
7
+ # Templatable Core
8
+ class Core
9
+ TEMPLATABLE_FILE = "Templatablefile"
10
+ TEMPLATABLE_TEMPLATE =<<-EOF
11
+ # encoding: utf-8
12
+
13
+ # output_fullpath
14
+ # output_fullpath is required
15
+ # output_fullpath allow only String
16
+ # output_fullpath's default value => "output_fullpath"
17
+ output_fullpath "output_fullpath"
18
+
19
+ # class_name
20
+ # class_name is required
21
+ # class_name allow only String
22
+ # class_name's default value => "class_name"
23
+ class_name "class_name"
24
+
25
+ TEMPLATE =<<-EOS
26
+ TODO: set your template(ERB format)
27
+ EOS
28
+
29
+ # template
30
+ # template is required
31
+ # template allow only String
32
+ # template's default value => "TODO: set your template(ERB format)"
33
+ template TEMPLATE
34
+
35
+ # placeholders
36
+ # placeholders is required
37
+ # placeholders allow only String-Array
38
+ # placeholders's default value => []
39
+ placeholders []
40
+ EOF
41
+
42
+ CLASS_TEMPLATE = <<-EOF
43
+ # encoding: utf-8
44
+ require 'templatable'
45
+
46
+ class <%=class_name%>
47
+ include Templatable
48
+ template <<-EOS
49
+ <%=template%>
50
+ EOS
51
+
52
+ <%=methods%>
53
+ end
54
+ EOF
55
+
56
+ # generate Templatablefile to current directory.
57
+ def init
58
+ File.open(TEMPLATABLE_FILE, "w") {|f|f.puts TEMPLATABLE_TEMPLATE}
59
+ end
60
+
61
+ # generate TemplateApplayClass
62
+ def execute
63
+ src = read_dsl
64
+ dsl = Templatable::Dsl.new
65
+ dsl.instance_eval src
66
+ template = get_template dsl
67
+ methods = get_methods dsl
68
+ output_src = get_templatable_class_code(dsl.templatable.class_name.camelize, template.chop, methods)
69
+ File.open(dsl.templatable.output_fullpath, "w:UTF-8") {|f|f.print output_src}
70
+ end
71
+
72
+ private
73
+ def read_dsl
74
+ File.open(TEMPLATABLE_FILE) {|f|f.read}
75
+ end
76
+
77
+ def get_template(dsl)
78
+ dsl.templatable.template.gsub(/<%=/, "<%=placeholders\[:").gsub(/%>/, "\]%>")
79
+ end
80
+
81
+ def get_methods(dsl)
82
+ ret = []
83
+ dsl.templatable.placeholders.each do |placeholder|
84
+ ret << " def manufactured_#{placeholder}"
85
+ ret << " # TODO: implement your logic"
86
+ ret << " end"
87
+ ret << ""
88
+ end
89
+ ret.join("\n").chop
90
+ end
91
+
92
+ def get_templatable_class_code(class_name, template, methods)
93
+ erb = ERB.new(CLASS_TEMPLATE)
94
+ erb.result(binding)
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ require 'templatable_dsl_model'
3
+
4
+ module Templatable
5
+ class Dsl
6
+ attr_accessor :templatable
7
+
8
+ # String Define
9
+ [:output_fullpath, :class_name, :template].each do |f|
10
+ define_method f do |value|
11
+ eval "@templatable.#{f.to_s} = '#{value}'", binding
12
+ end
13
+ end
14
+
15
+ # Array/Hash/Boolean Define
16
+ [:placeholders].each do |f|
17
+ define_method f do |value|
18
+ eval "@templatable.#{f.to_s} = #{value}", binding
19
+ end
20
+ end
21
+
22
+ def initialize
23
+ @templatable = Templatable::DslModel.new
24
+ @templatable.output_fullpath = 'output_fullpath'
25
+ @templatable.class_name = 'class_name'
26
+ @templatable.template = 'TODO: set your template(ERB format)'
27
+ @templatable.placeholders = []
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ require 'active_model'
3
+
4
+ module Templatable
5
+ class DslModel
6
+ include ActiveModel::Model
7
+
8
+ # output_fullpath
9
+ attr_accessor :output_fullpath
10
+ validates :output_fullpath, :presence => true
11
+
12
+ # class_name
13
+ attr_accessor :class_name
14
+ validates :class_name, :presence => true
15
+
16
+ # template
17
+ attr_accessor :template
18
+ validates :template, :presence => true
19
+
20
+ # placeholders
21
+ attr_accessor :placeholders
22
+ validates :placeholders, :presence => true
23
+
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ require "simplecov"
2
+ SimpleCov.start
3
+ RSpec.configure do |config|
4
+ config.treat_symbols_as_metadata_keys_with_true_values = true
5
+ config.run_all_when_everything_filtered = true
6
+ config.filter_run :focus
7
+ end
@@ -0,0 +1,131 @@
1
+ # encoding: utf-8
2
+ require "spec_helper"
3
+ require "templatable_core"
4
+
5
+ describe Templatable::Core do
6
+
7
+ context :init do
8
+ OUTPUT_DSL_TMP_DIR = 'generate_dsl'
9
+ cases = [
10
+ {
11
+ case_no: 1,
12
+ case_title: 'valid case',
13
+ expected_file: Templatable::Core::TEMPLATABLE_FILE,
14
+ expected_content: Templatable::Core::TEMPLATABLE_TEMPLATE,
15
+ },
16
+ ]
17
+
18
+ cases.each do |c|
19
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
20
+ begin
21
+ case_before c
22
+
23
+ # -- given --
24
+ templatable_core = Templatable::Core.new
25
+
26
+ # -- when --
27
+ templatable_core.init
28
+
29
+ # -- then --
30
+ actual = File.read("./#{c[:expected_file]}")
31
+ expect(actual).to eq(c[:expected_content])
32
+ ensure
33
+ case_after c
34
+ end
35
+ end
36
+
37
+ def case_before(c)
38
+ Dir.mkdir(OUTPUT_DSL_TMP_DIR) unless Dir.exists? OUTPUT_DSL_TMP_DIR
39
+ Dir.chdir(OUTPUT_DSL_TMP_DIR)
40
+ end
41
+
42
+ def case_after(c)
43
+ Dir.chdir('../')
44
+ FileUtils.rm_rf(OUTPUT_DSL_TMP_DIR) if Dir.exists? OUTPUT_DSL_TMP_DIR
45
+ end
46
+ end
47
+ end
48
+
49
+ context :execute do
50
+ OUTPUT_TEMPLATABLE_TMP_DIR = 'tmp_templatable'
51
+ TEMPLATABLE_CASE1 =<<-EOF
52
+ # encoding: utf-8
53
+ output_fullpath "./sample_use.rb"
54
+
55
+ class_name "sample_use"
56
+
57
+ TEMPLATE =<<-EOS
58
+ line1:<%=param1%>
59
+ line2:<%=param2%>
60
+ EOS
61
+
62
+ template TEMPLATE
63
+
64
+ placeholders ["param1", "param2"]
65
+ EOF
66
+
67
+ RESULT_CASE1 =<<-EOF
68
+ # encoding: utf-8
69
+ require 'templatable'
70
+
71
+ class SampleUse
72
+ include Templatable
73
+ template <<-EOS
74
+ line1:<%=placeholders[:param1]%>
75
+ line2:<%=placeholders[:param2]%>
76
+ EOS
77
+
78
+ def manufactured_param1
79
+ # TODO: implement your logic
80
+ end
81
+
82
+ def manufactured_param2
83
+ # TODO: implement your logic
84
+ end
85
+ end
86
+ EOF
87
+
88
+ cases = [
89
+ {
90
+ case_no: 1,
91
+ case_title: "valid case",
92
+ output_file: "sample_use.rb",
93
+ templatablefile: TEMPLATABLE_CASE1,
94
+ expected: RESULT_CASE1,
95
+ },
96
+ ]
97
+
98
+ cases.each do |c|
99
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
100
+ begin
101
+ case_before c
102
+
103
+ # -- given --
104
+ templatable_core = Templatable::Core.new
105
+
106
+ # -- when --
107
+ templatable_core.execute
108
+
109
+ # -- then --
110
+ actual_exists = File.exists? c[:output_file]
111
+ expect(actual_exists).to be_true
112
+ actual_contents = File.read c[:output_file]
113
+ expect(actual_contents).to eq(c[:expected])
114
+ ensure
115
+ case_after c
116
+ end
117
+ end
118
+
119
+ def case_before(c)
120
+ Dir.mkdir(OUTPUT_TEMPLATABLE_TMP_DIR) unless Dir.exists? OUTPUT_TEMPLATABLE_TMP_DIR
121
+ Dir.chdir(OUTPUT_TEMPLATABLE_TMP_DIR)
122
+ File.open(Templatable::Core::TEMPLATABLE_FILE, 'w') { |f|f.print c[:templatablefile] }
123
+ end
124
+
125
+ def case_after(c)
126
+ Dir.chdir('../')
127
+ FileUtils.rm_rf(OUTPUT_TEMPLATABLE_TMP_DIR) if Dir.exists? OUTPUT_TEMPLATABLE_TMP_DIR
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'templatable/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "templatable"
8
+ spec.version = Templatable::VERSION
9
+ spec.authors = ["tbpgr"]
10
+ spec.email = ["tbpgr@tbpgr.jp"]
11
+ spec.description = %q{Templatable is CLI tool that generate ruby-source-code using ERB template.}
12
+ spec.summary = %q{Templatable is CLI tool that generate ruby-source-code using ERB template.}
13
+ spec.homepage = "https://github.com/tbpgr/templatable"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "thor", "~> 0.18.1"
22
+ spec.add_runtime_dependency "activesupport", "~> 4.0.1"
23
+ spec.add_runtime_dependency "activemodel", "~> 4.0.2"
24
+ spec.add_runtime_dependency "tbpgr_utils", "~> 0.0.5"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.3"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "simplecov"
29
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: templatable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - tbpgr
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: thor
16
+ requirement: &27587208 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.18.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *27587208
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &27586908 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 4.0.1
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *27586908
36
+ - !ruby/object:Gem::Dependency
37
+ name: activemodel
38
+ requirement: &27586632 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 4.0.2
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *27586632
47
+ - !ruby/object:Gem::Dependency
48
+ name: tbpgr_utils
49
+ requirement: &27586356 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.5
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *27586356
58
+ - !ruby/object:Gem::Dependency
59
+ name: bundler
60
+ requirement: &27586080 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: '1.3'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *27586080
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: &27585852 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *27585852
80
+ - !ruby/object:Gem::Dependency
81
+ name: simplecov
82
+ requirement: &27585576 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *27585576
91
+ description: Templatable is CLI tool that generate ruby-source-code using ERB template.
92
+ email:
93
+ - tbpgr@tbpgr.jp
94
+ executables:
95
+ - templatable
96
+ extensions: []
97
+ extra_rdoc_files: []
98
+ files:
99
+ - .gitignore
100
+ - .rspec
101
+ - Gemfile
102
+ - LICENSE.txt
103
+ - README.md
104
+ - Rakefile
105
+ - bin/templatable
106
+ - lib/templatable/version.rb
107
+ - lib/templatable_core.rb
108
+ - lib/templatable_dsl.rb
109
+ - lib/templatable_dsl_model.rb
110
+ - spec/spec_helper.rb
111
+ - spec/templatable_core_spec.rb
112
+ - templatable.gemspec
113
+ homepage: https://github.com/tbpgr/templatable
114
+ licenses:
115
+ - MIT
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ! '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 1.8.11
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Templatable is CLI tool that generate ruby-source-code using ERB template.
138
+ test_files:
139
+ - spec/spec_helper.rb
140
+ - spec/templatable_core_spec.rb
141
+ has_rdoc: