erb_asterisk 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5b5ad39db47217328f8679052c4177f1f5c0ba51
4
- data.tar.gz: b89dc017508b2d331afef84b5551cbe4dd7321e4
3
+ metadata.gz: 49aa96fce97ae70a2a1b9b9c753d26de3742f204
4
+ data.tar.gz: 334f8b7138abff5373fb23fac0a505dbbe9bdd14
5
5
  SHA512:
6
- metadata.gz: 157b37e9e653f5f0652834c4353c329879ea89f207a95319ae3f76228d6ee145ddf373312e45ae1e2ab1ef26591bbd12488b33bbe62f87bf5b395483195f5468
7
- data.tar.gz: 1cb3c9e95ca2d2c5e6aa4b39568a164bf03a3132032459844d59c790a100b0a0e325722269bea26a208fb4ce2fe9646e97469755ff32fb3caee8858ff5750789
6
+ metadata.gz: 349e46939692e862bbaa4a73d0a6a8791d71eceba20b035fb2f24f0a97eee3514def575d233f40c91fb223b6f79b4445e9abafb73e6ab65e57ef554133e27d5b
7
+ data.tar.gz: da0a68f914b49c0e13139099fd0da49c9475b9ddabc3f641d3b09d9c1fb44825fe4e63d8f8423c6ceaae0b6430c5c8bd4d85758e8c8351878f8e04bcb0ca3877
data/README.md CHANGED
@@ -66,14 +66,14 @@ announce-holdtime = no
66
66
  retry = 0
67
67
  ```
68
68
 
69
- ### ERB configuration
69
+ ### ERB extensions
70
70
 
71
- Rendering template:
71
+ #### Render template
72
72
  ```
73
73
  <%= render 'template_file_name_without_ext', variable_name: variable_value %>
74
74
  ```
75
75
 
76
- Define inclusion (asterisk `#include` command) to external configuration file:
76
+ #### Define inclusion to external configuration file
77
77
  ```
78
78
  <%= include_to 'pjsip_endpoints.conf.includes' %>
79
79
  ```
@@ -88,25 +88,42 @@ pjsip_endpoints.conf.includes:
88
88
 
89
89
  You can include this file to your actual pjsip_endpoints.conf.
90
90
 
91
- entities/office/pjsip_endpoints.conf.erb:
91
+ Also, you can define priority for inclusion:
92
92
  ```
93
93
  <%= include_to 'pjsip_endpoints.conf.includes' %>
94
- <%= 15.times.each do |i| %>
95
- <%= render 'pjsip_operators', op: 100 + i %>
96
- <% end %>
94
+ <%= include_to 'pjsip_endpoints.conf.includes', priority: 999 %>
95
+ ```
96
+ This will render to:
97
97
  ```
98
+ ; priority: 999
99
+ #include "entities/taxi/pjsip_endpoints.conf"
100
+ #include "entities/office/pjsip_endpoints.conf"
101
+ ```
102
+
103
+ #### Apply line to a tag
98
104
 
99
- entities/taxi/pjsip_endpoints.conf.erb:
105
+ office/extensions.conf.erb:
100
106
  ```
101
- <%= include_to 'pjsip_endpoints.conf.includes' %>
102
- <%= 5.times.each do |i| %>
103
- <%= render 'pjsip_operators', op: 200 + i %>
104
- <% end %>
107
+ [office-inbound]
108
+ <%= apply_line_to :global_inbound_context, 'include => office-inbound' %>
109
+ ```
110
+
111
+ extensions.conf.erb:
112
+ ```
113
+ [inbound]
114
+ <% yield_here :global_inbound_context %>
115
+ ```
116
+ This will render to:
117
+
118
+ extensions.conf:
119
+ ```
120
+ [inbound]
121
+ include => office-inbound
105
122
  ```
106
123
 
107
- Global variables:
124
+ #### Global variables
108
125
 
109
- Project avaliable global variables can be defined inside file `erb_asterisk_project.rb`, e.g.:
126
+ Project available global variables can be defined inside file `erb_asterisk_project.rb`, e.g.:
110
127
  ```
111
128
  OPERATORS_SIZE = 31
112
129
  ```
data/exe/erb_asterisk CHANGED
@@ -1,5 +1,9 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ require 'pathname'
4
+ $LOAD_PATH.unshift(
5
+ File.expand_path('../../lib', Pathname.new(__FILE__).realpath))
6
+
3
7
  require 'erb_asterisk'
4
8
 
5
9
  include ErbAsterisk
@@ -1,3 +1,3 @@
1
1
  module ErbAsterisk
2
- VERSION = '0.0.5'.freeze
2
+ VERSION = '0.0.6'.freeze
3
3
  end
data/lib/erb_asterisk.rb CHANGED
@@ -4,8 +4,9 @@ require 'pathname'
4
4
 
5
5
  module ErbAsterisk
6
6
  # Render template
7
+ # TODO: render user defined templates
7
8
  def render(template, vars = {})
8
- tpl = File.read("#{@templates}/#{template}.erb")
9
+ tpl = File.read("#{@templates_path}/#{template}.erb")
9
10
  e = ERB.new(tpl)
