Class: TurboTest::StaticAnalysis::ActiveRecord::SexpBuilder

Inherits:
LineStackSexpBuilder
  • Object
show all
Defined in:
lib/turbo_test_static_analysis/active_record_schema/sexp_builder/sexp_builder.rb

Constant Summary collapse

COMMANDS =
%w[add_index add_foreign_key enable_extension].freeze
TABLE_REGEXP =
/create_table\s"(\w+)"/.freeze
TRIGGER_REGEXP =
/create_trigger\("(\w+)".+on\("(\w+)"/.freeze
COMMAND_REGEXP =
/\b(\w+)\b/.freeze

Constants inherited from LineStackSexpBuilder

LineStackSexpBuilder::ARITIES, LineStackSexpBuilder::EVENTS_TO_REJECT

Instance Attribute Summary collapse

Attributes inherited from LineStackSexpBuilder

#stack

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from LineStackSexpBuilder

#stack_line

Constructor Details

#initialize(path, filename = "-", lineno = 1) ⇒ SexpBuilder

Returns a new instance of SexpBuilder.



18
19
20
21
22
# File 'lib/turbo_test_static_analysis/active_record_schema/sexp_builder/sexp_builder.rb', line 18

def initialize(path, filename = "-", lineno = 1)
  super
  @schema_file = path.split("\n")
  @schema = Constructor.new
end

Instance Attribute Details

#schemaObject

Returns the value of attribute schema.



16
17
18
# File 'lib/turbo_test_static_analysis/active_record_schema/sexp_builder/sexp_builder.rb', line 16

def schema
  @schema
end

Class Method Details

.snapshot_from_file(path) ⇒ Object



24
25
26
27
28
# File 'lib/turbo_test_static_analysis/active_record_schema/sexp_builder/sexp_builder.rb', line 24

def self.snapshot_from_file(path)
  builder = new(File.read(path), path)
  builder.parse
  builder.snapshot
end

.snapshot_from_source(source) ⇒ Object



30
31
32
33
34
# File 'lib/turbo_test_static_analysis/active_record_schema/sexp_builder/sexp_builder.rb', line 30

def self.snapshot_from_source(source)
  builder = new(source)
  builder.parse
  builder.snapshot
end

Instance Method Details

#on_command(token_one, token_two) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/turbo_test_static_analysis/active_record_schema/sexp_builder/sexp_builder.rb', line 40

def on_command(token_one, token_two)
  super.tap do |_result|
    name = token_one[1]
    next unless COMMANDS.include? name

    last_line = @stack.remove_greater_than([lineno, column])
    content = method_content(lineno, last_line[0])
    @schema.send(name.to_sym, table_name(:command, token_two), content)
  end
end

#on_method_add_block(token_one, token_two) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/turbo_test_static_analysis/active_record_schema/sexp_builder/sexp_builder.rb', line 51

def on_method_add_block(token_one, token_two)
  super.tap do |_result|
    next unless (type = type_for_token(token_one[0]))

    first_line, last_line = send("#{type}_lines", token_one)
    handle_create(type, token_one, first_line, last_line)
  end
end

#snapshotObject



36
37
38
# File 'lib/turbo_test_static_analysis/active_record_schema/sexp_builder/sexp_builder.rb', line 36

def snapshot
  @schema.snapshot
end