lib/html2rss/attribute_post_processors/template.rb in html2rss-0.7.0 vs lib/html2rss/attribute_post_processors/template.rb in html2rss-0.8.0
- old
+ new
@@ -2,11 +2,12 @@
module Html2rss
module AttributePostProcessors
## Returns a formatted String according to the string pattern.
#
- # If +self+ is given as a method, the extracted value will be used.
+ # 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>
@@ -20,15 +21,12 @@
# price:
# selector: '.price'
# title:
# selector: h1
# post_process:
- # name: template
- # string: '%s (%s)'
- # methods:
- # - self
- # - price
+ # name: template
+ # string: '%{self} (%{price})'
#
# Would return:
# 'Product (23,42€)'
class Template
def initialize(value, env)
@@ -36,25 +34,33 @@
@options = env[:options]
@item = env[:item]
end
##
- # - uses {http://ruby-doc.org/core-2.6.3/String.html#method-i-25 String#%}
# @return [String]
def get
- string % methods
+ if @options['methods']
+ string % methods
+ else
+ names = string.scan(/%[<|{](\w*)[>|}]/).flatten
+ names.uniq!
+
+ format(string, names.map { |name| [name.to_sym, item_value(name)] }.to_h)
+ end
end
private
def string
@options['string']
end
def methods
- @methods ||= @options['methods'].map do |method|
- method == 'self' ? @value.to_s : @item.public_send(method.to_sym).to_s
- end
+ @methods ||= @options['methods'].map(&method(:item_value))
+ end
+
+ def item_value(method_name)
+ method_name.to_s == 'self' ? @value.to_s : @item.public_send(method_name.to_sym).to_s
end
end
end
end