Class: I18nContextGenerator::ChangedLocation

Inherits:
Data
  • Object
show all
Defined in:
lib/i18n_context_generator/changed_location.rb

Overview

A line in a Git diff, including the revision side needed by review clients.

fallback_line identifies a corresponding line in the head revision. It is useful when a removed translator comment needs a right-side suggestion anchor.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file:, line:, side: :right, fallback_line: nil) ⇒ ChangedLocation

Returns a new instance of ChangedLocation.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/i18n_context_generator/changed_location.rb', line 9

def initialize(file:, line:, side: :right, fallback_line: nil)
  normalized_side = side.to_s.downcase.to_sym
  raise ArgumentError, "Unsupported diff side: #{side}" unless %i[left right].include?(normalized_side)

  super(
    file: file.to_s,
    line: Integer(line),
    side: normalized_side,
    fallback_line: fallback_line && Integer(fallback_line)
  )
end

Instance Attribute Details

#fallback_lineObject (readonly)

Returns the value of attribute fallback_line

Returns:

  • (Object)

    the current value of fallback_line



8
9
10
# File 'lib/i18n_context_generator/changed_location.rb', line 8

def fallback_line
  @fallback_line
end

#fileObject (readonly)

Returns the value of attribute file

Returns:

  • (Object)

    the current value of file



8
9
10
# File 'lib/i18n_context_generator/changed_location.rb', line 8

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line

Returns:

  • (Object)

    the current value of line



8
9
10
# File 'lib/i18n_context_generator/changed_location.rb', line 8

def line
  @line
end

#sideObject (readonly)

Returns the value of attribute side

Returns:

  • (Object)

    the current value of side



8
9
10
# File 'lib/i18n_context_generator/changed_location.rb', line 8

def side
  @side
end

Class Method Details

.parse(value) ⇒ Object

Raises:

  • (ArgumentError)


21
22
23
24
25
26
27
28
# File 'lib/i18n_context_generator/changed_location.rb', line 21

def self.parse(value)
  return value if value.is_a?(self)

  match = /\A(.+):(\d+)\z/.match(value.to_s)
  raise ArgumentError, "Invalid changed location: #{value.inspect}" unless match

  new(file: match[1], line: match[2], side: :right)
end

Instance Method Details

#left?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/i18n_context_generator/changed_location.rb', line 30

def left?
  side == :left
end

#review_sideObject



38
39
40
# File 'lib/i18n_context_generator/changed_location.rb', line 38

def review_side
  side.to_s.upcase
end

#right?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/i18n_context_generator/changed_location.rb', line 34

def right?
  side == :right
end

#to_hObject



46
47
48
49
50
51
52
53
# File 'lib/i18n_context_generator/changed_location.rb', line 46

def to_h
  {
    file: file,
    line: line,
    side: side,
    fallback_line: fallback_line
  }
end

#to_sObject



42
43
44
# File 'lib/i18n_context_generator/changed_location.rb', line 42

def to_s
  "#{file}:#{line}"
end