templator 0.1 → 0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,9 @@
1
1
  module Templator
2
2
 
3
- class ParameterCodeLoader
3
+ class ParameterFileSelector
4
4
 
5
- def self.load_code_from(*paths)
5
+ def self.select_parameter_files(*paths)
6
6
  files = get_candidate_files paths
7
- code = concatenate_content_of files
8
7
  end
9
8
 
10
9
  private
@@ -36,16 +35,5 @@ module Templator
36
35
  File.file? file
37
36
  end
38
37
  end
39
-
40
- # Concatenates the content of the given files.
41
- # @param [Array<String>] files array of files to process
42
- # @return [String] the content concatenated of all given files
43
- def self.concatenate_content_of(files)
44
- files.inject("") do |content, file|
45
- content += File.read(file)
46
- content += "\n" unless content[-1, 1] == "\n"
47
- content
48
- end
49
- end
50
38
  end
51
39
  end
@@ -1,5 +1,5 @@
1
1
  require 'rubygems'
2
- require 'templator/parameter_code_loader'
2
+ require 'templator/parameter_file_selector'
3
3
  require 'templator/parameter_dsl'
4
4
 
5
5
  module Templator
@@ -18,24 +18,24 @@ module Templator
18
18
  # to parameters defined in loaded files.
19
19
  def self.load_files(*paths)
20
20
 
21
- code = ParameterCodeLoader.load_code_from(*paths)
21
+ files = ParameterFileSelector.select_parameter_files(*paths)
22
22
 
23
23
  parameters = Parameters.new
24
- parameters.load(code)
24
+ parameters.load(files)
25
25
  return parameters
26
26
  end
27
27
 
28
28
  # Retrieves the value of a variable
29
- # defined in the parameter files previously loaded
29
+ # defined in the parameter files previously loaded.
30
30
  # @param [#to_s] var the fully qualified name of the variable (in dot notation)
31
31
  def get(var)
32
32
  var.to_s.split('.').inject(@parameters) {|result, element| result.send(element)}
33
33
  end
34
34
 
35
- # Loads code in a fresh context
36
- # @param [String ] code code to load
37
- def load(code)
38
- @parameters = Templator::ParameterDsl.new.parse(code)
35
+ # Loads parameters from provided files.
36
+ # @param [Array<String>] files files to load.
37
+ def load(files)
38
+ @parameters = Templator::ParameterFileLoader.new.parse(*files)
39
39
  end
40
40
  end
41
41
  end
@@ -0,0 +1,3 @@
1
+ module Templator
2
+ VERSION=0.2
3
+ end
@@ -30,7 +30,7 @@ module Templator
30
30
 
31
31
  describe "#param" do
32
32
 
33
- context ", when no context is provided," do
33
+ context "when no context is provided," do
34
34
  it "should retrieve from the #parameters instance the value of the provided parameter name" do
35
35
 
36
36
  parameters = mock(:parameters)
@@ -43,7 +43,7 @@ module Templator
43
43
  end
44
44
  end
45
45
 
46
- context ", when a context is defined in the includer" do
46
+ context "when a context is defined in the includer" do
47
47
 
48
48
  before do
49
49
  @parameters = mock(:parameters)
@@ -2,7 +2,15 @@ require 'templator/parameter_dsl'
2
2
 
3
3
  module Templator
4
4
 
5
- describe ParameterDsl do
5
+ describe ParameterFileLoader do
6
+
7
+ after(:all) do
8
+ File.delete("test")
9
+ File.delete("test1")
10
+ File.delete("test2")
11
+ end
12
+
13
+ subject {ParameterFileLoader.new}
6
14
 
7
15
  describe "#parse" do
8
16
 
@@ -10,12 +18,11 @@ module Templator
10
18
 
11
19
  it "should return an object that allows to access the parameter value in a natural way" do
12
20
 
13
- code =<<-CODE
21
+ in_file "test", <<-CODE
14
22
  export :var1 => "value1"
15
23
  CODE
16
24
 
17
- pdsl = ParameterDsl.new
18
- group = pdsl.parse(code)
25
+ group = subject.parse("test")
19
26
 
20
27
  group.var1.should == "value1"
21
28
  end
@@ -24,14 +31,14 @@ module Templator
24
31
  context "when the DSL defines a group and a parameter inside this group" do
25
32
 
26
33
  it "should return an object that allows to access the parameter value in a natural way" do
