Class: Refinement::Changeset::FileModification
- Inherits:
-
Object
- Object
- Refinement::Changeset::FileModification
- Defined in:
- lib/refinement/changeset/file_modification.rb
Overview
Represents a modification to a single file or directory on disk
Constant Summary collapse
- DIRECTORY_CHANGE_TYPE =
Returns the change type for directories.
:'had contents change'
Instance Attribute Summary collapse
-
#path ⇒ Pathname
readonly
The path to the modified file.
-
#prior_path ⇒ Pathname, Nil
readonly
The prior path to the modified file, or ‘nil` if it was not renamed or copied.
-
#type ⇒ #to_s
readonly
The type of change that happened to this file.
Instance Method Summary collapse
-
#contents ⇒ String
The current contents of the file.
-
#initialize(path:, type:, prior_path: nil, contents_reader: -> { nil }, prior_contents_reader: -> { nil }) ⇒ FileModification
constructor
A new instance of FileModification.
-
#prior_contents ⇒ String
The prior contents of the file.
-
#yaml_diff(keypath) ⇒ String, Nil
A YAML string representing the diff of the file from the prior revision to the current revision at the given keypath in the YAML, or ‘nil` if there is no diff.
Constructor Details
#initialize(path:, type:, prior_path: nil, contents_reader: -> { nil }, prior_contents_reader: -> { nil }) ⇒ FileModification
Returns a new instance of FileModification.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/refinement/changeset/file_modification.rb', line 17 def initialize(path:, type:, prior_path: nil, contents_reader: -> { nil }, prior_contents_reader: -> { nil }) @path = path @type = type @prior_path = prior_path @contents_reader = contents_reader @prior_contents_reader = prior_contents_reader end |
Instance Attribute Details
#path ⇒ Pathname (readonly)
Returns the path to the modified file.
9 10 11 |
# File 'lib/refinement/changeset/file_modification.rb', line 9 def path @path end |
#prior_path ⇒ Pathname, Nil (readonly)
Returns the prior path to the modified file, or ‘nil` if it was not renamed or copied.
12 13 14 |
# File 'lib/refinement/changeset/file_modification.rb', line 12 def prior_path @prior_path end |
#type ⇒ #to_s (readonly)
Returns the type of change that happened to this file.
15 16 17 |
# File 'lib/refinement/changeset/file_modification.rb', line 15 def type @type end |
Instance Method Details
#contents ⇒ String
Returns the current contents of the file.
111 112 113 114 115 116 117 118 |
# File 'lib/refinement/changeset/file_modification.rb', line 111 def contents @contents ||= begin @contents_reader[].tap { @contents_reader = nil } || DOES_NOT_EXIST rescue StandardError DOES_NOT_EXIST end end |
#prior_contents ⇒ String
Returns the prior contents of the file.
121 122 123 124 125 126 127 128 |
# File 'lib/refinement/changeset/file_modification.rb', line 121 def prior_contents @prior_contents ||= begin @prior_contents_reader[].tap { @prior_contents_reader = nil } || DOES_NOT_EXIST rescue StandardError DOES_NOT_EXIST end end |
#yaml_diff(keypath) ⇒ String, Nil
Returns a YAML string representing the diff of the file from the prior revision to the current revision at the given keypath in the YAML, or ‘nil` if there is no diff.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/refinement/changeset/file_modification.rb', line 68 def yaml_diff(keypath) require 'yaml' dig_yaml = lambda do |yaml| return yaml if DOES_NOT_EXIST == yaml object = YAML.safe_load(yaml, [Symbol]) if keypath.empty? object elsif object.respond_to?(:dig) object.dig(*keypath) else # backwards compatibility keypath.reduce(object) do |acc, elem| acc[elem] end end end prior = dig_yaml[prior_contents] current = dig_yaml[contents] require 'xcodeproj/differ' return unless (diff = Xcodeproj::Differ.diff( prior, current, key_1: 'prior_revision', key_2: 'current_revision' )) diff.to_yaml.prepend("#{path} changed at keypath #{keypath.inspect}\n") end |