Class: ElasticGraph::SchemaDefinition::SchemaArtifact

Inherits:
Object
  • Object
show all
Defined in:
lib/elastic_graph/schema_definition/schema_artifact_manager.rb

Instance Method Summary collapse

Instance Method Details

#diff(color:) ⇒ Object



459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/elastic_graph/schema_definition/schema_artifact_manager.rb', line 459

def diff(color:)
  return nil unless exists?

  ::Tempfile.create do |f|
    f.write(dumped_contents.chomp)
    f.fsync

    `git diff --no-index #{file_name} #{f.path}#{" --color" if color}`
      .gsub(file_name, "existing_contents")
      .gsub(f.path, "/updated_contents")
  end
end

#dump(output) ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
# File 'lib/elastic_graph/schema_definition/schema_artifact_manager.rb', line 435

def dump(output)
  if out_of_date?
    dirname = File.dirname(file_name)
    FileUtils.mkdir_p(dirname) # Create directory if needed.

    ::File.write(file_name, dumped_contents)
    output.puts "Dumped schema artifact to `#{file_name}`."
  else
    output.puts "`#{file_name}` is already up to date."
  end
end

#existing_dumped_contentsObject



451
452
453
454
455
456
457
# File 'lib/elastic_graph/schema_definition/schema_artifact_manager.rb', line 451

def existing_dumped_contents
  return nil unless exists?

  # We drop the first 2 lines because it is the comment block containing dynamic elements.
  file_contents = ::File.read(file_name).split("\n").drop(2).join("\n")
  loader.call(file_contents)
end

#out_of_date?Boolean

Returns:

  • (Boolean)


447
448
449
# File 'lib/elastic_graph/schema_definition/schema_artifact_manager.rb', line 447

def out_of_date?
  (_ = existing_dumped_contents) != desired_contents
end