erb_asterisk 0.0.12 → 0.0.13

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 124bb817f6e9799290a789694e9be27b777d6530
4
- data.tar.gz: af58874559e1b969d11bdfe3e41ba6512cc04978
3
+ metadata.gz: 3a6778b40d23d7041aba395f23ba8f43dbebfec9
4
+ data.tar.gz: b36f903921fba2bec91860612bec8caae9c3bfe9
5
5
  SHA512:
6
- metadata.gz: 31ec76833416111df756969553f613e36dcdd64dca87fe6b0dfe3e1ecbb95cb025b105780c6137b6d99b8274b0f9992f713193527fe950bdaf595dda62e2fe4b
7
- data.tar.gz: c2c040017c0bb3977507e2f767e111056629fb3b38dc9b3f1f11e1258b0b14ef42a5a367dc15d94549d102b904b1185a65492857ebc4e30e29dc480bb5bae36c
6
+ metadata.gz: 7bb2a7800da7b7945397bdb3c06895ac6c7af75f5c67ab76ee17dd0c7f03af4bec8bcfbf114fa82b61fad0a9a342936140169d4cfc06bb48e3891d7a8dd1a4d4
7
+ data.tar.gz: 452ee0b49ba80154632295e4ec6d10ee9ed1aa00bac205907f8a643f6053fc568b32a70f45a7646446d93da0abf899f254c417acfe0c70cece3259cd3843fbf0
data/README.md CHANGED
@@ -68,12 +68,12 @@ retry = 0
68
68
 
69
69
  ### ERB extensions
70
70
 
71
- #### Render template
71
+ #### render: render template
72
72
  ```
73
73
  <%= render 'template_file_name_without_ext', variable_name: variable_value %>
74
74
  ```
75
75
 
76
- #### Define inclusion to external configuration file
76
+ #### include_to: define inclusion to external configuration file
77
77
  ```
78
78
  <%= include_to 'pjsip_endpoints.conf.includes' %>
79
79
  ```
@@ -88,7 +88,7 @@ pjsip_endpoints.conf.includes:
88
88
 
89
89
  You can include this file to your actual pjsip_endpoints.conf.
90
90
 
91
- Also, you can define priority for inclusion:
91
+ Also, you can define `priority` argument for inclusion:
92
92
  ```
93
93
  <%= include_to 'pjsip_endpoints.conf.includes' %>
94
94
  <%= include_to 'pjsip_endpoints.conf.includes', priority: 999 %>
@@ -100,7 +100,7 @@ This will render to:
100
100
  #include "entities/office/pjsip_endpoints.conf"
101
101
  ```
102
102
 
103
- #### Apply line to a tag
103
+ #### apply_line_to: apply line to a tag
104
104
 
105
105
  office/extensions.conf.erb:
106
106
  ```
@@ -122,7 +122,38 @@ extensions.conf:
122
122
  include => office-inbound
123
123
  ```
124
124
 
125
- #### Escape special symbols in extension name
125
+ #### content_for: apply block to a tag
126
+ office/extensions.conf.erb:
127
+ ```
128
+ <% content_for :global_outbound_context do %>
129
+ <% exten = 'super_extension-' -%>
130
+ exten => _<%= escape_exten exten %>X.,1,NoOp
131
+ same => n,Goto(another-extension,${EXTEN:<%= exten.size %>},1)
132
+ <% end %>
133
+ ```
134
+
135
+ extensions.conf.erb:
136
+ ```
137
+ [outbound]
138
+ <%= yield_here :global_outbound_context %>
139
+ ```
140
+
141
+ extensions.conf:
142
+ ```
143
+ [outbound]
144
+ ; Yield for :outbound
145
+ exten => _super_e[x]te[n]sio[n]-X.,1,NoOp
146
+ same => n,Goto(another-extension,${EXTEN:16},1)
147
+ ```
148
+
149
+ `apply_line_to` and `content_for` can has `priority` argument, just like `include_to` method:
150
+ ```
151
+ <% content_for :global_outbound_context, priority: 999 do %>
152
+ ...
153
+ <% apply_line_to :outbound_glob, 'include => outbound', priority: -10 %>
154
+ ```
155
+
156
+ #### escape_exten: escape special symbols in extension name
126
157
  ```
127
158
  exten => _<%= escape_exten 'LongExtension1234!' %>-X.,1,NoOp
128
159
  ```
