123456789_123456789_123456789_123456789_123456789_

Class: Prism::Relocation::Repository

Relationships & Source Files
Namespace Children
Exceptions:
Inherits: Object
Defined in: lib/prism/relocation.rb

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.

Class Method Summary

Instance Attribute Summary

  • #entries readonly

    The entries that have been saved on this repository.

  • #fields readonly

    The fields that have been configured on this repository.

  • #source readonly

    The source associated with this repository.

Instance Method Summary

  • #character_columns

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

  • #character_offsets

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

  • #code_unit_columns(encoding)

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

  • #code_unit_offsets(encoding)

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

  • #code_units_cache(encoding)

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

  • #columns

    Configure the columns field for this repository and return self.

  • #comments

    Configure both the leading and trailing comment fields for this repository and return self.

  • #filepath

    Configure the filepath field for this repository and return self.

  • #leading_comments

    Configure the leading comments field for this repository and return self.

  • #lines

    Configure the lines field for this repository and return self.

  • #offsets

    Configure the offsets field for this repository and return self.

  • #trailing_comments

    Configure the trailing comments field for this repository and return self.

  • #field(name, value) private

    Append the given field to the repository and return the repository so that these calls can be chained.

  • #enter(node_id, field_name) Internal use only

    This method is called from nodes and locations when they want to enter themselves into the repository.

  • #reify! Internal use only

    This method is called from the entries in the repository when they need to reify their values.

Constructor Details

.new(source) ⇒ Repository

Initialize a new repository with the given source.

[ GitHub ]

  
# File 'lib/prism/relocation.rb', line 369

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

Instance Attribute Details

#entries (readonly)

The entries that have been saved on this repository.

[ GitHub ]

  
# File 'lib/prism/relocation.rb', line 366

attr_reader :entries

#fields (readonly)

The fields that have been configured on this repository.

[ GitHub ]

  
# File 'lib/prism/relocation.rb', line 363

attr_reader :fields

#source (readonly)

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

[ GitHub ]

  
# File 'lib/prism/relocation.rb', line 360

attr_reader :source

Instance Method Details

#character_columns

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

[ GitHub ]

  
# File 'lib/prism/relocation.rb', line 415

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

#character_offsets

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

[ GitHub ]

  
# File 'lib/prism/relocation.rb', line 398

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

#code_unit_columns(encoding)

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

[ GitHub ]

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

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

#code_unit_offsets(encoding)

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

[ GitHub ]

  
# File 'lib/prism/relocation.rb', line 404

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

#code_units_cache(encoding)

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

[ GitHub ]

  
# File 'lib/prism/relocation.rb', line 376

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

#columns

Configure the columns field for this repository and return self.

[ GitHub ]

  
# File 'lib/prism/relocation.rb', line 409

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

#comments

Configure both the leading and trailing comment fields for this repository and return self.

[ GitHub ]

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

def comments
  leading_comments.trailing_comments
end

#enter(node_id, field_name)

This method is for internal use only.

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.

[ GitHub ]

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

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

#field(name, value) (private)

Append the given field to the repository and return the repository so that these calls can be chained.

Raises:

[ GitHub ]

  
# File 'lib/prism/relocation.rb', line 487

def field(name, value)
  raise ConfigurationError, "Cannot specify multiple #{name} fields" if @fields.key?(name)
  @fields[name] = value
  self
end

#filepath

Configure the filepath field for this repository and return self.

Raises:

[ GitHub ]

  
# File 'lib/prism/relocation.rb', line 381

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

#leading_comments

Configure the leading comments field for this repository and return self.

[ GitHub ]

  
# File 'lib/prism/relocation.rb', line 427

def leading_comments
  field(:leading_comments, LeadingCommentsField.new)
end

#lines

Configure the lines field for this repository and return self.

[ GitHub ]

  
# File 'lib/prism/relocation.rb', line 387

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

#offsets

Configure the offsets field for this repository and return self.

[ GitHub ]

  
# File 'lib/prism/relocation.rb', line 392

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

#reify!

This method is for internal use only.

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.

[ GitHub ]

  
# File 'lib/prism/relocation.rb', line 455

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

  # Attach the comments if they have been requested as part of the
  # configuration of this repository.
  if fields.key?(:leading_comments) || fields.key?(:trailing_comments)
    result.attach_comments!
  end

  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 = {}

      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

#trailing_comments

Configure the trailing comments field for this repository and return self.

[ GitHub ]

  
# File 'lib/prism/relocation.rb', line 433

def trailing_comments
  field(:trailing_comments, TrailingCommentsField.new)
end