lib/html2rss/attribute_post_processors/substring.rb in html2rss-0.11.0 vs lib/html2rss/attribute_post_processors/substring.rb in html2rss-0.12.0
- old
+ new
@@ -26,27 +26,47 @@
# start: 4
# end: 6
#
# Would return:
# 'bar'
- class Substring
- ##
- # @param value [String] The original string to extract a substring from.
- # @param env [Item::Context] Context object providing additional environment details.
- def initialize(value, env)
- @value = value
- @options = env[:options]
+ class Substring < Base
+ def self.validate_args!(value, context)
+ assert_type value, String, :value
+
+ options = context[:options]
+ assert_type options[:start], Integer, :start
+
+ end_index = options[:end]
+ assert_type end_index, Integer, :end if end_index
end
##
# Extracts the substring from the original string based on the provided start and end indices.
#
# @return [String] The extracted substring.
def get
- start_index = @options[:start].to_i
- end_index = @options[:end]&.to_i || @value.length
+ value[range]
+ end
- @value[start_index..end_index]
+ ##
+ # Determines the range for the substring extraction based on the provided start and end indices.
+ #
+ # @return [Range] The range object representing the start and end/Infinity (integers).
+ def range
+ return (start_index..) unless end_index?
+
+ if start_index == end_index
+ raise ArgumentError,
+ 'The `start` value must be unequal to the `end` value.'
+ end
+
+ (start_index..end_index)
end
+
+ private
+
+ def end_index? = !context[:options][:end].to_s.empty?
+ def end_index = context[:options][:end].to_i
+ def start_index = context[:options][:start].to_i
end
end
end