@@ -137,6 +168,9 @@ exten => _Lo[n]gE[x]te[n]sio[n]1234[!]-X.,1,NoOp
137
168
  Project available global variables can be defined inside file `erb_asterisk_project.rb`, e.g.:
138
169
  ```
139
170
  OPERATORS_SIZE = 31
171
+
172
+ LAN = '192.168.1.0/255.255.255.0'
173
+ EXTERNAL_HOST = 'no-ip.some.host.eg'
140
174
  ```
141
175
 
142
176
  ### Command line arguments
@@ -0,0 +1,22 @@
1
+ module ErbAsterisk
2
+ module Inclusion
3
+ # Declare current config file inclusion to file_name
4
+ # args can has :priority key (larger the number - higher the priority)
5
+ def include_to(file_name, args = {})
6
+ return unless TOPLEVEL_BINDING.local_variable_defined?(:current_conf_file)
7
+ default_args!(args)
8
+ @exports[file_name] = [] if @exports[file_name].nil?
9
+
10
+ arr = @exports[file_name]
11
+
12
+ current_conf_file = TOPLEVEL_BINDING.local_variable_get(:current_conf_file)
13
+ unless arr.index { |i| i[:file] == current_conf_file }.nil?
14
+ puts "Skip #{current_conf_file} duplicate inclusion to #{file_name}"
15
+ return
16
+ end
17
+
18
+ arr << { file: current_conf_file, priority: args[:priority] }
19
+ "; Included to \"#{file_name}\""
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ module ErbAsterisk
2
+ module Render
3
+ # Render template
4
+ def render(template, vars = {})
5
+ old_erb_output = @erb_output
6
+ @erb_output = ''
7
+
8
+ erb = new_erb(read_template(template))
9
+
10
+ b = binding
11
+ vars.each do |name, value|
12
+ b.local_variable_set(name, value)
13
+ end
14
+
15
+ r = erb.result(b)
16
+ @erb_output = old_erb_output
17
+ r
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ module ErbAsterisk
2
+ module Utils
3
+ # Escape special symbols in extension name
4
+ #
5
+ # vnov -> v[n]on
6
+ # LongExtension1234! -> Lo[n]gE[x]te[n]sio[n]1234[!]
7
+ #
8
+ def escape_exten(exten)
9
+ exten.each_char.reduce('') do |s, c|
10
+ s << (%w(x z n . !).include?(c.downcase) ? "[#{c}]" : c)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module ErbAsterisk
2
- VERSION = '0.0.12'.freeze
2
+ VERSION = '0.0.13'.freeze
3
3
  end
@@ -0,0 +1,46 @@
1
+ module ErbAsterisk
2
+ module Yields
3
+ # Apply line to place where yield_here :tag defined
4
+ def apply_line_to(tag, line, args = {})
5
+ default_args!(args)
6
+ apply_to_yields(:line, tag, line, args[:priority])
7
+ "; Applied \"#{line}\" to :#{tag}"
8
+ end
9
+
10
+ # Apply block to yield_here
11
+ def content_for(tag, args = {})
12
+ default_args!(args)
13
+ old_output = @erb_output
14
+ @erb_output = ''
15
+ apply_to_yields(:block, tag, yield, args[:priority])
16
+ @erb_output = old_output
17
+ end
18
+
19
+ # Define place where put apply_line_to
20
+ def yield_here(tag)
21
+ "<%= yield_actual :#{tag} %>"
22
+ end
23
+
24
+ def yield_actual(tag)
25
+ "; Yield for :#{tag}\n" << output_yield(tag)
26
+ end
27
+
28
+ private
29
+
30
+ def apply_to_yields(type, tag, content, priority)
31
+ @yields[tag] = [] if @yields[tag].nil?
32
+ @yields[tag] << { content: content, priority: priority, type: type }
33
+ end
34
+
35
+ def output_yield(tag)
36
+ a = @yields[tag]
37
+ a = a.sort_by { |i| -i[:priority] }
38
+ result = a.reduce('') do |s, i|
39
+ s << "; priority: #{i[:priority]}\n" if i[:priority] != 0
40
+ s << "#{i[:content]}\n"
41
+ end
42
+
43
+ result
44
+ end
45
+ end
46
+ end
data/lib/erb_asterisk.rb CHANGED
@@ -2,69 +2,16 @@ require 'erb'
2
2
  require 'find'
