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 #=> Preview.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.
100 101 102 103 |
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 100 def initialize( text, match=false ) @text = text @match = match end |
Instance Attribute Details
#match ⇒ Object (readonly)
Whether the substring was part of the match
99 100 101 |
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 99 def match @match end |
#text ⇒ Object (readonly)
The substring text
97 98 99 |
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 97 def text @text end |
Instance Method Details
#match? ⇒ Boolean
Does this part indicate a matched substring?
106 |
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 106 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
121 |
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 121 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
#=> Preview.jpg
115 |
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 115 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]
130 |
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 130 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)
109 |
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 109 def to_s; @text; end |