curly_bracket_parser 0.1.0 → 0.9.0

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
  SHA256:
3
- metadata.gz: e2581aa770602a42a73eb9f79f3840dd53609d2905a75db5aaf3a5b0dac1f018
4
- data.tar.gz: 912c7fb9c2285515bf5b7af00da8c629222af329e68dd431f5cd649c20815df4
3
+ metadata.gz: 3d129cee0f2fe16c754b7b4f10d31505d8279dc4399c03e94bab98f9c86b9c60
4
+ data.tar.gz: 4477f5908c20c4f3513705dd973f702a6addd118f8aab221cab9bc3c751167f4
5
5
  SHA512:
6
- metadata.gz: 05eba36a79347485fd42db86ecfe49824913b497a89b6f36aa3b2924c4b2b8b6b1fae89b4bb3099708f5b76488706d1d6da4ff035f0c41bbf52cb0633f96a624
7
- data.tar.gz: 56a9306ca6375a6e7c4366781b638eb509918c2a101703de930d0f481a8661ef00e1054925c74ff845c3de0cd2ea51f6b7304f34a259aad27379a8a15fa51c3b
6
+ metadata.gz: 633664bae5c9062f5d71a0ec643db1c957b936e88a5fae88a7dda9cbb80d12d2682e37d4b8da33cd1c7a1fdd52e28a169a792bdb66ae004fb716fef4cb595c22
7
+ data.tar.gz: a6aebeae6ee35a392fe90b8d6d59838abb4bb49097ef2048c4e62fcddd95c61837cffc20c5367d8a3a77a4f2b3035ddbea12ae1b7ab7b249957200836ba876cf
data/README.md CHANGED
@@ -1,15 +1,11 @@
1
1
  # curly_bracket_parser
2
2
 
3
- Simple parser to replace curly brackets `{{like_this}}` inside strings like URLs, texts or even files easily.
3
+ Ruby gem with simple parser to replace curly brackets `{{like_this}}` inside strings like URLs, texts or even files easily.
4
4
 
5
5
  Additional support for build-in filters and custom filters make them more powerful. `{{example|my_filter}}`
6
6
 
