Class: InevitableCacophony::Parser::SectionedText

Inherits:
Object
  • Object
show all
Defined in:
lib/inevitable_cacophony/parser/sectioned_text.rb

Constant Summary collapse

PARAGRAPH_DELIMITER =
"\n\n"
SENTENCE_DELIMITER =
/\.\s+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description, delimiter = PARAGRAPH_DELIMITER) ⇒ SectionedText

Returns a new instance of SectionedText.

Parameters:

  • description (String)

    The description to parse

  • delimiter (String, Regex) (defaults to: PARAGRAPH_DELIMITER)

    The delimiter between string sections. Defaults to splitting by paragraphs.



17
18
19
# File 'lib/inevitable_cacophony/parser/sectioned_text.rb', line 17

def initialize(description, delimiter=PARAGRAPH_DELIMITER)
        @sections = description.split(delimiter).map(&:strip)
end

Instance Attribute Details

#sectionsObject

Returns the value of attribute sections.



21
22
23
# File 'lib/inevitable_cacophony/parser/sectioned_text.rb', line 21

def sections
  @sections
end

Instance Method Details

#find(key) ⇒ String

Find a section (paragraph, sentence, etc.) of the description matching a given regular expression.

Parameters:

  • key (Regex)

Returns:

  • (String)


27
28
29
# File 'lib/inevitable_cacophony/parser/sectioned_text.rb', line 27

def find(key)
        find_all(key).first
end

#find_all(key) ⇒ Array<String>

Find all sections matching a given key

Parameters:

  • key (Regex)

Returns:

  • (Array<String>)


34
35
36
# File 'lib/inevitable_cacophony/parser/sectioned_text.rb', line 34

def find_all(key)
        @sections.select { |s| key.match?(s) } || raise("No match for #{key.inspect} in #{@sections.inspect}")
end

#find_all_paragraphs(key) ⇒ Array<SectionedText>

As above but finds all matching paragraphs.

Parameters:

  • key (Regex)

Returns:



49
50
51
52
53
# File 'lib/inevitable_cacophony/parser/sectioned_text.rb', line 49

def find_all_paragraphs(key)
        find_all(key).map do |string|
                SectionedText.new(string, SENTENCE_DELIMITER)
        end
end

#find_paragraph(key) ⇒ SectionedText

Find a paragraph within the description, and break it up into sentences.

Parameters:

  • key (Regex)

Returns:



41
42
43
44
# File 'lib/inevitable_cacophony/parser/sectioned_text.rb', line 41

def find_paragraph(key)
        find_all_paragraphs(key).first
        find_all_paragraphs(key).first
end

#inspectObject



55
56
57
# File 'lib/inevitable_cacophony/parser/sectioned_text.rb', line 55

def inspect
        "<SectionedText: #{@sections.inspect}>"
end