27
- code = <<-CODE
34
+
35
+ in_file("test", <<-CODE)
28
36
  group "group1" do
29
37
  export :var2 => "value2"
30
38
  end
31
39
  CODE
32
40
 
33
- pdsl = ParameterDsl.new
34
- group = pdsl.parse(code)
41
+ group = subject.parse("test")
35
42
 
36
43
  group.group1.var2.should == "value2"
37
44
  end
@@ -40,7 +47,7 @@ module Templator
40
47
  context "when the DSL defines a parameter for which the value is a parameter from another group" do
41
48
 
42
49
  it "should return an object that allows to access the parameter value in a natural way" do
43
- code = <<-CODE
50
+ in_file("test", <<-CODE)
44
51
  group "group3" do
45
52
  export :var3 => "value3"
46
53
  end
@@ -51,8 +58,7 @@ module Templator
51
58
  CODE
52
59
 
53
60
 
54
- pdsl = ParameterDsl.new
55
- group = pdsl.parse(code)
61
+ group = subject.parse("test")
56
62
 
57
63
  group.group4.var4.should == group.group3.var3
58
64
  end
@@ -75,14 +81,13 @@ module Templator
75
81
 
76
82
  it "should include in the current group all parameters and sub groups defined in the source group" do
77
83
 
78
- my_code = code + <<-CODE
84
+ in_file("test", code + <<-CODE)
79
85
  group :target do
80
86
  include_group "source"
81
87
  end
82
88
  CODE
83
89
 
84
- pdsl = ParameterDsl.new
85
- group = pdsl.parse(my_code)
90
+ group = subject.parse("test")
86
91
 
87
92
  group.target.subgroup.var_in_subgroup.should == "value_in_subgroup"
88
93
  group.target.var_in_group.should == "value_in_group"
@@ -93,14 +98,13 @@ module Templator
93
98
  context "and the group to include is given as a Group" do
94
99
 
95
100
  it "should include in the current group all parameters and sub groups defined in the source group" do
96
- my_code = code + <<-CODE
101
+ in_file("test", code + <<-CODE)
97
102
  group :target do
98
103
  include_group source
99
104
  end
100
105
  CODE
101
106
 
102
- pdsl = ParameterDsl.new
103
- group = pdsl.parse(my_code)
107
+ group = subject.parse("test")
104
108
 
105
109
  group.target.subgroup.var_in_subgroup.should == "value_in_subgroup"
106
110
  group.target.var_in_group.should == "value_in_group"
@@ -111,7 +115,7 @@ module Templator
111
115
  context "when a group is defined twice" do
112
116
 
113
117
  it "should merge the definition of each group into a single one" do
114
- my_code = <<-CODE
118
+ in_file("test", <<-CODE)
115
119
  group :group1 do
116
120
  export :var1 => "value1"
117
121
  end
@@ -121,13 +125,50 @@ module Templator
121
125
  end
122
126
  CODE
123
127
 
124
- pdsl = ParameterDsl.new
125
- group = pdsl.parse(my_code)
128
+ group = subject.parse("test")
129
+
130
+ group.group1.var1.should == "value1"
131
+ group.group1.var2.should == "value2"
132
+ end
133
+ end
134
+
135
+ context "when parameters are defined in several files" do
136
+
137
+ it "should correctly load all parameters" do
138
+
139
+ in_file("test1", <<-CODE)
140
+ group :group1 do
141
+ export :var1 => "value1"
142
+ end
143
+ CODE
144
+
145
+ in_file("test2", <<-CODE)
146
+ group :group1 do
147
+ export :var2 => "value2"
148
+ end
149
+ CODE
126
150
 
151
+ group = subject.parse("test1", "test2")
127
152
  group.group1.var1.should == "value1"
128
153
  group.group1.var2.should == "value2"
129
154
  end
130
155
  end
156
+
157
+ context "when parameter file contains syntax error" do
158
+ it "should display a clear error message" do
159
+ in_file("test1", <<-CODE)
160
+ groupe :wrong_syntax do
161
+ export :var1 => "value1"
162
+ end
163
+ CODE
164
+
165
+ expect {group = subject.parse("test1")}.to raise_error {|error|
166
+ error.should be_a(ParseError)
167
+ error.line.should == 1
168
+ error.file.should == "test1"
169
+ }
170
+ end
171
+ end
131
172
  end
