|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative 'common' |
| 4 | + |
| 5 | +module OctocatalogDiff |
| 6 | + module API |
| 7 | + module V1 |
| 8 | + # This class represents a `diff` produced by a catalog-diff operation. This has traditionally |
| 9 | + # been stored as an array with: |
| 10 | + # [0] Type of change - '+', '-', '!', '~' |
| 11 | + # [1] Type, title, and maybe structure, delimited by "\f" |
| 12 | + # [2] Content of the "old" catalog |
| 13 | + # [3] Content of the "new" catalog |
| 14 | + # [4] File and line of the "old" catalog |
| 15 | + # [5] File and line of the "new" catalog |
| 16 | + # This object seeks to preserve this traditional structure, while providing methods to make it |
| 17 | + # easier to deal with. |
| 18 | + class Diff |
| 19 | + attr_reader :raw |
| 20 | + |
| 21 | + # Constructor: Accepts a diff in the traditional array format and stores it. |
| 22 | + # @param raw [Array] Diff in the traditional format |
| 23 | + def initialize(raw) |
| 24 | + unless raw.is_a?(Array) |
| 25 | + raise ArgumentError, 'OctocatalogDiff::API::V1::Diff#initialize expects Array argument' |
| 26 | + end |
| 27 | + @raw = raw |
| 28 | + end |
| 29 | + |
| 30 | + # Public: Retrieve an indexed value from the array |
| 31 | + # @return [?] Indexed value |
| 32 | + def [](i) |
| 33 | + @raw[i] |
| 34 | + end |
| 35 | + |
| 36 | + # Public: Get the change type |
| 37 | + # @return [String] Change type symbol (~, !, +, -) |
| 38 | + def change_type |
| 39 | + @raw[0] |
| 40 | + end |
| 41 | + |
| 42 | + # Public: Get the change type (English word) |
| 43 | + # @return [String] Change type word |
| 44 | + def change_type_word |
| 45 | + return 'change' if @raw[0] == '!' || @raw[0] == '~' |
| 46 | + return 'addition' if @raw[0] == '+' |
| 47 | + return 'removal' if @raw[0] == '-' |
| 48 | + raise ArgumentError, "No change type corresponds to #{@raw[0].inspect}" |
| 49 | + end |
| 50 | + |
| 51 | + # Public: Get the type_title string |
| 52 | + # @return [?] Type_title_structure |
| 53 | + def type_title |
| 54 | + @raw[1] |
| 55 | + end |
| 56 | + |
| 57 | + # Public: Get the resource type |
| 58 | + # @return [String] Resource type |
| 59 | + def type |
| 60 | + @raw[1].split(/\f/)[0] |
| 61 | + end |
| 62 | + |
| 63 | + # Public: Get the resource title |
| 64 | + # @return [String] Resource title |
| 65 | + def title |
| 66 | + @raw[1].split(/\f/)[1] |
| 67 | + end |
| 68 | + |
| 69 | + # Public: Get the structure of the resource as an array |
| 70 | + # @return [Array] Structure of resource |
| 71 | + def structure |
| 72 | + @raw[1].split(/\f/)[2..-1] |
| 73 | + end |
| 74 | + |
| 75 | + # Public: Get the "old" value, i.e. "from" catalog |
| 76 | + # @return [?] "old" value |
| 77 | + def old_value |
| 78 | + return nil if @raw[0] == '+' |
| 79 | + @raw[2] |
| 80 | + end |
| 81 | + |
| 82 | + # Public: Get the "new" value, i.e. "to" catalog |
| 83 | + # @return [?] "old" value |
| 84 | + def new_value |
| 85 | + return nil if @raw[0] == '-' |
| 86 | + return @raw[2] if @raw[0] == '+' |
| 87 | + @raw[3] |
| 88 | + end |
| 89 | + |
| 90 | + # Public: Get the "old" location, i.e. location in the "from" catalog |
| 91 | + # @return [Hash] <file:, line:> of resource |
| 92 | + def old_location |
| 93 | + return nil if @raw[0] == '+' |
| 94 | + return @raw[3] if @raw[0] == '-' |
| 95 | + @raw[4] |
| 96 | + end |
| 97 | + |
| 98 | + # Public: Get the "new" location, i.e. location in the "to" catalog |
| 99 | + # @return [Hash] <file:, line:> of resource |
| 100 | + def new_location |
| 101 | + return @raw[3] if @raw[0] == '+' |
| 102 | + return nil if @raw[0] == '-' |
| 103 | + @raw[5] |
| 104 | + end |
| 105 | + |
| 106 | + # Public: Get the filename from the "old" location |
| 107 | + # @return [String] Filename |
| 108 | + def old_file |
| 109 | + x = old_location |
| 110 | + x.nil? ? nil : x['file'] |
| 111 | + end |
| 112 | + |
| 113 | + # Public: Get the line number from the "old" location |
| 114 | + # @return [String] Line number |
| 115 | + def old_line |
| 116 | + x = old_location |
| 117 | + x.nil? ? nil : x['line'] |
| 118 | + end |
| 119 | + |
| 120 | + # Public: Get the filename from the "new" location |
| 121 | + # @return [String] Filename |
| 122 | + def new_file |
| 123 | + x = new_location |
| 124 | + x.nil? ? nil : x['file'] |
| 125 | + end |
| 126 | + |
| 127 | + # Public: Get the line number from the "new" location |
| 128 | + # @return [String] Line number |
| 129 | + def new_line |
| 130 | + x = new_location |
| 131 | + x.nil? ? nil : x['line'] |
| 132 | + end |
| 133 | + end |
| 134 | + end |
| 135 | + end |
| 136 | +end |
0 commit comments