Class: I18n::Keys::Occurence

Inherits:
Object
  • Object
show all
Includes:
Ansi
Defined in:
lib/i18n/keys/occurence.rb

Constant Summary collapse

@@context_lines =
2

Constants included from Ansi

Ansi::COLORS, Ansi::STYLES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Ansi

#ansi_format

Constructor Details

#initialize(key, file, start_pos, end_pos) ⇒ Occurence

Returns a new instance of Occurence.



26
27
28
29
30
31
# File 'lib/i18n/keys/occurence.rb', line 26

def initialize(key, file, start_pos, end_pos)
  @key = key.to_sym
  @file = file
  @start_pos = start_pos
  @end_pos = end_pos
end

Instance Attribute Details

#end_posObject (readonly)

Returns the value of attribute end_pos.



24
25
26
# File 'lib/i18n/keys/occurence.rb', line 24

def end_pos
  @end_pos
end

#fileObject (readonly)

Returns the value of attribute file.



24
25
26
# File 'lib/i18n/keys/occurence.rb', line 24

def file
  @file
end

#keyObject (readonly)

Returns the value of attribute key.



24
25
26
# File 'lib/i18n/keys/occurence.rb', line 24

def key
  @key
end

#start_posObject (readonly)

Returns the value of attribute start_pos.



24
25
26
# File 'lib/i18n/keys/occurence.rb', line 24

def start_pos
  @start_pos
end

Class Method Details

.context_linesObject



11
12
13
# File 'lib/i18n/keys/occurence.rb', line 11

def context_lines
  @@context_lines
end

.context_lines=(num) ⇒ Object



15
16
17
# File 'lib/i18n/keys/occurence.rb', line 15

def context_lines=(num)
  @@context_lines = num
end

.from_sexp(sexp, file = nil) ⇒ Object



19
20
21
# File 'lib/i18n/keys/occurence.rb', line 19

def from_sexp(sexp, file = nil)
  new(sexp[1], file || sexp.file, sexp.source_start_pos, sexp.source_end_pos)
end

Instance Method Details

#==(other) ⇒ Object



102
103
104
# File 'lib/i18n/keys/occurence.rb', line 102

def ==(other)
  key == other.key and start_pos == other.start_pos and end_pos == other.end_pos
end

#columnObject



59
60
61
# File 'lib/i18n/keys/occurence.rb', line 59

def column
  position[1]
end

#contentObject



42
43
44
# File 'lib/i18n/keys/occurence.rb', line 42

def content
  lines.join
end

#content_headObject



76
77
78
# File 'lib/i18n/keys/occurence.rb', line 76

def content_head
  content[0..(start_pos - 1)].to_s
end

#content_tailObject



80
81
82
# File 'lib/i18n/keys/occurence.rb', line 80

def content_tail
  content[(end_pos + 1)..-1].to_s
end

#contextObject



92
93
94
95
96
# File 'lib/i18n/keys/occurence.rb', line 92

def context
  @context ||=
    lines[line_num - self.class.context_lines - 1, self.class.context_lines].join +
    line(true) + lines[line_num, self.class.context_lines].join
end

#lengthObject



63
64
65
# File 'lib/i18n/keys/occurence.rb', line 63

def length
  end_pos - start_pos + 1
end

#length=(length) ⇒ Object



67
68
69
70
# File 'lib/i18n/keys/occurence.rb', line 67

def length=(length)
  @length = length
  @end_pos = start_pos + length - 1
end

#line(highlight = false) ⇒ Object



50
51
52
53
# File 'lib/i18n/keys/occurence.rb', line 50

def line(highlight = false)
  line = lines[line_num - 1].dup
  highlight ? line_head + ansi_format(original_key, [:red, :bold]) + line_tail : line
end

#line_headObject



84
85
86
# File 'lib/i18n/keys/occurence.rb', line 84

def line_head
  line[0..(column - 2)].to_s
end

#line_numObject



55
56
57
# File 'lib/i18n/keys/occurence.rb', line 55

def line_num
  position[0]
end

#line_tailObject



88
89
90
# File 'lib/i18n/keys/occurence.rb', line 88

def line_tail
  line[(column + length - 1)..-1].to_s
end

#linesObject



46
47
48
# File 'lib/i18n/keys/occurence.rb', line 46

def lines
  @lines ||= File.open(file, 'r') { |f| f.readlines }
end

#original_keyObject



72
73
74
# File 'lib/i18n/keys/occurence.rb', line 72

def original_key
  line[column - 1, length]
end

#positionObject



106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/i18n/keys/occurence.rb', line 106

def position
  @position ||= begin
    line_num, col = 1, 1
    lines.inject(0) do |sum, line|
      break if sum + line.length > start_pos
      line_num += 1
      col = start_pos - sum - line.length + 1
      sum += line.length
    end
    [line_num, col]
  end
end

#replace!(replacement) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/i18n/keys/occurence.rb', line 33

def replace!(replacement)
  replacement = replacement.to_sym.inspect
  content = content_head + replacement + content_tail
  @key = replacement.to_sym
  self.length = replacement.to_s.length
  File.open(file, 'w+') { |f| f.write(content) }
  @position, @lines, @context = nil, nil, nil
end

#to_sObject



98
99
100
# File 'lib/i18n/keys/occurence.rb', line 98

def to_s
  "#{key}: #{file} [#{line_num}/#{column}]"
end