132
173
  end
133
174
  end
@@ -0,0 +1,29 @@
1
+ require 'templator/parameter_file_selector'
2
+ require 'spec_helper'
3
+
4
+
5
+ module Templator
6
+
7
+ describe "ParameterFileSelector" do
8
+
9
+ describe "#select_parameter_files" do
10
+
11
+ it "should select the given file" do
12
+
13
+ path = File.join(parameter_dir_path, 'parameter1')
14
+
15
+ file = ParameterFileSelector.select_parameter_files(path)
16
+
17
+ file.should == [path]
18
+
19
+
20
+ end
21
+
22
+ it "should load the code from all the files inside the given directory" do
23
+ files = ParameterFileSelector.select_parameter_files(parameter_dir_path)
24
+ files.sort.should == Dir["#{parameter_dir_path}/*"].sort
25
+ end
26
+ end
27
+
28
+ end
29
+ end
@@ -8,19 +8,15 @@ module Templator
8
8
 
9
9
  describe "#load_files" do
10
10
 
11
+ it "should load the given file and return a Parameters instance" do
12
+ path = File.join(parameter_dir_path, 'parameter1')
11
13
 
12
- context "parameter files that contain only simple parameter definitions" do
14
+ parameters = Parameters.load_files(path)
13
15
 
14
- it "should load the given file and return a Parameters instance" do
15
- path = File.join(parameter_dir_path, 'parameter1')
16
+ parameters.should be_kind_of Parameters
16
17
 
17
- parameters = Parameters.load_files(path)
18
-
19
- parameters.should be_kind_of Parameters
20
-
21
- parameters.get(:parameter1).should == 'value1'
22
- parameters.get("group1.parameter2").should == 'value2'
23
- end
18
+ parameters.get(:parameter1).should == 'value1'
19
+ parameters.get("group1.parameter2").should == 'value2'
24
20
  end
25
21
  end
26
22
  end
metadata CHANGED
@@ -1,256 +1,256 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: templator
3
- version: !ruby/object:Gem::Version
4
- hash: 9
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- version: "0.1"
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Christophe Arguel
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2011-12-19 00:00:00 Z
18
- dependencies:
19
- - !ruby/object:Gem::Dependency
12
+ date: 2012-12-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
20
15
  name: bundler
21
- prerelease: false
22
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
23
17
  none: false
24
- requirements:
18
+ requirements:
25
19
  - - ~>
26
- - !ruby/object:Gem::Version
27
- hash: 15
28
- segments:
29
- - 1
30
- - 0
31
- version: "1.0"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
32
22
  type: :development
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: rake
36
23
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
38
33
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 25
43
- segments:
44
- - 0
45
- - 9
46
- version: "0.9"
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0.9'
47
38
  type: :development
48
- version_requirements: *id002
49
- - !ruby/object:Gem::Dependency
50
- name: rspec
51
39
  prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0.9'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
53
49
  none: false
54
- requirements:
50
+ requirements:
55
51
  - - ~>
56
- - !ruby/object:Gem::Version
57
- hash: 13
58
- segments:
59
- - 2
60
- - 7
61
- version: "2.7"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.7'
62
54
  type: :development
63
- version_requirements: *id003
64
- - !ruby/object:Gem::Dependency
65
- name: yard
66
55
  prerelease: false
67
- requirement: &id004 !ruby/object:Gem::Requirement
56
+ version_requirements: !ruby/object:Gem::Requirement
68
57
  none: false
69
- requirements:
58
+ requirements:
70
59
  - - ~>
71
- - !ruby/object:Gem::Version
72
- hash: 5
73
- segments:
74
- - 0
75
- - 7
76
- - 3
60
+ - !ruby/object:Gem::Version
61
+ version: '2.7'
62
+ - !ruby/object:Gem::Dependency
63
+ name: yard
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
77
69
  version: 0.7.3
78
70
  type: :development
79
- version_requirements: *id004
80
- - !ruby/object:Gem::Dependency
81
- name: thor
82
71
  prerelease: false
83
- requirement: &id005 !ruby/object:Gem::Requirement
72
+ version_requirements: !ruby/object:Gem::Requirement
84
73
  none: false
85
- requirements:
74
+ requirements:
86
75
  - - ~>
