Class: FixtureFox::Analyzer

Inherits:
Object
  • Object
show all
Defined in:
lib/fixture_fox/analyzer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, ast, ids: {}, anchors: Anchors.new(type)) ⇒ Analyzer

Returns a new instance of Analyzer.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/fixture_fox/analyzer.rb', line 42

def initialize(type, ast, ids: {}, anchors: Anchors.new(type))
  constrain type, PgGraph::Type
  constrain ast, Ast
  constrain anchors, Anchors
  @type = type
  @ast = ast
  @tables = {}
  @data_tables = {}
  @records = []
  @record_refs = []
  @fields = [] # List of all fields

  @ids = Hash.new(0)
  ids.each { |k,v| @ids[k] = v } # copy but preserve default value
  @anchors = anchors
  @defined_anchors = {}
  @referenced_anchors = {}

  @assigned = false
  @checked = false
end

Instance Attribute Details

#anchorsObject (readonly)

Anchors object. This includes both anchors defined in the sources and anchors supplied to #initialize



25
26
27
# File 'lib/fixture_fox/analyzer.rb', line 25

def anchors
  @anchors
end

#astObject (readonly)

Returns the value of attribute ast.



3
4
5
# File 'lib/fixture_fox/analyzer.rb', line 3

def ast
  @ast
end

#defined_anchorsObject (readonly)

Map from anchor name to anchors defined by the sources



28
29
30
# File 'lib/fixture_fox/analyzer.rb', line 28

def defined_anchors
  @defined_anchors
end

#fieldsObject (readonly)

AstField


15
16
17
# File 'lib/fixture_fox/analyzer.rb', line 15

def fields
  @fields
end

#idrObject (readonly)

Initialized by #call



5
6
7
# File 'lib/fixture_fox/analyzer.rb', line 5

def idr
  @idr
end

#idsObject (readonly)

Map from qualified table name to maximum record ID



18
19
20
# File 'lib/fixture_fox/analyzer.rb', line 18

def ids
  @ids
end

#record_refsObject (readonly)

AstRecordRef


14
15
16
# File 'lib/fixture_fox/analyzer.rb', line 14

def record_refs
  @record_refs
end

#recordsObject (readonly)

AstRecord


13
14
15
# File 'lib/fixture_fox/analyzer.rb', line 13

def records
  @records
end

#referenced_anchorsObject (readonly)

Map from anchor name to anchors referenced by the sources. FIXME: Unused



31
32
33
# File 'lib/fixture_fox/analyzer.rb', line 31

def referenced_anchors
  @referenced_anchors
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/fixture_fox/analyzer.rb', line 4

def type
  @type
end

Instance Method Details

#assign_typesObject

Assign types and collect fields, records, and anchors. Note that #assign_types and #check_types are separate methods to make it possible to analyze the AST just up to the point where external anchors are needed for further processing. This is used in the caching mechanism in Postspec



75
76
77
78
79
# File 'lib/fixture_fox/analyzer.rb', line 75

def assign_types
  @assigned = true
  assign_table_types
  self
end

#assigned?Boolean

True iff types have been assigned

Returns:

  • (Boolean)


34
# File 'lib/fixture_fox/analyzer.rb', line 34

def assigned?() @assigned end

#callObject

TODO: Also generate data



65
66
67
68
69
# File 'lib/fixture_fox/analyzer.rb', line 65

def call
  assign_types
  check_types
  self
end

#check_types(anchors: nil) ⇒ Object

Check types. Merges anchors: into self.anchors



82
83
84
85
86
87
88
# File 'lib/fixture_fox/analyzer.rb', line 82

def check_types(anchors: nil)
  @checked = true
  @anchors.merge!(anchors) if anchors
  check_field_types
  check_record_ref_types
  self
end

#checked?Boolean

True iff types have been checked

Returns:

  • (Boolean)


37
# File 'lib/fixture_fox/analyzer.rb', line 37

def checked?() @checked end

#data_tablesObject

List of non-empty tables (PgGraph::Type::Table objects)



11
# File 'lib/fixture_fox/analyzer.rb', line 11

def data_tables() @data_tables.values end

#generate(ids: nil) ⇒ Object

Generate IDR. Use self.ids if ids: is nil



91
92
93
94
95
96
# File 'lib/fixture_fox/analyzer.rb', line 91

def generate(ids: nil)
  ids.each { |k,v| @ids[k] = v } if ids # copy while preserving default value in hash
  generate_record_ids
  generate_idr
  @idr
end

#generated?Boolean

True iff data has been generated

Returns:

  • (Boolean)


40
# File 'lib/fixture_fox/analyzer.rb', line 40

def generated?() !@idr.nil? end

#tablesObject

List of all known tables whether empty or not (PgGraph::Type::Table objects)



8
# File 'lib/fixture_fox/analyzer.rb', line 8

def tables() @tables.values end