Class: ToSource::State

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeState

Returns a new instance of State.



8
9
10
11
12
# File 'lib/to_source/state.rb', line 8

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

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



6
7
8
# File 'lib/to_source/state.rb', line 6

def buffer
  @buffer
end

#identationObject (readonly)

Returns the value of attribute identation.



5
6
7
# File 'lib/to_source/state.rb', line 5

def identation
  @identation
end

#lastObject (readonly)

Returns the value of attribute last.



4
5
6
# File 'lib/to_source/state.rb', line 4

def last
  @last
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/to_source/state.rb', line 41

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

#execute(command) ⇒ Object



14
15
16
17
# File 'lib/to_source/state.rb', line 14

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

#indentObject



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

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

#last_keyword?Boolean

Returns:

  • (Boolean)


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

def last_keyword?
  last.kind_of?(Command::Token::Keyword)
end

#new_lineObject



45
46
47
# File 'lib/to_source/state.rb', line 45

def new_line
  write("\n")
end

#push(command) ⇒ Object



31
32
33
34
# File 'lib/to_source/state.rb', line 31

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

#shift(width) ⇒ Object



53
54
55
56
57
# File 'lib/to_source/state.rb', line 53

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

#sourceObject



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

def source
  buffer.join('')
end

#spaceObject



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

def space
  write(' ')
end

#write(string) ⇒ Object



23
24
25
# File 'lib/to_source/state.rb', line 23

def write(string)
  @buffer << string
end