Class: OctocatalogDiff::API::V1::Diff

Inherits:
Object
  • Object
show all
Defined in:
lib/octocatalog-diff/api/v1/diff.rb

Overview

This class represents a `diff` produced by a catalog-diff operation. This has traditionally been stored as an array with:

[0] Type of change - '+', '-', '!', '~'
[1] Type, title, and maybe structure, delimited by "\f"
[2] Content of the "old" catalog
[3] Content of the "new" catalog
[4] File and line of the "old" catalog
[5] File and line of the "new" catalog

This object seeks to preserve this traditional structure, while providing methods to make it easier to deal with. We recommend using the named options, rather than #raw or the indexed array, as the raw object and indexed array are not guaranteed to be stable.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Diff

Constructor: Accepts a diff in the traditional array format and stores it.

Parameters:

  • raw (Array)

    Diff in the traditional format


37
38
39
40
41
42
43
44
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 37

def initialize(raw)
  unless raw.is_a?(Array)
    raise ArgumentError, "OctocatalogDiff::API::V1::Diff#initialize expects Array argument (got #{raw.class})"
  end

  @raw = raw
  initialize_helper
end

Instance Attribute Details

#diff_typeObject (readonly)

Returns the value of attribute diff_type


20
21
22
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 20

def diff_type
  @diff_type
end

#rawObject (readonly)

Returns the value of attribute raw


20
21
22
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 20

def raw
  @raw
end

#structureObject (readonly)

Returns the value of attribute structure


20
21
22
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 20

def structure
  @structure
end

#titleObject (readonly)

Returns the value of attribute title


20
21
22
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 20

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type


20
21
22
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 20

def type
  @type
end

Class Method Details

.factory(object_in) ⇒ OctocatalogDiff::API::V1::Diff

Public: Construct a OctocatalogDiff::API::V1::Diff object from many different types of input. This includes passing a OctocatalogDiff::API::V1::Diff object and getting that identical object back.

Parameters:

  • object_in (?)

    Object in

Returns:

Raises:

  • (ArgumentError)

27
28
29
30
31
32
33
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 27

def self.factory(object_in)
  return object_in if object_in.is_a?(OctocatalogDiff::API::V1::Diff)

  return new(object_in) if object_in.is_a?(Array)

  raise ArgumentError, "Cannot construct OctocatalogDiff::API::V1::Diff from #{object_in.class}"
end

Instance Method Details

#[](i) ⇒ ?

Public: Retrieve an indexed value from the array

Returns:

  • (?)

    Indexed value


48
49
50
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 48

def [](i)
  @raw[i]
end

#[]=(i, new_value) ⇒ Object

Public: Set an element of the array

Parameters:


54
55
56
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 54

def []=(i, new_value)
  @raw[i] = new_value
end

#addition?Boolean

Public: Is this an addition?

Returns:

  • (Boolean)

    True if this is an addition


60
61
62
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 60

def addition?
  diff_type == '+'
end

#change?Boolean

Public: Is this a change?

Returns:

  • (Boolean)

    True if this is an change


72
73
74
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 72

def change?
  diff_type == '~' || diff_type == '!'
end

#inspectString

Public: String inspection

Returns:

  • (String)

    String for inspection


164
165
166
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 164

def inspect
  to_h.inspect
end

#new_fileString

Public: Get the filename from the “new” location

Returns:


107
108
109
110
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 107

def new_file
  x = new_location
  x.nil? ? nil : x['file']
end

#new_lineString

Public: Get the line number from the “new” location

Returns:


114
115
116
117
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 114

def new_line
  x = new_location
  x.nil? ? nil : x['line']
end

#new_locationHash

Public: Get the “new” location, i.e. location in the “to” catalog

Returns:

  • (Hash)

    <file:, line:> of resource


129
130
131
132
133
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 129

def new_location
  return @raw[3] if addition?
  return if removal?
  @raw[5]
end

#new_value?

Public: Get the “new” value, i.e. “to” catalog

Returns:

  • (?)

    “new” value


85
86
87
88
89
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 85

def new_value
  return if removal?
  return @raw[2] if addition?
  @raw[3]
end

#old_fileString

Public: Get the filename from the “old” location

Returns:


93
94
95
96
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 93

def old_file
  x = old_location
  x.nil? ? nil : x['file']
end

#old_lineString

Public: Get the line number from the “old” location

Returns:


100
101
102
103
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 100

def old_line
  x = old_location
  x.nil? ? nil : x['line']
end

#old_locationHash

Public: Get the “old” location, i.e. location in the “from” catalog

Returns:

  • (Hash)

    <file:, line:> of resource


121
122
123
124
125
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 121

def old_location
  return if addition?
  return @raw[3] if removal?
  @raw[4]
end

#old_value?

Public: Get the “old” value, i.e. “from” catalog

Returns:

  • (?)

    “old” value


78
79
80
81
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 78

def old_value
  return if addition?
  @raw[2]
end

#removal?Boolean

Public: Is this a removal?

Returns:

  • (Boolean)

    True if this is an addition


66
67
68
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 66

def removal?
  diff_type == '-'
end

#to_hHash

Public: Convert this object to a hash

Returns:

  • (Hash)

    Hash with keys set by these methods


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 137

def to_h
  {
    diff_type: diff_type,
    type: type,
    title: title,
    structure: structure,
    old_value: old_value,
    new_value: new_value,
    old_file: old_file,
    old_line: old_line,
    new_file: new_file,
    new_line: new_line,
    old_location: old_location,
    new_location: new_location
  }
end

#to_h_with_string_keysHash

Public: Convert this object to a hash with string keys

Returns:

  • (Hash)

    Hash with keys set by these methods, with string keys


156
157
158
159
160
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 156

def to_h_with_string_keys
  result = {}
  to_h.each { |key, val| result[key.to_s] = val }
  result
end

#to_sString

Public: To string

Returns:

  • (String)

    Compact string representation


170
171
172
# File 'lib/octocatalog-diff/api/v1/diff.rb', line 170

def to_s
  raw.inspect
end