Class: Lumix::TextSnippet

Inherits:
Object
  • Object
show all
Defined in:
lib/lumix/text_snippet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, text, first, last) ⇒ TextSnippet

Returns a new instance of TextSnippet.



5
6
7
8
9
10
# File 'lib/lumix/text_snippet.rb', line 5

def initialize(name, text, first, last)
  @name = name
  @text = text
  @begin = first
  @end = last
end

Instance Attribute Details

#beginObject (readonly)

Returns the value of attribute begin.



4
5
6
# File 'lib/lumix/text_snippet.rb', line 4

def begin
  @begin
end

#endObject (readonly)

Returns the value of attribute end.



4
5
6
# File 'lib/lumix/text_snippet.rb', line 4

def end
  @end
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/lumix/text_snippet.rb', line 4

def name
  @name
end

#textObject (readonly)

Returns the value of attribute text.



4
5
6
# File 'lib/lumix/text_snippet.rb', line 4

def text
  @text
end

Instance Method Details

#cleanup(txt) ⇒ Object

cleanup($1)



24
25
26
# File 'lib/lumix/text_snippet.rb', line 24

def cleanup(txt)
  txt.gsub(/\s+/, ' ')
end

#left(context = 5) ⇒ Object



14
15
16
17
18
# File 'lib/lumix/text_snippet.rb', line 14

def left(context = 5)
  ctx = [@begin - context * 10, 0].max
  @text[ctx...@begin].split(/\s+/).last(context).join(' ')# =~ /((\S+\s+){0,#{context}}\S*)\z/m
  #cleanup($1)
end

#right(context = 5) ⇒ Object

cleanup($1)



19
20
21
22
23
# File 'lib/lumix/text_snippet.rb', line 19

def right(context = 5)
  ctx = [@end + context * 10, @text.size].min
  @text[@end..ctx].split(/\s+/).first(context).join(' ')# =~ /\A(\S*(\s+\S+){0,#{context}})/m
  #cleanup($1)
end

#to_sObject



11
12
13
# File 'lib/lumix/text_snippet.rb', line 11

def to_s
  cleanup(@text[@begin...@end])
end