Class: LiqrrdMetal::MatchPart
- Inherits:
-
Object
- Object
- LiqrrdMetal::MatchPart
- Defined in:
- lib/liqrrdmetal/liqrrdmetal.rb
Overview
Used to identify substrings and whether or not they were matched directly by the search string.
Instance Attribute Summary collapse
-
#match ⇒ Object
readonly
Whether the substring was part of the match.
-
#text ⇒ Object
readonly
The substring text.
Instance Method Summary collapse
-
#initialize(text, match = false) ⇒ MatchPart
constructor
A new instance of MatchPart.
-
#match? ⇒ Boolean
Does this part indicate a matched substring?.
-
#to_ascii ⇒ Object
The text wrapped with underscores (only wrapped if it was a match) score,parts = LiqqrdMetal.score_with_parts( “re”, “Preview.jpg” ) puts parts.map(&:to_ascii).join #=> P_re_view.jpg.
-
#to_html ⇒ Object
The text wrapped by the HTML
\<span class='match'\>...\</span\>
(only wrapped if it was a match) score,parts = LiqqrdMetal.score_with_parts( “re”, “Preview.jpg” ) puts parts.map(&:to_html).join #=> P<span class=‘match’>re</span>view.jpg. -
#to_json(*a) ⇒ Object
Get this part as a terse JSON payload suitable for transmitting over the wire.
-
#to_s ⇒ Object
Returns the substring (regardless of whether it was matched or not).
Constructor Details
#initialize(text, match = false) ⇒ MatchPart
Returns a new instance of MatchPart.
99 100 101 102 |
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 99 def initialize( text, match=false ) @text = text @match = match end |
Instance Attribute Details
#match ⇒ Object (readonly)
Whether the substring was part of the match
98 99 100 |
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 98 def match @match end |
#text ⇒ Object (readonly)
The substring text
96 97 98 |
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 96 def text @text end |
Instance Method Details
#match? ⇒ Boolean
Does this part indicate a matched substring?
105 |
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 105 def match?; @match; end |
#to_ascii ⇒ Object
The text wrapped with underscores (only wrapped if it was a match)
score,parts = LiqqrdMetal.score_with_parts( "re", "Preview.jpg" )
puts parts.map(&:to_ascii).join
#=> P_re_view.jpg
120 |
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 120 def to_ascii; @match ? "_#{@text}_" : text; end |
#to_html ⇒ Object
The text wrapped by the HTML \<span class='match'\>...\</span\>
(only wrapped if it was a match)
score,parts = LiqqrdMetal.score_with_parts( "re", "Preview.jpg" )
puts parts.map(&:to_html).join
#=> P<span class='match'>re</span>view.jpg
114 |
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 114 def to_html; @match ? "<span class='match'>#{@text}</span>" : @text; end |
#to_json(*a) ⇒ Object
Get this part as a terse JSON payload suitable for transmitting over the wire.
require 'json'
score,parts = LiqqrdMetal.score_with_parts( "re", "Preview.jpg" )
puts parts.to_json
#=> [{"t":"P","m":false},{"t":"re","m":true},{"t":"view.jpg","m":false}]
129 |
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 129 def to_json(*a); { t:@text, m:!!@match }.to_json(*a); end |
#to_s ⇒ Object
Returns the substring (regardless of whether it was matched or not)
108 |
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 108 def to_s; @text; end |