Class: Prism::Relocation::Repository
- Inherits:
-
Object
- Object
- Prism::Relocation::Repository
- 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.
Defined Under Namespace
Classes: ConfigurationError
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
The entries that have been saved on this repository.
-
#fields ⇒ Object
readonly
The fields that have been configured on this repository.
-
#source ⇒ Object
readonly
The source associated with this repository.
Instance Method Summary collapse
-
#character_columns ⇒ Object
Configure the character columns field for this repository and return self.
-
#character_offsets ⇒ Object
Configure the character offsets field for this repository and return self.
-
#code_unit_columns(encoding) ⇒ Object
Configure the code unit columns field for this repository for a specific encoding and return self.
-
#code_unit_offsets(encoding) ⇒ Object
Configure the code unit offsets field for this repository for a specific encoding and return self.
-
#code_units_cache(encoding) ⇒ Object
Create a code units cache for the given encoding from the source.
-
#columns ⇒ Object
Configure the columns field for this repository and return self.
-
#comments ⇒ Object
Configure both the leading and trailing comment fields for this repository and return self.
-
#enter(node_id, field_name) ⇒ Object
This method is called from nodes and locations when they want to enter themselves into the repository.
-
#filepath ⇒ Object
Configure the filepath field for this repository and return self.
-
#initialize(source) ⇒ Repository
constructor
Initialize a new repository with the given source.
-
#leading_comments ⇒ Object
Configure the leading comments field for this repository and return self.
-
#lines ⇒ Object
Configure the lines field for this repository and return self.
-
#offsets ⇒ Object
Configure the offsets field for this repository and return self.
-
#reify! ⇒ Object
This method is called from the entries in the repository when they need to reify their values.
-
#trailing_comments ⇒ Object
Configure the trailing comments field for this repository and return self.
Constructor Details
#initialize(source) ⇒ Repository
Initialize a new repository with the given source.
370 371 372 373 374 |
# File 'lib/prism/relocation.rb', line 370 def initialize(source) @source = source @fields = {} @entries = Hash.new { |hash, node_id| hash[node_id] = {} } end |
Instance Attribute Details
#entries ⇒ Object (readonly)
The entries that have been saved on this repository.
367 368 369 |
# File 'lib/prism/relocation.rb', line 367 def entries @entries end |
#fields ⇒ Object (readonly)
The fields that have been configured on this repository.
364 365 366 |
# File 'lib/prism/relocation.rb', line 364 def fields @fields end |
#source ⇒ Object (readonly)
The source associated with this repository. This will be either a SourceFilepath (the most common use case) or a SourceString.
361 362 363 |
# File 'lib/prism/relocation.rb', line 361 def source @source end |
Instance Method Details
#character_columns ⇒ Object
Configure the character columns field for this repository and return self.
416 417 418 |
# File 'lib/prism/relocation.rb', line 416 def character_columns field(:character_columns, CharacterColumnsField.new) end |
#character_offsets ⇒ Object
Configure the character offsets field for this repository and return self.
399 400 401 |
# File 'lib/prism/relocation.rb', line 399 def character_offsets field(:character_offsets, CharacterOffsetsField.new) end |
#code_unit_columns(encoding) ⇒ Object
Configure the code unit columns field for this repository for a specific encoding and return self.
422 423 424 |
# File 'lib/prism/relocation.rb', line 422 def code_unit_columns(encoding) field(:code_unit_columns, CodeUnitColumnsField.new(self, encoding)) end |
#code_unit_offsets(encoding) ⇒ Object
Configure the code unit offsets field for this repository for a specific encoding and return self.
405 406 407 |
# File 'lib/prism/relocation.rb', line 405 def code_unit_offsets(encoding) field(:code_unit_offsets, CodeUnitOffsetsField.new(self, encoding)) end |
#code_units_cache(encoding) ⇒ Object
Create a code units cache for the given encoding from the source.
377 378 379 |
# File 'lib/prism/relocation.rb', line 377 def code_units_cache(encoding) source.code_units_cache(encoding) end |
#columns ⇒ Object
Configure the columns field for this repository and return self.
410 411 412 |
# File 'lib/prism/relocation.rb', line 410 def columns field(:columns, ColumnsField.new) end |
#comments ⇒ Object
Configure both the leading and trailing comment fields for this repository and return self.
440 441 442 |
# File 'lib/prism/relocation.rb', line 440 def comments leading_comments.trailing_comments end |
#enter(node_id, field_name) ⇒ Object
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.
447 448 449 450 451 |
# File 'lib/prism/relocation.rb', line 447 def enter(node_id, field_name) # :nodoc: entry = Entry.new(self) @entries[node_id][field_name] = entry entry end |
#filepath ⇒ Object
Configure the filepath field for this repository and return self.
382 383 384 385 |
# File 'lib/prism/relocation.rb', line 382 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 ⇒ Object
Configure the leading comments field for this repository and return self.
428 429 430 |
# File 'lib/prism/relocation.rb', line 428 def leading_comments field(:leading_comments, LeadingCommentsField.new) end |
#lines ⇒ Object
Configure the lines field for this repository and return self.
388 389 390 |
# File 'lib/prism/relocation.rb', line 388 def lines field(:lines, LinesField.new) end |
#offsets ⇒ Object
Configure the offsets field for this repository and return self.
393 394 395 |
# File 'lib/prism/relocation.rb', line 393 def offsets field(:offsets, OffsetsField.new) end |
#reify! ⇒ Object
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.
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
# File 'lib/prism/relocation.rb', line 456 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 = {} #: Hash[Symbol, untyped] 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 ⇒ Object
Configure the trailing comments field for this repository and return self.
434 435 436 |
# File 'lib/prism/relocation.rb', line 434 def trailing_comments field(:trailing_comments, TrailingCommentsField.new) end |