lib/html2rss/attribute_post_processors/template.rb in html2rss-0.9.0 vs lib/html2rss/attribute_post_processors/template.rb in html2rss-0.10.0
- old
+ new
@@ -1,67 +1,91 @@
-require 'sanitize'
+# frozen_string_literal: true
module Html2rss
module AttributePostProcessors
- ## Returns a formatted String according to the string pattern.
+ ##
+ # Returns a formatted String according to the string pattern.
#
# If +self+ is used, the selectors extracted value will be used.
# It uses [Kernel#format](https://ruby-doc.org/core/Kernel.html#method-i-format)
#
# Imagine this HTML:
+ #
# <li>
# <h1>Product</h1>
# <span class="price">23,42€</span>
# </li>
#
+ #
# YAML usage example:
#
# selectors:
# items:
# selector: 'li'
# price:
- # selector: '.price'
+ # selector: '.price'
# title:
# selector: h1
# post_process:
# name: template
# string: '%{self} (%{price})'
#
# Would return:
# 'Product (23,42€)'
class Template
+ ##
+ # @param value [String]
+ # @param env [Item::Context]
def initialize(value, env)
@value = value
@options = env[:options]
@item = env[:item]
@string = @options[:string]
end
##
# @return [String]
def get
- return format_string_with_methods if @options[:methods]
-
- names = string.scan(/%[<|{](\w*)[>|}]/)
- names.flatten!
- names.compact!
- names.map!(&:to_sym)
-
- format(string, names.map { |name| [name, item_value(name)] }.to_h)
+ @options[:methods] ? format_string_with_methods : format_string_with_dynamic_params
end
private
+ ##
+ # @return [String] the string containing the template
attr_reader :string
+ ##
+ # @return [Array<String>]
def methods
- @methods ||= @options[:methods].map(&method(:item_value))
+ @methods ||= @options[:methods].map { |method_name| item_value(method_name) }
end
+ ##
+ # Formats a string using methods.
+ #
+ # @return [String]
+ # @deprecated Use %<id>s formatting instead. Will be removed in version 1.0.0. See README / Dynamic parameters.
def format_string_with_methods
+ warn '[DEPRECATION] This method of using params is deprecated and \
+ support for it will be removed in version 1.0.0.\
+ Please use dynamic parameters (i.e. %<id>s, see README.md) instead.'
+
string % methods
end
+ ##
+ # @return [String]
+ def format_string_with_dynamic_params
+ param_names = string.scan(/%[<|{](\w*)[>|}]/)
+ param_names.flatten!
+
+ format(string, param_names.to_h { |name| [name.to_sym, item_value(name)] })
+ end
+
+ ##
+ # @param method_name [String, Symbol]
+ # @return [String]
def item_value(method_name)
method_name.to_sym == :self ? @value.to_s : @item.public_send(method_name).to_s
end
end
end