Class: AlgebraDB::SyntaxBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/algebra_db/syntax_builder.rb

Overview

Class that builds syntax.

Instance Method Summary collapse

Constructor Details

#initialize(params = []) ⇒ SyntaxBuilder

Returns a new instance of SyntaxBuilder.



5
6
7
8
# File 'lib/algebra_db/syntax_builder.rb', line 5

def initialize(params = [])
  @params = params
  @syntax = +''
end

Instance Method Details

#param(param) ⇒ Object



19
20
21
22
# File 'lib/algebra_db/syntax_builder.rb', line 19

def param(param)
  text "$#{@params.length + 1}"
  @params << param
end

#paramsObject



50
51
52
# File 'lib/algebra_db/syntax_builder.rb', line 50

def params
  @params.map(&:dup)
end

#parenthesize {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
44
# File 'lib/algebra_db/syntax_builder.rb', line 37

def parenthesize
  raise ArgumentError, 'need a block' unless block_given?

  text_nospace('(')
  yield self
  @syntax.strip!
  text(')')
end

#separate(listish, separator: ',') ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/algebra_db/syntax_builder.rb', line 24

def separate(listish, separator: ',')
  raise ArgumentError, 'need a block' unless block_given?

  len = listish.length
  listish.each.with_index do |e, i|
    yield e, self
    unless (i + 1) == len
      @syntax.strip!
      text(separator)
    end
  end
end

#syntaxObject



46
47
48
# File 'lib/algebra_db/syntax_builder.rb', line 46

def syntax
  @syntax.dup
end

#text(str) ⇒ Object



10
11
12
13
# File 'lib/algebra_db/syntax_builder.rb', line 10

def text(str)
  @syntax << str
  @syntax << ' '
end

#text_nospace(str) ⇒ Object



15
16
17
# File 'lib/algebra_db/syntax_builder.rb', line 15

def text_nospace(str)
  @syntax << str
end