7
7
  Using [LuckyCase](https://github.com/magynhard/lucky_case), all its case formats are supported as filter.
8
8
 
9
- ```diff
10
- - beta version
11
- ```
12
-
13
9
 
14
10
 
15
11
 
@@ -1,7 +1,11 @@
1
1
  require 'lucky_case'
2
2
 
3
3
  require 'curly_bracket_parser/version'
4
+ require_relative 'custom_errors/filter_already_registered_error'
5
+ require_relative 'custom_errors/invalid_filter_error'
6
+ require_relative 'custom_errors/invalid_variable_error'
4
7
  require_relative 'custom_errors/unresolved_variables_error'
8
+ require_relative 'custom_errors/variable_already_registered_error'
5
9
 
6
10
  #
7
11
  # CurlyBracketParser
@@ -17,8 +21,8 @@ module CurlyBracketParser
17
21
  VARIABLE_DECODER_REGEX = /{{([^{}\|]+)\|?([^{}\|]*)}}/
18
22
  VARIABLE_REGEX = /{{[^{}]+}}/
19
23
 
20
- VALID_FILTERS = [
21
- LuckyCase::CASES.keys.map(&:to_s)
24
+ VALID_DEFAULT_FILTERS = [
25
+ LuckyCase::CASES.keys.map(&:to_s)
22
26
  ].flatten
23
27
 
24
28
  #----------------------------------------------------------------------------------------------------
@@ -31,13 +35,21 @@ module CurlyBracketParser
31
35
  # @param [String] replace_pattern pattern used when param unresolved_vars is set to :replace. You can include the var name \\1 and filter \\1. Empty string to remove unresolved variables.
32
36
  # @return [String, UnresolvedVariablesError] parsed string
33
37
  def self.parse(string, variables, unresolved_vars: :raise, replace_pattern: "##\\1##")
38
+ variables ||= {}
34
39
  result_string = string.clone
35
40
  if CurlyBracketParser.any_variable_included? string
36
41
  loop do
37
42
  variables(string).each do |string_var|
38
43
  name, filter = decode_variable(string_var)
39
44
  if variables[name.to_sym]
40
- value = process_filter(variables[name.to_sym], filter)
45
+ value = if filter
46
+ process_filter(filter, variables[name.to_sym])
47
+ else
48
+ variables[name.to_sym]
49
+ end
50
+ result_string.gsub!(string_var, value)
51
+ elsif registered_default_var?(name.to_s)
52
+ value = process_default_var(name)
41
53
  result_string.gsub!(string_var, value)
42
54
  end
43
55
  end
@@ -89,42 +101,154 @@ module CurlyBracketParser
89
101
 
90
102
  # Register your custom filter to the filter list
91
103
  #
92
- # @param [String] name of the filter, also used then in your strings, e.g. {{var_name|my_filter_name}}
104
+ # @param [String] filter name of the filter, also used then in your strings, e.g. {{var_name|my_filter_name}}
93
105
  # @param [Lambda] function of the filter to run the variable against
94
106
  # @return [Boolean] true if filter was added, false if it already exists
95
- def self.register_filter(name, &block)
96
- raise "NOT IMPLEMENTED YET!"
107
+ def self.register_filter(filter, &block)
108
+ @@registered_filters ||= {}
109
+ filter = filter.to_s
110
+ if valid_filter?(filter)
111
+ raise FilterAlreadyRegisteredError, "The given filter name '#{filter}' is already registered"
112
+ else
113
+ @@registered_filters[filter] = block
114
+ end
97
115
  end
98
116
 
99
117
  #----------------------------------------------------------------------------------------------------
100
118
 
101
- def self.register_default_variable(name, &block)
102
- raise "NOT IMPLEMENTED YET!"
119
+ # @param [String] filter name of the filter, also used then in your strings, e.g. {{var_name|my_filter_name}}
120
+ # @param [String] value string to apply the specified filter on
121
+ # @return [String] converted string with applied filter
122
+ def self.process_filter(filter, value)
123
+ @@registered_filters ||= {}
124
+ filter = filter.to_s
125
+ if @@registered_filters[filter]
126
+ @@registered_filters[filter].call(value)
127
+ elsif VALID_DEFAULT_FILTERS.include?(filter) && LuckyCase.valid_case_type?(filter)
128
+ LuckyCase.convert_case(value, filter)
129
+ else
130
+ message = "Invalid filter '#{filter}'. Valid filters are: #{self.valid_filters.join(' ')}"
131
+ raise InvalidFilterError, message
132
+ end
103
133
  end
104
134
 
105
135
  #----------------------------------------------------------------------------------------------------
106
136
 
107
- def self.process_filter(value, filter)
108
- return value unless filter
109
- if VALID_FILTERS.include? filter
110
- if LuckyCase.valid_case_type? filter
111
- return LuckyCase.convert_case(value, filter)
112
- else
113
- raise "FILTER '#{filter}' NOT IMPLEMENTED YET!"
114
- end
115
- else
116
- raise "Invalid filter '#{filter}'"
117
- end
137
+ # Retrieve Array with valid filters
138
+ #
139
+ # @return [Array<String>] of valid filters
140
+ def self.valid_filters
141
+ all_filters = VALID_DEFAULT_FILTERS
142
+ @@registered_filters ||= {}
143
+ all_filters + @@registered_filters.keys.map(&:to_s)
144
+ end
145
+
146
+ #----------------------------------------------------------------------------------------------------
147
+
148
+ # Check if a given filter is valid
149
+ #
150
+ # @param [String] name
151
+ # @return [Boolean] true if filter exists, otherwise false
152
+ def self.valid_filter?(name)
153
+ self.valid_filters.include? name
118
154
  end
119
155
 
120
156
  #----------------------------------------------------------------------------------------------------
121
157
 
158
+ # Check if given variable has a defined filter
159
+ # @example
160
+ # {{variable|filter}} => has filter, true
161
+ # {{variable2}} => has no filter, false
162
+ #
163
+ # @param [String] variable
164
+ # @return [Boolean] true if has a defined filter, otherwise false
122
165
  def self.has_filter?(variable)
123
166
  decode_variable(variable)[:filter] != nil
124
167
  end
125
168
 
126
169
  #----------------------------------------------------------------------------------------------------
127
170
 
171
+ # Return the given default variable by processing its block/proc
172
+ #
173
+ # @param [String] name of the variable to return
174
+ # @return [String] value of the variable
175
+ def self.process_default_var(name)
176
+ @@registered_default_vars ||= {}
177
+ name = name.to_s
178
+ if @@registered_default_vars[name]
179
+ @@registered_default_vars[name].call()
180
+ else
181
+ message = "Invalid default variable '#{name}'. Valid registered default variables are: #{self.registered_default_vars.join(' ')}"
182
+ raise InvalidVariableError, message
183
+ end
184
+ end
185
+
186
+ #----------------------------------------------------------------------------------------------------
187
+
188
+ # Register a default variable to be replaced automatically by the given block value in future
189
+ # If the variable exists already, it will throw an VariableAlreadyRegisteredError
190
+ #
191
+ # @param [String] name of the default var
192
+ # @param [Proc] block
193
+ # @return [Proc] given block
194
+ def self.register_default_var(name, &block)
195
+ @@registered_default_vars ||= {}
196
+ name = name.to_s
197
+ if registered_default_var?(name)
198
+ raise VariableAlreadyRegisteredError, "The given variable name '#{name}' is already registered. If you want to override that variable explicitly, call #register_default_var! instead!"
199
+ else
200
+ @@registered_default_vars[name] = block
201
+ end
202
+ end
203
+
204
+ #----------------------------------------------------------------------------------------------------
205
+
206
+ # Register a default variable to be replaced automatically by the given block value in future
207
+ # If the variable exists already, it will be overwritten
208
+ #
209
+ # @param [String] name of the default var
210
+ # @param [Proc] block
211
+ # @return [Proc] given block
212
+ def self.register_default_var!(name, &block)
213
+ @@registered_default_vars ||= {}
214
+ name = name.to_s
215
+ @@registered_default_vars[name] = block
216
+ end
217
+
218
+ #----------------------------------------------------------------------------------------------------
219
+
220
+ # Unregister / remove an existing default variable
221
+ #
222
+ # @param [String] name of the variable
223
+ def self.unregister_default_var(name)
224
+ @@registered_default_vars ||= {}
225
+ name = name.to_s
226
+ @@registered_default_vars.delete(name)
227
+ nil
228
+ end
229
+
230
+ #----------------------------------------------------------------------------------------------------
231
+
232
+ # Return an array of registered default variables
233
+ #
234
+ # @return [Array<String>]
235
+ def self.registered_default_vars
236
+ @@registered_default_vars ||= {}
237
+ @@registered_default_vars.keys.map(&:to_s)
238
+ end
239
+
240
+ #----------------------------------------------------------------------------------------------------
241
+
242
+ # Check if the given variable is a registered default variable
243
+ #
244
+ # @param [String] name of the variable
245
+ # @return [Boolean] true if variable is registered, otherwise false
246
+ def self.registered_default_var?(name)
247
+ self.registered_default_vars.include? name
248
+ end
249
+
250
+ #----------------------------------------------------------------------------------------------------
251
+
128
252
  # Return a hash containing separated name and filter of a variable
129
253
  #
130
254
  # @example
@@ -145,7 +269,7 @@ module CurlyBracketParser
145
269
  def self.decoded_variables(string)
146
270
  var_name = 0
147
271
  var_filter = 1
148
- string.scan(VARIABLE_DECODER_REGEX).map { |e| {"#{e[var_name].strip}": e[var_filter].strip != '' ? e[var_filter].strip : nil } }.flatten
272
+ string.scan(VARIABLE_DECODER_REGEX).map { |e| { "#{e[var_name].strip}": e[var_filter].strip != '' ? e[var_filter].strip : nil } }.flatten
149
273
  end
150
274
 
151
275
  #----------------------------------------------------------------------------------------------------
@@ -1,3 +1,3 @@
1
1
  module CurlyBracketParser
2
- VERSION = '0.1.0'.freeze
2
+ VERSION = '0.9.0'.freeze
3
3
  end
@@ -0,0 +1,2 @@
1
+ class FilterAlreadyRegisteredError < StandardError
2
+ end
@@ -0,0 +1,2 @@
1
+ class InvalidFilterError < StandardError
2
+ end
@@ -0,0 +1,2 @@
1
+ class InvalidVariableError < StandardError
2
+ end
@@ -0,0 +1,2 @@
1
+ class VariableAlreadyRegisteredError < StandardError
2
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: curly_bracket_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthäus Beyrle
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-22 00:00:00.000000000 Z
11
+ date: 2021-01-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lucky_case
@@ -85,7 +85,11 @@ files:
85
85
  - curly_bracket_parser.gemspec
86
86
  - lib/curly_bracket_parser.rb
87
87
  - lib/curly_bracket_parser/version.rb
88
+ - lib/custom_errors/filter_already_registered_error.rb
89
+ - lib/custom_errors/invalid_filter_error.rb
90
+ - lib/custom_errors/invalid_variable_error.rb
88
91
  - lib/custom_errors/unresolved_variables_error.rb
92
+ - lib/custom_errors/variable_already_registered_error.rb
89
93
  homepage: https://github.com/magynhard/curly_bracket_parser
90
94
  licenses:
91
95
  - MIT