Class: ToSource::State

Inherits:
Object
  • Object
show all
Defined in:
lib/to_source/state.rb

Overview

Emitter state

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeundefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize object



35
36
37
38
39
# File 'lib/to_source/state.rb', line 35

def initialize
  @last        = Command::NULL
  @indentation = 0
  @buffer      = []
end

Instance Attribute Details

#bufferArray (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return buffer

Returns:

  • (Array)


27
28
29
# File 'lib/to_source/state.rb', line 27

def buffer
  @buffer
end

#identationFixnum (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return current indentation level

Returns:

  • (Fixnum)


19
20
21
# File 'lib/to_source/state.rb', line 19

def identation
  @identation
end

#lastCommand (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return last command

Returns:



11
12
13
# File 'lib/to_source/state.rb', line 11

def last
  @last
end

Instance Method Details

#blank?true, false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Test for blank line

Returns:

  • (true)

    if line is blank

  • (false)

    otherwise



114
115
116
# File 'lib/to_source/state.rb', line 114

def blank?
  buffer.last == "\n"
end

#execute(command) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Execute command

Parameters:

Returns:

  • (self)


49
50
51
52
53
# File 'lib/to_source/state.rb', line 49

def execute(command)
  command.run(self)
  @last = command
  self
end

#indentself

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Indent line if needed

Returns:

  • (self)


98
99
100
101
102
# File 'lib/to_source/state.rb', line 98

def indent
  return unless blank?
  write(' ' * @indentation)
  self
end

#new_lineself

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Write newline

Returns:

  • (self)


124
125
126
127
# File 'lib/to_source/state.rb', line 124

def new_line
  write("\n")
  self
end

#push(command) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Push command

Parameters:

Returns:

  • (self)


86
87
88
89
90
# File 'lib/to_source/state.rb', line 86

def push(command)
  indent
  write(command.content)
  self
end

#shift(width) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Perform shift by width

Parameters:

  • width (Fixnum)

Returns:

  • (self)


137
138
139
140
141
# File 'lib/to_source/state.rb', line 137

def shift(width)
  @indentation += width
  @indentation = 0 if @indentation < 0
  new_line
end

#sourceString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return source

Returns:

  • (String)


74
75
76
# File 'lib/to_source/state.rb', line 74

def source
  buffer.join('')
end

#write(string) ⇒ self

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Write string to buffer

Parameters:

  • string (String)

Returns:

  • (self)


63
64
65
66
# File 'lib/to_source/state.rb', line 63

def write(string)
  @buffer << string
  self
end