10
11
 
11
12
  b = TOPLEVEL_BINDING
@@ -17,27 +18,50 @@ module ErbAsterisk
17
18
  end
18
19
 
19
20
  # Declare current config file inclusion to file_name
20
- def include_to(file_name)
21
+ # args can has :priority key (larger the number - higher the priority)
22
+ def include_to(file_name, args = {})
21
23
  return unless TOPLEVEL_BINDING.local_variable_defined?(:current_conf_file)
24
+ args = { priority: 0 }.merge(args)
22
25
  @exports[file_name] = [] if @exports[file_name].nil?
26
+
23
27
  arr = @exports[file_name]
24
28
 
25
29
  current_conf_file = TOPLEVEL_BINDING.local_variable_get(:current_conf_file)
26
- if arr.include?(current_conf_file)
30
+ unless arr.index { |i| i[:file] == current_conf_file }.nil?
27
31
  puts "Skip #{current_conf_file} duplicate inclusion to #{file_name}"
28
32
  return
29
33
  end
30
34
 
31
- arr << current_conf_file
35
+ arr << { file: current_conf_file, priority: args[:priority] }
32
36
  "; Included to \"#{file_name}\""
33
37
  end
34
38
 
39
+ # Apply line to place where yield_here :tag defined
40
+ def apply_line_to(tag, line)
41
+ if @yields[tag].nil?
42
+ @yields[tag] = line
43
+ else
44
+ @yields[tag] << "\n#{line}"
45
+ end
46
+
47
+ nil
48
+ end
49
+
50
+ # Define place where put apply_line_to
51
+ def yield_here(tag)
52
+ "<%= yield_actual :#{tag} %>"
53
+ end
54
+
55
+ def yield_actual(tag)
56
+ @yields[tag]
57
+ end
58
+
35
59
  def execute
36
60
  init_instance
37
61
  load_project_file
38
62
 
39
63
  root = asterisk_root
40
- @templates = "#{root}templates".freeze
64
+ @templates_path = "#{root}templates".freeze
41
65
 
42
66
  render_files(root)
43
67
  export_includes(root)
@@ -51,7 +75,8 @@ module ErbAsterisk
51
75
 
52
76
  def init_instance
53
77
  @exports = {}
54
- @templates = ''
78
+ @templates_path = ''
79
+ @yields = {}
55
80
  end
56
81
 
57
82
  def asterisk_root
@@ -65,28 +90,55 @@ module ErbAsterisk
65
90
  end
66
91
 
67
92
  def render_files(root)
93
+ erbs = load_erbs(root)
94
+
95
+ # It does two round of rendering because of apply_line_to and yield_here.
96
+ # First round accumulates apply_line_to declarations and converts
97
+ # yield_here to yield_actual.
98
+ render_erbs(erbs)
99
+ # Second round replaces yield_actual with accumulated apply_line_to.
100
+ render_erbs(erbs)
101
+
102
+ save_erbs(erbs)
103
+ export_includes(root)
104
+ end
105
+
106
+ def load_erbs(root)
107
+ erbs = {}
108
+
68
109
  Find.find(root) do |f|
69
110
  next if File.directory?(f)
70
- next if f.start_with?(@templates)
111
+ next if f.start_with?(@templates_path)
71
112
  next unless f.end_with?('.erb')
72
113
 
73
- output_config = f.chomp('.erb')
114
+ erbs[f] = { config: f.chomp('.erb'),
115
+ content: File.read(f) }
116
+ end
74
117
 
75
- TOPLEVEL_BINDING.local_variable_set(:current_conf_file,
76
- output_config.sub(root, ''))
118
+ erbs
119
+ end
77
120
 
78
- File.write(output_config, ERB.new(File.read(f)).result)
121
+ def render_erbs(erbs)
122
+ erbs.each do |file, value|
123
+ # Declare global variable with current erb file name for include_to method:
124
+ TOPLEVEL_BINDING.local_variable_set(:current_conf_file, value[:config])
125
+ erbs[file][:content] = ERB.new(value[:content]).result
79
126
  end
80
127
  end
81
128
 
129
+ def save_erbs(erbs)
130
+ erbs.each { |_, value| File.write(value[:config], value[:content]) }
131
+ end
132
+
82
133
  def export_includes(root)
83
134
  @exports.each do |include_file, content|
84
- s = ''
85
- content.each do |file|
86
- s << "#include \"#{file}\"\n"
135
+ content = content.sort_by { |i| -i[:priority] }
136
+ result = content.reduce('') do |s, i|
137
+ s << "; priority: #{i[:priority]}\n" if i[:priority] != 0
138
+ s << "#include \"#{i[:file].sub(root, '')}\"\n"
87
139
  end
88
140
 
89
- File.write("#{root}#{include_file}", s)
141
+ File.write("#{root}#{include_file}", result)
90
142
  end
91
143
  end
92
144
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: erb_asterisk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Artem Baikuzin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-08-01 00:00:00.000000000 Z
11
+ date: 2017-08-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler