Module: Rodbot::Refinements
- Extended by:
- Memoize
- Defined in:
- lib/rodbot/refinements.rb
Class Method Summary collapse
-
.inflector ⇒ Zeitwerk::Inflector
Reusable inflector instance.
Instance Method Summary collapse
-
#camelize ⇒ String
Convert from under_scores to CamelCase.
-
#constantize ⇒ Class, Module
Convert module or class path to module or class.
-
#html_to_text ⇒ String
Converts HTML to plain text by removing all tags.
-
#md_to_html ⇒ String
Converts Markdown in the string to HTML.
-
#psub ⇒ String
Replace placeholders.
-
#uri_concat ⇒ String
Safely concat path segments to a URI string.
Methods included from Memoize
Class Method Details
.inflector ⇒ Zeitwerk::Inflector
Reusable inflector instance
112 113 114 |
# File 'lib/rodbot/refinements.rb', line 112 memoize def inflector Zeitwerk::Inflector.new end |
Instance Method Details
#camelize ⇒ String
Convert from under_scores to CamelCase
16 17 18 19 20 |
# File 'lib/rodbot/refinements.rb', line 16 refine String do def camelize Rodbot::Refinements.inflector.camelize(self, nil) end end |
#constantize ⇒ Class, Module
Convert module or class path to module or class
29 30 31 32 33 |
# File 'lib/rodbot/refinements.rb', line 29 refine String do def constantize Module.const_get(self.split('/').map(&:camelize).join('::')) end end |
#html_to_text ⇒ String
Converts HTML to plain text by removing all tags
82 83 84 85 86 |
# File 'lib/rodbot/refinements.rb', line 82 refine String do def html_to_text self.gsub(/<.*?>/, '') end end |
#md_to_html ⇒ String
Converts Markdown in the string to HTML
69 70 71 72 73 |
# File 'lib/rodbot/refinements.rb', line 69 refine String do def md_to_html Kramdown::Document.new(self, input: 'GFM').to_html.strip end end |
#psub ⇒ String
Replace placeholders
Placeholders are all UPCASE and wrapped in [[ and ]]. They must match keys in the placeholder hash, however, these keys are Symbols and all downcase.
100 101 102 103 104 |
# File 'lib/rodbot/refinements.rb', line 100 refine String do def psub(placeholders) self.gsub(/\[\[.*?\]\]/) { placeholders[_1[2..-3].downcase.to_sym] } end end |
#uri_concat ⇒ String
Safely concat path segments to a URI string
URI#join is ultimately used to add the given segments which has a maybe counter-intuitive API at first. Check out the docs of URI#join and the examples below.
53 54 55 56 57 58 59 60 |
# File 'lib/rodbot/refinements.rb', line 53 refine String do def uri_concat(*segments) parser = URI::Parser.new segments.inject(URI(self)) do |uri, segment| uri + parser.escape(segment) end.to_s end end |