Class: Prism::Relocation::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/prism/relocation.rb,
sig/generated/prism/relocation.rbs

Overview

A repository is a configured collection of fields and a set of entries that knows how to reparse a source and reify the values.

Defined Under Namespace

Classes: ConfigurationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Repository

Initialize a new repository with the given source.

: (Source source) -> void

Parameters:



428
429
430
431
432
# File 'lib/prism/relocation.rb', line 428

def initialize(source)
  @source = source
  @fields = {}
  @entries = Hash.new { |hash, node_id| hash[node_id] = {} }
end

Instance Attribute Details

#entries ⇒ Hash[Integer, Hash[Symbol, Entry]] (readonly)

The entries that have been saved on this repository.

Returns:

  • (Hash[Integer, Hash[Symbol, Entry]])


423
424
425
# File 'lib/prism/relocation.rb', line 423

def entries
  @entries
end

#fields ⇒ Hash[Symbol, _Field] (readonly)

The fields that have been configured on this repository.

Returns:



420
421
422
# File 'lib/prism/relocation.rb', line 420

def fields
  @fields
end

#source ⇒ Source (readonly)

The source associated with this repository. This will be either a SourceFilepath (the most common use case) or a SourceString.

Returns:



417
418
419
# File 'lib/prism/relocation.rb', line 417

def source
  @source
end

Instance Method Details

#character_columns ⇒ self

Configure the character columns field for this repository and return self.

: () -> self

Returns:

  • (self)


490
491
492
# File 'lib/prism/relocation.rb', line 490

def character_columns
  field(:character_columns, CharacterColumnsField.new)
end

#character_offsets ⇒ self

Configure the character offsets field for this repository and return self.

: () -> self

Returns:

  • (self)


467
468
469
# File 'lib/prism/relocation.rb', line 467

def character_offsets
  field(:character_offsets, CharacterOffsetsField.new)
end

#code_unit_columns(encoding) ⇒ self

Configure the code unit columns field for this repository for a specific encoding and return self.

: (Encoding encoding) -> self

Parameters:

  • encoding (Encoding)

Returns:

  • (self)


498
499
500
# File 'lib/prism/relocation.rb', line 498

def code_unit_columns(encoding)
  field(:code_unit_columns, CodeUnitColumnsField.new(self, encoding))
end

#code_unit_offsets(encoding) ⇒ self

Configure the code unit offsets field for this repository for a specific encoding and return self.

: (Encoding encoding) -> self

Parameters:

  • encoding (Encoding)

Returns:

  • (self)


475
476
477
# File 'lib/prism/relocation.rb', line 475

def code_unit_offsets(encoding)
  field(:code_unit_offsets, CodeUnitOffsetsField.new(self, encoding))
end

#code_units_cache(encoding) ⇒ _CodeUnitsCache

Create a code units cache for the given encoding from the source.

: (Encoding encoding) -> _CodeUnitsCache

Parameters:

  • encoding (Encoding)

Returns:



437
438
439
# File 'lib/prism/relocation.rb', line 437

def code_units_cache(encoding)
  source.code_units_cache(encoding)
end

#columns ⇒ self

Configure the columns field for this repository and return self.

: () -> self

Returns:

  • (self)


482
483
484
# File 'lib/prism/relocation.rb', line 482

def columns
  field(:columns, ColumnsField.new)
end

#enter(node_id, field_name) ⇒ Entry

This method is called from nodes and locations when they want to enter themselves into the repository. It it internal-only and meant to be called from the #save* APIs.

: (Integer node_id, Symbol field_name) -> Entry

Parameters:

  • node_id (Integer)
  • field_name (Symbol)

Returns:



507
508
509
510
511
# File 'lib/prism/relocation.rb', line 507

def enter(node_id, field_name) # :nodoc:
  entry = Entry.new(self)
  @entries[node_id][field_name] = entry
  entry
end

#filepath ⇒ self

Configure the filepath field for this repository and return self.

: () -> self

Returns:

  • (self)


444
445
446
447
# File 'lib/prism/relocation.rb', line 444

def filepath
  raise ConfigurationError, "Can only specify filepath for a filepath source" unless source.is_a?(SourceFilepath)
  field(:filepath, FilepathField.new(source.value))
end

#lines ⇒ self

Configure the lines field for this repository and return self.

: () -> self

Returns:

  • (self)


452
453
454
# File 'lib/prism/relocation.rb', line 452

def lines
  field(:lines, LinesField.new)
end

#offsets ⇒ self

Configure the offsets field for this repository and return self.

: () -> self

Returns:

  • (self)


459
460
461
# File 'lib/prism/relocation.rb', line 459

def offsets
  field(:offsets, OffsetsField.new)
end

#reify! ⇒ void

This method returns an undefined value.

This method is called from the entries in the repository when they need to reify their values. It is internal-only and meant to be called from the various value APIs.

: () -> void



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# File 'lib/prism/relocation.rb', line 518

def reify! # :nodoc:
  result = source.result

  queue = [result.value] #: Array[Prism::node]
  while (node = queue.shift)
    @entries[node.node_id].each do |field_name, entry|
      value = node.public_send(field_name)
      values = {} #: entry_values

      fields.each_value do |field|
        values.merge!(field.fields(value))
      end

      entry.reify!(values)
    end

    queue.concat(node.compact_child_nodes)
  end

  @entries.clear
end