Class: SocialSnippet::Inserter

Inherits:
Object
  • Object
show all
Defined in:
lib/social_snippet/inserter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src) ⇒ Inserter

Constructor

Parameters:

  • src (Array<String>)

    The source code


11
12
13
14
15
16
# File 'lib/social_snippet/inserter.rb', line 11

def initialize(src)
  @src_index = 0
  @dest_index = -1
  @src = src.clone.freeze
  @dest = []
end

Instance Attribute Details

#destObject (readonly)

Returns the value of attribute dest


6
7
8
# File 'lib/social_snippet/inserter.rb', line 6

def dest
  @dest
end

#dest_indexObject (readonly)

Returns the value of attribute dest_index


4
5
6
# File 'lib/social_snippet/inserter.rb', line 4

def dest_index
  @dest_index
end

#srcObject (readonly)

Returns the value of attribute src


5
6
7
# File 'lib/social_snippet/inserter.rb', line 5

def src
  @src
end

#src_indexObject (readonly)

Returns the value of attribute src_index


3
4
5
# File 'lib/social_snippet/inserter.rb', line 3

def src_index
  @src_index
end

Instance Method Details

#ignoreObject

Ignore current line


38
39
40
# File 'lib/social_snippet/inserter.rb', line 38

def ignore
  @src_index += 1
end

#insert(line_or_lines) ⇒ Object

Insert text

Parameters:

  • line_or_lines (String or Array<String>)

    The inserted text


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/social_snippet/inserter.rb', line 45

def insert(line_or_lines)
  if line_or_lines.is_a?(Array)
    lines = line_or_lines
    dest.insert dest_index + 1, *lines
    @dest_index += lines.length
  else
    line = line_or_lines
    dest.insert dest_index + 1, line
    @dest_index += 1
  end
end

#set_index(new_index) ⇒ Object

Set index

Parameters:

  • new_index (Number)

    The next index


21
22
23
24
25
26
27
28
29
30
# File 'lib/social_snippet/inserter.rb', line 21

def set_index(new_index)
  if new_index > src.length
    raise "invalid index"
  end
  if new_index > src_index
    last_index = [new_index - 1, src.length - 1].min
    insert src[src_index .. last_index]
    @src_index = new_index
  end
end

#set_index_lastObject

Set index to last


33
34
35
# File 'lib/social_snippet/inserter.rb', line 33

def set_index_last
  set_index src.length
end

#to_sString

Get text

Returns:

  • (String)

60
61
62
# File 'lib/social_snippet/inserter.rb', line 60

def to_s
  dest.join("\n")
end