Class: RailVerdict::RailsContext::Context
- Inherits:
-
Object
- Object
- RailVerdict::RailsContext::Context
- Defined in:
- lib/rail_verdict/rails_context/context.rb
Instance Attribute Summary collapse
-
#detected ⇒ Object
readonly
Returns the value of attribute detected.
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
-
#unresolved ⇒ Object
readonly
Returns the value of attribute unresolved.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(detected:, scope:, entries:, unresolved:) ⇒ Context
constructor
A new instance of Context.
- #to_h ⇒ Object
Constructor Details
#initialize(detected:, scope:, entries:, unresolved:) ⇒ Context
Returns a new instance of Context.
78 79 80 81 82 83 84 |
# File 'lib/rail_verdict/rails_context/context.rb', line 78 def initialize(detected:, scope:, entries:, unresolved:) @detected = deep_freeze(detected) @scope = scope.dup.freeze @entries = deep_freeze(entries) @unresolved = deep_freeze(unresolved) freeze end |
Instance Attribute Details
#detected ⇒ Object (readonly)
Returns the value of attribute detected.
6 7 8 |
# File 'lib/rail_verdict/rails_context/context.rb', line 6 def detected @detected end |
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
6 7 8 |
# File 'lib/rail_verdict/rails_context/context.rb', line 6 def entries @entries end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
6 7 8 |
# File 'lib/rail_verdict/rails_context/context.rb', line 6 def scope @scope end |
#unresolved ⇒ Object (readonly)
Returns the value of attribute unresolved.
6 7 8 |
# File 'lib/rail_verdict/rails_context/context.rb', line 6 def unresolved @unresolved end |
Class Method Details
.build(repository_root:, git_context: nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rail_verdict/rails_context/context.rb', line 8 def self.build(repository_root:, git_context: nil) root = File.realpath(repository_root.to_s) rescue repository_root.to_s detected = Detector.detect(repository_root: root) scope = git_context ? "changed" : "full" if git_context.nil? return new(detected: detected, scope: scope, entries: [], unresolved: []) end changed = git_context.changed_files.map(&:path).compact.sort rails_changed = changed.select { |path| Classifier.classify(path) != "unknown" } rails_changed = rails_changed.reject do |path| full = File.join(root, path) File.file?(full) && File.size(full) > Limits::MAX_FILE_BYTES rescue false end.first(Limits::MAX_FILES) entries = rails_changed.map do |source_path| kind = Classifier.classify(source_path) constant = ConstantInferencer.infer(kind: kind, path: source_path) = [] unresolved = [] .concat(Resolvers::TestCandidates.for(repository_root: root, kind: kind, path: source_path)) .concat(Resolvers::PolicyResolver.for(repository_root: root, kind: kind, path: source_path, constant: constant)) .concat(Resolvers::ViewResolver.for(repository_root: root, kind: kind, path: source_path, constant: constant)) .concat(Resolvers::SchemaResolver.for(repository_root: root, kind: kind, constant: constant, path: source_path)) if kind == "model" assocs = Resolvers::AssociationExtractor.extract(repository_root: root, path: source_path) assocs.each do |assoc| << { "path" => source_path, "relationship" => "association:#{assoc['macro']} :#{assoc['name']}", "confidence" => assoc["confidence"], "provenance" => assoc["provenance"] } end end if kind == "controller" || source_path == "config/routes.rb" .concat(Resolvers::RouteResolver.for(repository_root: root, kind: kind, constant: constant)) end = .first(Limits::MAX_RELATED).sort_by { |item| [item["path"], item["relationship"]] } scan = kind == "controller" ? Resolvers::RouteScanner.scan(repository_root: root) : nil if scan && scan[:unresolved] unresolved << { "source_path" => source_path, "reason" => "dynamic_route" } end { "source_path" => source_path, "kind" => kind, "constant" => constant, "confidence" => constant ? "exact" : "conventional", "provenance" => "path:#{source_path}", "related" => , "unresolved" => unresolved } end.sort_by { |entry| entry["source_path"] } unresolved_global = [] route_scan = Resolvers::RouteScanner.scan(repository_root: root) if route_scan[:unresolved] unresolved_global << { "source_path" => "config/routes.rb", "reason" => "dynamic_dsl" } end has_structure = File.file?(File.join(root, "db/structure.sql")) if has_structure unresolved_global << { "source_path" => "db/structure.sql", "reason" => "structure_sql_not_parsed" } end new(detected: detected, scope: scope, entries: entries, unresolved: unresolved_global) rescue StandardError fallback_detected = begin; Detector.detect(repository_root: root); rescue StandardError; {}; end new(detected: fallback_detected, scope: git_context ? "changed" : "full", entries: [], unresolved: []) end |
Instance Method Details
#to_h ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/rail_verdict/rails_context/context.rb', line 86 def to_h result = { "detected" => detected, "scope" => scope, "entries" => entries } result["unresolved"] = unresolved unless unresolved.empty? result end |