Class: ActiverecordCallbackLens::Parser::ConditionParser

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord_callback_lens/parser/condition_parser.rb

Overview

Turns a CallbackDefinition’s raw if/unless conditions into a structured ConditionTree.

Proc/Lambda conditions are parsed by reading their source file with Prism, isolating the enclosing lambda/block node by line number, and recursively mapping the boolean AST (‘&&`, `||`, `!`, predicate calls) onto ConditionTree nodes. Symbol conditions become MethodRefNodes whose expansion is deferred to the MethodResolver. Any other entry kind (e.g. ActiveModel’s framework injected Conditionals::Value guard on after_* callbacks) is ignored.

Parsing never raises on missing or unreadable source: a nil source_location or a missing file is skipped silently, and a Prism parse failure warns to $stderr while leaving the condition unresolved.

Defined Under Namespace

Classes: LambdaLocator

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition) ⇒ ConditionParser

Returns a new instance of ConditionParser.

Parameters:



30
31
32
# File 'lib/activerecord_callback_lens/parser/condition_parser.rb', line 30

def initialize(definition)
  @definition = definition
end

Class Method Details

.parse(definition) ⇒ Collector::CallbackDefinition

Returns a copy with condition_tree populated.

Parameters:

Returns:



25
26
27
# File 'lib/activerecord_callback_lens/parser/condition_parser.rb', line 25

def self.parse(definition)
  new(definition).parse
end

Instance Method Details

#parseCollector::CallbackDefinition

Returns a copy with condition_tree set.

Returns:



35
36
37
# File 'lib/activerecord_callback_lens/parser/condition_parser.rb', line 35

def parse
  @definition.with(condition_tree: build_tree)
end