3
3
  require 'pathname'
4
4
 
5
- module ErbAsterisk
6
- # Render template
7
- def render(template, vars = {})
8
- tpl = read_template(template)
9
- e = new_erb(tpl)
10
-
11
- b = binding
12
- vars.each do |name, value|
13
- b.local_variable_set(name, value)
14
- end
15
-
16
- e.result(b)
17
- end
18
-
19
- # Declare current config file inclusion to file_name
20
- # args can has :priority key (larger the number - higher the priority)
21
- def include_to(file_name, args = {})
22
- return unless TOPLEVEL_BINDING.local_variable_defined?(:current_conf_file)
23
- args = { priority: 0 }.merge(args)
24
- @exports[file_name] = [] if @exports[file_name].nil?
25
-
26
- arr = @exports[file_name]
27
-
28
- current_conf_file = TOPLEVEL_BINDING.local_variable_get(:current_conf_file)
29
- unless arr.index { |i| i[:file] == current_conf_file }.nil?
30
- puts "Skip #{current_conf_file} duplicate inclusion to #{file_name}"
31
- return
32
- end
33
-
34
- arr << { file: current_conf_file, priority: args[:priority] }
35
- "; Included to \"#{file_name}\""
36
- end
37
-
38
- # Apply line to place where yield_here :tag defined
39
- def apply_line_to(tag, line)
40
- if @yields[tag].nil?
41
- @yields[tag] = line
42
- else
43
- @yields[tag] << "\n#{line}"
44
- end
5
+ require 'erb_asterisk/render'
6
+ require 'erb_asterisk/inclusion'
7
+ require 'erb_asterisk/yields'
8
+ require 'erb_asterisk/utils'
45
9
 
46
- "; Applied \"#{line}\" to :#{tag}"
47
- end
48
-
49
- # Define place where put apply_line_to
50
- def yield_here(tag)
51
- "<%= yield_actual :#{tag} %>"
52
- end
53
-
54
- def yield_actual(tag)
55
- "; Yield for :#{tag}\n" << @yields[tag]
56
- end
57
-
58
- # Escape special symbols in extension name
59
- #
60
- # vnov -> v[n]on
61
- # LongExtension1234! -> Lo[n]gE[x]te[n]sio[n]1234[!]
62
- #
63
- def escape_exten(exten)
64
- exten.each_char.reduce('') do |s, c|
65
- s << (ERB_ASTERISK_PATTERNS.include?(c.downcase) ? "[#{c}]" : c)
66
- end
67
- end
10
+ module ErbAsterisk
11
+ include Render
12
+ include Inclusion
13
+ include Yields
14
+ include Utils
68
15
 
69
16
  def execute(opts)
70
17
  init_instance(opts)
@@ -82,7 +29,6 @@ module ErbAsterisk
82
29
  ERB_PROJECT_FILE = './erb_asterisk_project.rb'.freeze
83
30
  ERB_ASTERISK_CONF = 'asterisk.conf'.freeze
84
31
  ERB_ASTERISK_DIR = 'asterisk/'.freeze
85
- ERB_ASTERISK_PATTERNS = %w(x z n . !)
86
32
 
87
33
  def init_instance(opts)
88
34
  @exports = {}
@@ -168,6 +114,10 @@ module ErbAsterisk
168
114
  end
169
115
 
170
116
  def new_erb(content)
171
- ERB.new(content, nil, '-')
117
+ ERB.new(content, nil, '-', '@erb_output')
118
+ end
119
+
120
+ def default_args!(args)
121
+ args[:priority] = 0 if args[:priority].nil?
172
122
  end
173
123
  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.12
4
+ version: 0.0.13
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-08 00:00:00.000000000 Z
11
+ date: 2017-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: slop
@@ -113,7 +113,11 @@ files:
113
113
  - exe/erb_asterisk
114
114
  - lib/erb_asterisk.rb
115
115
  - lib/erb_asterisk/arg_parser.rb
116
+ - lib/erb_asterisk/inclusion.rb
117
+ - lib/erb_asterisk/render.rb
118
+ - lib/erb_asterisk/utils.rb
116
119
  - lib/erb_asterisk/version.rb
120
+ - lib/erb_asterisk/yields.rb
117
121
  homepage: https://github.com/ybinzu/erb_asterisk
118
122
  licenses:
119
123
  - MIT