Class: Foxtail::Bundle::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/foxtail/bundle/scope.rb

Overview

Variable scope and state management during resolution Corresponds to fluent-bundle/src/scope.ts

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bundle, **args) ⇒ Scope

Returns a new instance of Scope.



14
15
16
17
18
19
20
# File 'lib/foxtail/bundle/scope.rb', line 14

def initialize(bundle, **args)
  @bundle = bundle
  @args = args      # External variables passed to format()
  @locals = {}      # Local variables (set within functions)
  @errors = []      # Error collection during resolution
  @dirty = Set.new  # Circular reference detection (message/term IDs)
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



9
10
11
# File 'lib/foxtail/bundle/scope.rb', line 9

def args
  @args
end

#bundleObject (readonly)

Returns the value of attribute bundle.



8
9
10
# File 'lib/foxtail/bundle/scope.rb', line 8

def bundle
  @bundle
end

#dirtyObject (readonly)

Returns the value of attribute dirty.



12
13
14
# File 'lib/foxtail/bundle/scope.rb', line 12

def dirty
  @dirty
end

#errorsObject (readonly)

Returns the value of attribute errors.



11
12
13
# File 'lib/foxtail/bundle/scope.rb', line 11

def errors
  @errors
end

#localsObject (readonly)

Returns the value of attribute locals.



10
11
12
# File 'lib/foxtail/bundle/scope.rb', line 10

def locals
  @locals
end

Instance Method Details

#add_error(message) ⇒ Object

Add an error to the collection



43
# File 'lib/foxtail/bundle/scope.rb', line 43

def add_error(message) = @errors << message

#all_variablesObject

Get all available variables (locals + args)



60
# File 'lib/foxtail/bundle/scope.rb', line 60

def all_variables = @args.merge(@locals)

#child_scopeObject

Create a child scope (for function calls)



49
50
51
52
53
54
# File 'lib/foxtail/bundle/scope.rb', line 49

def child_scope(**)
  child = self.class.new(@bundle, **@args, **)
  child.instance_variable_set(:@locals, @locals.dup)
  child.instance_variable_set(:@dirty, @dirty.dup)
  child
end

#clear_localsObject

Reset locals (used in some resolution contexts)



57
# File 'lib/foxtail/bundle/scope.rb', line 57

def clear_locals = @locals.clear

#release(id) ⇒ Object

Release tracking of a message/term ID



40
# File 'lib/foxtail/bundle/scope.rb', line 40

def release(id) = @dirty.delete(id)

#set_local(name, value) ⇒ Object

Set a local variable (used within functions)



26
# File 'lib/foxtail/bundle/scope.rb', line 26

def set_local(name, value) = @locals[name.to_sym] = value

#track(id) ⇒ Object

Track a message/term ID to detect circular references



29
30
31
32
33
34
35
36
37
# File 'lib/foxtail/bundle/scope.rb', line 29

def track(id)
  if @dirty.include?(id)
    add_error("Circular reference detected: #{id}")
    return false
  end

  @dirty.add(id)
  true
end

#tracking?(id) ⇒ Boolean

Check if an ID is currently being tracked (circular reference check)

Returns:

  • (Boolean)


46
# File 'lib/foxtail/bundle/scope.rb', line 46

def tracking?(id) = @dirty.include?(id)

#variable(name) ⇒ Object

Get a variable value (checks locals first, then args)



23
# File 'lib/foxtail/bundle/scope.rb', line 23

def variable(name) = @locals[name.to_sym] || @args[name.to_sym]