Class: ChiliPDF::StringToken
- Inherits:
-
Object
- Object
- ChiliPDF::StringToken
- Defined in:
- lib/chili_pdf/string_token.rb
Overview
Associates documentation of a “string to replace” (matcher) with it’s “replacement text” (replacement_object) and allows either either static or dynamic string replacement declarations.
Constant Summary collapse
- DEFAULT_DESCRIPTION =
Description used if the default description is empty
"[No description provided]"
- STARTING_DELIMITER =
String used to denote the begining of a token
"{{"
- ENDING_DELIMITER =
String used to denote the ending of a token
"}}"
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the String description of the token.
-
#matcher ⇒ Object
readonly
Public: Get the matcher of the token Returns the String matcher of token.
-
#replacement_object ⇒ Object
readonly
Returns the String replacement_object of token.
Instance Method Summary collapse
-
#apply_to(string) ⇒ Object
Public: Substitutes any occurrences of #replacement_object in the specified object’s response to #to_s.
-
#initialize(matcher, replacement_object, description = nil) ⇒ StringToken
constructor
Public: Create new instance of StringToken class.
- #matcher_with_delimiters ⇒ Object
Constructor Details
#initialize(matcher, replacement_object, description = nil) ⇒ StringToken
Public: Create new instance of StringToken class
matcher - the #to_s value to search for replacement_object - the value to replace ‘matcher’ with description - a friendly description which can be used
to explain what purpose the matcher is
intended to serve
‘replacement_object’ may be any object, but special behavior exists if the value is a Proc object. Refer to the #apply_to documentation for details on said behavior.
Returns new instance of StringToken
39 40 41 42 43 |
# File 'lib/chili_pdf/string_token.rb', line 39 def initialize(matcher, replacement_object, description = nil) @matcher = matcher @replacement_object = replacement_object @description = description.blank? ? DEFAULT_DESCRIPTION : description end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the String description of the token
23 24 25 |
# File 'lib/chili_pdf/string_token.rb', line 23 def description @description end |
#matcher ⇒ Object (readonly)
Public: Get the matcher of the token Returns the String matcher of token
17 18 19 |
# File 'lib/chili_pdf/string_token.rb', line 17 def matcher @matcher end |
#replacement_object ⇒ Object (readonly)
Returns the String replacement_object of token
20 21 22 |
# File 'lib/chili_pdf/string_token.rb', line 20 def replacement_object @replacement_object end |
Instance Method Details
#apply_to(string) ⇒ Object
Public: Substitutes any occurrences of #replacement_object in the specified
object's response to #to_s. If #replacement_object returns a Proc
object, #call will be issued to it.
string - string to search for token occurrences in
Examples: Returns copy of string with matcher text occurrences replaced.
53 54 55 |
# File 'lib/chili_pdf/string_token.rb', line 53 def apply_to(string) string.gsub(replacement_regexp(string), replacement_value.to_s) end |
#matcher_with_delimiters ⇒ Object
57 58 59 |
# File 'lib/chili_pdf/string_token.rb', line 57 def matcher_with_delimiters "#{STARTING_DELIMITER}#{matcher}#{ENDING_DELIMITER}" end |