87
- - !ruby/object:Gem::Version
88
- hash: 43
89
- segments:
90
- - 0
91
- - 14
92
- - 6
76
+ - !ruby/object:Gem::Version
77
+ version: 0.7.3
78
+ - !ruby/object:Gem::Dependency
79
+ name: thor
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
93
85
  version: 0.14.6
94
86
  type: :runtime
95
- version_requirements: *id005
96
- description: "Templator\n\
97
- =========\n\n\
98
- Description\n\
99
- -----------\n\
100
- Templator is a command line tool allowing to generate text documents from templates written \n\
101
- in the [ERB](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html) template language.\n\n\
102
- It also provides a Domain Specific Language, the _Parameter DSL_, to define a set of parameters\n\
103
- that can be referenced from template files in order to generate the target document\n\
104
- with expected values.\n\n\
105
- Templator is developped in Ruby. It requires Ruby 1.8.7 and higher, or any version of the 1.9 branch.\n\n\
106
- Installation\n\
107
- ------------\n\n\
108
- To quickly install Templator, use the following command:\n\n gem install templator\n\n\
109
- Usage\n\
110
- -----\n\n\
111
- The following command allows to display the online help:\n\n $ templator help\n\n\
112
- Two tasks are available from the command line:\n\n * __gen__ \n\n\
113
- This task is responsible for the transformation of a given template to a target document, \n\
114
- taking into account any provided parameter files.\n\n\
115
- Here is the most simple command line invokation.\n \n $ templator gen path/to/template path/to/target\n\n\
116
- Files that define parameters can be passed to Templator with the __-p__ switch:\n\n $ templator gen path/to/template path/to/target -p path/to/paramaters1 path/to/parameters2\n\n\
117
- When parameter files are passed, Templator firstly parses these files with respect to the Parameter DSL (see below).\n\
118
- Files are parsed in the same order that they are provided by the __-p__ switch. \n\
119
- All parameters exported from these files are then visible by the template.\n\n\
120
- The __-c__ switch allows to define a default context from which Templator will try to\n\
121
- resolve parameter names that are not fully qualified in the template. More details are provided\n\
122
- at the end of this document.\n\n\n * __get_param__\n\n\
123
- This task allows to get the value of a parameter from the provided parameter files.\n\n $ templator get_param 'my_parameter' -p path/to/parameters \n\n\
124
- Parameter DSL\n\
125
- -------------\n\n\
126
- The set of parameters is expressed in a Ruby DSL that provides following methods:\n\n * __export__\n\n\
127
- This method allows to define new parameters and make them visible from a template during\n\
128
- the generation process. Following example shows how to define the parameter 'my_parameter' with\n\
129
- the value 'my_value'\n\n export \"my_parameter\" => \"my_value\"\n\n\
130
- It is also possible to define several parameters in a single export line:\n\n export \"my_parameter\" => \"my_value\", \"my_other_parameter\" => \"my_other_value\"\n\n\n\
131
- It is worth noting that parameter names can be a Ruby Symbol:\n\n export :my_parameter => \"my_value\"\n\n\
132
- More over, the parameter value can be any Ruby valid expression, for example:\n\n export :integer => 3\n export :now => Time.now\n export :upper_parameter => \"my_value\".upcase\n\n\
133
- Last but not least, you can use the value of previously defined parameters to build a \n\
134
- more complex parameter:\n\n export :parameter1 => 1\n export :parameter2 => 2\n export :sum => parameter1 + parameter2\n\n\
135
- Each time the Parameter DSL parser encounters an exported parameter, it defines \n\
136
- a method with the same name. In the previous example, the value of :parameter1\n\
137
- and :parameter2 is gotten by invoking the corresponding methods, parameter1 et parameter2. \n\n * __group__\n\n\
138
- The group method allows to define a subset of parameters.\n\n group :my_group {\n export :my_parameter => \"my_value\"\n }\n\n\
139
- Nested group is also possible:\n\n group :top {\n group :inner {\n ...\n }\n }\n\n\
140
- Value of parameters defined in other groups must be retrieved with \n\
141
- the fully qualified name of the parameter in dot notation.\n\n group :foo_group {\n export :foo => \"foo\"\n }\n\n group :bar_group {\n export :bar => \"bar\"\n }\n\n group :foobar_group {\n export :foobar => foo_group.foo + bar_group.bar\n }\n\n\
142
- A group can de defined multiple times. The resulting group is a merge of all\n\
143
- definitions taking into account the order of the parsing:\n\n #file1\n group :my_group {\n export :parameter1 => 1\n export :parameter2 => 2\n }\n\n #file2\n group :my_group {\n export :parameter1 => 0.99999\n export :parameter3 => 3\n }\n\n\
144
- Assuming that file1 and file2 are parsed in this order, the resulting group \n\
145
- is semantically equivalent to this one:\n\n group :my_group {\n export :parameter1 => 0.99999\n export :parameter2 => 2\n export :parameter3 => 3\n }\n\n * __include_group__\n\n\
146
- The include_group method is an interesting way to share some common parameters between different groups.\n\
147
- It allows to mix the parameters of a group in another one.\n\
148
- It is conceptually equivalent to the well known Ruby include method.\n\n\
149
- Consider the following example:\n\n group :mixin {\n export :mixme => \"some value\"\n }\n\n group :my_group {\n include_group :mixin\n export :another_parameter => \"another_value\"\n }\n\n\
150
- Thus, the resulting group is equivalent to :\n\n group :my_group {\n export :mixme => \"some value\"\n export :another_parameter => \"another_value\"\n }\n\n\
151
- Template Actions\n\
152
- ----------------\n\n\
153
- As said before, the template language used by Templator is ERB.\n\n\
154
- In addition to the features provided by ERB, the following extra methods can be invoked from a template:\n\n * __param__\n\n\
155
- This method allows to retrieve the value of a parameter. \n\n\
156
- Here is a concrete example:\n\n\
157
- File _parameters.txt_:\n\n group :my_group {\n export my_parameter => \"my_value\"\n }\n ...\n\n\
158
- File _template.txt_:\n\n The value of the parameter \"my_parameter\" defined in the group \"my_group\" is <%= param \"my_group.my_parameter\" %>\n\n\
159
- Command line invokation from the shell:\n\n $ templator gen template.txt output -p parameters.txt\n\n\
160
- The resulting _output_ file should have the following content:\n\n The value of the parameter \"my_parameter\" defined in the group \"my_group\" is my_value\n\n * __param_exists?__\n\n\
161
- This method tests if a parameter is defined. \n\
162
- Consider the following template example:\n\n <% if param_exists? \"my_group.my_parameter\" %>\n The parameter \"my_parameter\" is well defined in group \"my_group\".\n <% else %>\n There is no parameter \"my_parameter\" defined in group \"my_group\".\n <% end %>\n\n * __include_file__\n\n\n\
163
- This method parses the content of the given file as an ERB template, \n\
164
- and appends the resulting text into the output stream of the source template.\n\n\
165
- This is a convenient method to spread a template on multiple files.\n\n\
166
- Here is an example that dynamically generates the name of the template to\n\
167
- include according to the value of a parameter:\n\n blah blah blah\n <%= include_file \"#{param :my_parameter}.txt\"\n\n\
168
- The path of the template to include is interpreted relatively from the path\n\
169
- of the source template. \n\n\
170
- Contextual resolution of parameter names\n\n\
171
- Context\n\
172
- -------\n\n\
173
- A context is defined with the __-c__ switch. It is eventually\n\
174
- used by the __param__ and __param_exists?__ methods to resolve\n\
175
- the provided parameter names.\n\
176
- Whenever the resolution of a parameter name fails, \n\
177
- if a context is defined, it is prepended to the parameter name \n\
178
- and a new resolution is tried with the resulting name. In this case,\n\
179
- you must ensure that the context matches a valid fully qualified group name.\n\n\
180
- Context is a convenient way to generate different documents from the same template,\n\
181
- assuming that a group of parameters is defined for each expected documents.\n\n\
182
- Here is a concrete example where the objective is to generate a Debian /etc/network/interfaces \n\
183
- file for three different hosts.\n\n\
184
- File _hosts.txt_:\n\n group :host_a {\n export :address => \"192.168.121.1\"\n export :netmask => \"255.255.255.0\"\n export :gateway => \"192.168.121.254\"\n }\n\n group :host_b {\n export :address => \"192.168.122.1\"\n export :netmask => \"255.255.255.0\"\n export :gateway => \"192.168.122.254\"\n }\n\n group :host_c {\n export :address => \"192.168.123.1\"\n export :netmask => \"255.255.255.0\"\n export :gateway => \"192.168.123.254\"\n }\n\n\
185
- File _interfaces.txt_:\n\n iface eth0 inet static\n address <%= param :address %>\n netmask <%= param :netmask %>\n gateway <%= param :gateway %>\n\n\
186
- Command line execution from shell:\n\n for host in host_a host_b host_c\n do\n templator gen interfaces.txt interfaces.$host -p hosts.txt -c $host\n done\n\n\
187
- Copyright\n\
188
- ---------\n\n\
189
- Copyright \xC2\xA9 2011 Christophe Arguel. See LICENSE for details.\n"
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 0.14.6
94
+ description: ! "Templator\n=========\n\nDescription\n-----------\nTemplator is a command
95
+ line tool allowing to generate text documents from templates written \nin the [ERB](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/erb/rdoc/ERB.html)
96
+ template language.\n\nIt also provides a Domain Specific Language, the _Parameter
97
+ DSL_, to define a set of parameters\nthat can be referenced from template files
98
+ in order to generate the target document\nwith expected values.\n\nTemplator is
99
+ developped in Ruby. It requires Ruby 1.8.7 and higher, or any version of the 1.9
100
+ branch.\n\nInstallation\n------------\n\nTo quickly install Templator, use the following
101
+ command:\n\n gem install templator\n\nUsage\n-----\n\nThe following command allows
102
+ to display the online help:\n\n $ templator help\n\nTwo tasks are available from
103
+ the command line:\n\n * __gen__ \n\nThis task is responsible for the transformation
104
+ of a given template to a target document, \ntaking into account any provided parameter
105
+ files.\n\nHere is the most simple command line invokation.\n \n $ templator
106
+ gen path/to/template path/to/target\n\nFiles that define parameters can be passed
107
+ to Templator with the __-p__ switch:\n\n $ templator gen path/to/template path/to/target
108
+ -p path/to/paramaters1 path/to/parameters2\n\nWhen parameter files are passed, Templator
109
+ firstly parses these files with respect to the Parameter DSL (see below).\nFiles
110
+ are parsed in the same order that they are provided by the __-p__ switch. \nAll
111
+ parameters exported from these files are then visible by the template.\n\nThe __-c__
112
+ switch allows to define a default context from which Templator will try to\nresolve
113
+ parameter names that are not fully qualified in the template. More details are provided\nat
114
+ the end of this document.\n\n\n * __get_param__\n\nThis task allows to get the value
115
+ of a parameter from the provided parameter files.\n\n $ templator get_param 'my_parameter'
116
+ -p path/to/parameters \n\nParameter DSL\n-------------\n\nThe set of parameters
117
+ is expressed in a Ruby DSL that provides following methods:\n\n * __export__\n\nThis
118
+ method allows to define new parameters and make them visible from a template during\nthe
119
+ generation process. Following example shows how to define the parameter 'my_parameter'
120
+ with\nthe value 'my_value'\n\n export \"my_parameter\" => \"my_value\"\n\nIt
121
+ is also possible to define several parameters in a single export line:\n\n export
122
+ \"my_parameter\" => \"my_value\", \"my_other_parameter\" => \"my_other_value\"\n\n\nIt
123
+ is worth noting that parameter names can be a Ruby Symbol:\n\n export :my_parameter
124
+ => \"my_value\"\n\nMore over, the parameter value can be any Ruby valid expression,
125
+ for example:\n\n export :integer => 3\n export :now => Time.now\n export
126
+ :upper_parameter => \"my_value\".upcase\n\nLast but not least, you can use the value
127
+ of previously defined parameters to build a \nmore complex parameter:\n\n export
128
+ :parameter1 => 1\n export :parameter2 => 2\n export :sum => parameter1 + parameter2\n\nEach
129
+ time the Parameter DSL parser encounters an exported parameter, it defines \na method
130
+ with the same name. In the previous example, the value of :parameter1\nand :parameter2
131
+ is gotten by invoking the corresponding methods, parameter1 et parameter2. \n\n
132
+ * __group__\n\nThe group method allows to define a subset of parameters.\n\n group
133
+ :my_group do\n export :my_parameter => \"my_value\"\n end\n\nNested group
134
+ is also possible:\n\n group :top do\n group :inner do\n ...\n
135
+ \ end\n end\n\nValue of parameters defined in other groups must be retrieved
136
+ with \nthe fully qualified name of the parameter in dot notation.\n\n group :foo_group
137
+ do\n export :foo => \"foo\"\n end\n\n group :bar_group do\n export
138
+ :bar => \"bar\"\n end\n\n group :foobar_group do\n export :foobar =>
139
+ foo_group.foo + bar_group.bar\n end\n\nA group can de defined multiple times.
140
+ The resulting group is a merge of all\ndefinitions taking into account the order
141
+ of the parsing:\n\n #file1\n group :my_group do\n export :parameter1
142
+ => 1\n export :parameter2 => 2\n end\n\n #file2\n group :my_group
143
+ do\n export :parameter1 => 0.99999\n export :parameter3 => 3\n end\n\nAssuming
144
+ that file1 and file2 are parsed in this order, the resulting group \nis semantically
145
+ equivalent to this one:\n\n group :my_group do\n export :parameter1 =>
146
+ 0.99999\n export :parameter2 => 2\n export :parameter3 => 3\n end\n\n
147
+ * __include_group__\n\nThe include_group method is an interesting way to share some
148
+ common parameters between different groups.\nIt allows to mix the parameters of
149
+ a group in another one.\nIt is conceptually equivalent to the well known Ruby include
150
+ method.\n\nConsider the following example:\n\n group :mixin do\n export
151
+ :mixme => \"some value\"\n end\n\n group :my_group do\n include_group
152
+ :mixin\n export :another_parameter => \"another_value\"\n end\n\nThus,
153
+ the resulting group is equivalent to :\n\n group :my_group do\n export
154
+ :mixme => \"some value\"\n export :another_parameter => \"another_value\"\n
155
+ \ end\n\nTemplate Actions\n----------------\n\nAs said before, the template language
156
+ used by Templator is ERB.\n\nIn addition to the features provided by ERB, the following
157
+ extra methods can be invoked from a template:\n\n * __param__\n\nThis method allows
158
+ to retrieve the value of parameters passed to Templator by the __-p__ swicth. \n\nHere
159
+ is a concrete example:\n\nFile _parameters.txt_:\n\n group :my_group do\n export
160
+ :my_parameter => \"my_value\"\n end\n ...\n\nFile _template.txt_:\n\n The
161
+ value of the parameter \"my_parameter\" defined in the group \"my_group\" is <%=
162
+ param \"my_group.my_parameter\" %>\n\nCommand line invokation from the shell:\n\n
163
+ \ $ templator gen template.txt output -p parameters.txt\n\nThe resulting _output_
164
+ file should have the following content:\n\n The value of the parameter \"my_parameter\"
165
+ defined in the group \"my_group\" is my_value\n\n * __param_exists?__\n\nThis method
166
+ tests if a parameter is defined. \nConsider the following template example:\n\n
167
+ \ <% if param_exists? \"my_group.my_parameter\" %>\n The parameter \"my_parameter\"
168
+ is well defined in group \"my_group\".\n <% else %>\n There is no parameter
169
+ \"my_parameter\" defined in group \"my_group\".\n <% end %>\n\n * __include_file__\n\n\nThis
170
+ method parses the content of the given file as an ERB template, \nand appends the
171
+ resulting text into the output stream of the source template.\n\nThis is a convenient
172
+ method to spread a template on multiple files.\n\nHere is an example that dynamically
173
+ generates the name of the template to\ninclude according to the value of a parameter:\n\n
174
+ \ blah blah blah\n <%= include_file \"#{param :my_parameter}.txt\" %>\n\nThe
175
+ path of the template to include is interpreted relatively from the path\nof the
176
+ source template. \n\nContextual resolution of parameter names\n\nContext\n-------\n\nA
177
+ context is defined with the __-c__ switch. It is eventually\nused by the __param__
178
+ and __param_exists?__ methods to resolve\nthe provided parameter names.\nWhenever
179
+ the resolution of a parameter name fails, \nif a context is defined, it is prepended
180
+ to the parameter name \nand a new resolution is tried with the resulting name. In
181
+ this case,\nyou must ensure that the context matches a valid fully qualified group
182
+ name.\n\nContext is a convenient way to generate different documents from the same
183
+ template,\nassuming that a group of parameters is defined for each expected documents.\n\nHere
184
+ is a concrete example where the objective is to generate a Debian /etc/network/interfaces
185
+ \nfile for three different hosts.\n\nFile _hosts.txt_:\n\n group :common_parameters
186
+ do\n export :gateway => \"192.168.121.254\"\n end\n\n group :host_a
187
+ do\n export :address => \"192.168.121.1\"\n export :netmask => \"255.255.255.0\"\n
188
+ \ include_group :common_parameters\n end\n\n group :host_b do\n export
189
+ :address => \"192.168.121.2\"\n export :netmask => \"255.255.255.0\"\n include_group
190
+ :common_parameters\n end\n\n group :host_c do\n export :address =>
191
+ \"192.168.121.3\"\n export :netmask => \"255.255.255.0\"\n include_group
192
+ :common_parameters\n end\n\nFile _interfaces.txt_:\n\n iface eth0 inet static\n
193
+ \ address <%= param :address %>\n netmask <%= param :netmask %>\n gateway
194
+ <%= param :gateway %>\n\nCommand line execution from shell:\n\n for host in host_a
195
+ host_b host_c\n do\n templator gen interfaces.txt interfaces.$host -p
196
+ hosts.txt -c $host\n done\n\nCopyright\n---------\n\nCopyright © 2011 Christophe
197
+ Arguel. See LICENSE for details.\n"
190
198
  email: christophe.arguel@free.fr
191
- executables:
199
+ executables:
192
200
  - templator
193
201
  extensions: []
194
-
195
- extra_rdoc_files:
202
+ extra_rdoc_files:
196
203
  - CHANGES
197
204
  - LICENSE
198
205
  - README.md
199
206
  - TODO
200
- files:
201
- - Gemfile.lock
207
+ files:
208
+ - TODO
202
209
  - CHANGES
203
- - Gemfile
204
- - Rakefile
205
210
  - LICENSE
206
- - TODO
207
211
  - README.md
208
- - lib/templator/parameters.rb
209
- - lib/templator/parameter_code_loader.rb
212
+ - Gemfile.lock
213
+ - Rakefile
214
+ - Gemfile
215
+ - lib/templator/version.rb
216
+ - lib/templator/parameter_file_selector.rb
210
217
  - lib/templator/actions.rb
218
+ - lib/templator/parameters.rb
211
219
  - lib/templator/parameter_dsl.rb
212
- - spec/templator/parameter_dsl_spec.rb
213
- - spec/templator/parameter_code_loader_spec.rb
220
+ - spec/templator/parameter_file_selector_spec.rb
214
221
  - spec/templator/actions_spec.rb
222
+ - spec/templator/parameter_dsl_spec.rb
215
223
  - spec/templator/parameters_spec.rb
216
224
  - bin/templator
217
225
  homepage: https://github.com/carguel/templator
218
226
  licenses: []
219
-
220
227
  post_install_message:
221
228
  rdoc_options: []
222
-
223
- require_paths:
229
+ require_paths:
224
230
  - lib
225
- required_ruby_version: !ruby/object:Gem::Requirement
231
+ required_ruby_version: !ruby/object:Gem::Requirement
226
232
  none: false
227
- requirements:
228
- - - ">="
229
- - !ruby/object:Gem::Version
230
- hash: 3
231
- segments:
233
+ requirements:
234
+ - - ! '>='
235
+ - !ruby/object:Gem::Version
236
+ version: '0'
237
+ segments:
232
238
  - 0
233
- version: "0"
234
- required_rubygems_version: !ruby/object:Gem::Requirement
239
+ hash: 3420970963505475500
240
+ required_rubygems_version: !ruby/object:Gem::Requirement
235
241
  none: false
236
- requirements:
237
- - - ">="
238
- - !ruby/object:Gem::Version
239
- hash: 23
240
- segments:
241
- - 1
242
- - 3
243
- - 6
242
+ requirements:
243
+ - - ! '>='
244
+ - !ruby/object:Gem::Version
244
245
  version: 1.3.6
245
246
  requirements: []
246
-
247
247
  rubyforge_project:
248
- rubygems_version: 1.8.10
248
+ rubygems_version: 1.8.24
249
249
  signing_key:
250
250
  specification_version: 3
251
251
  summary: A command line tool allowing to generate text documents from ERB template
252
- test_files:
253
- - spec/templator/parameter_dsl_spec.rb
254
- - spec/templator/parameter_code_loader_spec.rb
252
+ test_files:
253
+ - spec/templator/parameter_file_selector_spec.rb
255
254
  - spec/templator/actions_spec.rb
255
+ - spec/templator/parameter_dsl_spec.rb
256
256
  - spec/templator/parameters_spec.rb