Class: LiqrrdMetal::MatchPart

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#matchObject (readonly)

Whether the substring was part of the match



98
99
100
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 98

def match
  @match
end

#textObject (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?

Returns:

  • (Boolean)


105
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 105

def match?;   @match; end

#to_asciiObject

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_htmlObject

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_sObject

Returns the substring (regardless of whether it was matched or not)



108
# File 'lib/liqrrdmetal/liqrrdmetal.rb', line 108

def to_s;     @text;  end