Sha256: 805dba21bceb23d9c64f68c47dcad98b6988e68306b58657827a22852d409ce7
Contents?: true
Size: 1.95 KB
Versions: 1
Compression:
Stored size: 1.95 KB
Contents
# frozen_string_literal: true module Html2rss module AttributePostProcessors ## # Returns a defined part of a String. # # Both parameters must be an Integer and they can be negative. # The +end+ parameter can be omitted, in that case it will not cut the # String at the end. # # A Regexp or a MatchString is not supported. # # See the [`String#[]`](https://ruby-doc.org/core/String.html#method-i-5B-5D) # documentation for more information. # # Imagine this HTML: # <h1>Foo bar and baz<h1> # # YAML usage example: # selectors: # title: # selector: h1 # post_process: # name: substring # start: 4 # end: 6 # # Would return: # 'bar' 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 value[range] end ## # 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
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
html2rss-0.12.0 | lib/html2rss/attribute_post_processors/substring.rb |