Class: VER::Text::Index

Inherits:
Struct
  • Object
show all
Includes:
Comparable
Defined in:
lib/ver/text/index.rb,
lib/ver/text/index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(widget, idx) ⇒ Index

Returns a new instance of Index.



7
8
9
# File 'lib/ver/text/index.rb', line 7

def initialize(widget, idx)
  self.widget, self.idx = widget, idx.to_str
end

Instance Attribute Details

#idxObject

Returns the value of attribute idx

Returns:

  • (Object)

    the current value of idx



3
4
5
# File 'lib/ver/text/index.rb', line 3

def idx
  @idx
end

#widgetObject

Returns the value of attribute widget

Returns:

  • (Object)

    the current value of widget



3
4
5
# File 'lib/ver/text/index.rb', line 3

def widget
  @widget
end

#xObject

Returns the value of attribute x

Returns:

  • (Object)

    the current value of x



3
4
5
# File 'lib/ver/text/index.rb', line 3

def x
  @x
end

#yObject

Returns the value of attribute y

Returns:

  • (Object)

    the current value of y



3
4
5
# File 'lib/ver/text/index.rb', line 3

def y
  @y
end

Instance Method Details

#<=>(other) ⇒ Object



15
16
17
18
19
20
# File 'lib/ver/text/index.rb', line 15

def <=>(other)
  self_idx, other_idx = idx, other.to_index.idx
  return  1 if widget.compare(self_idx, '>',  other_idx)
  return -1 if widget.compare(self_idx, '<',  other_idx)
  return  0 if widget.compare(self_idx, '==', other_idx)
end

#delta(other) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/ver/text/index.rb', line 22

def delta(other)
  y_diff = other.y - y

  if y_diff == 0
    (other.x - x).abs
  else
    y_diff.abs
  end
end

#inspectObject



68
69
70
# File 'lib/ver/text/index.rb', line 68

def inspect
  "#<Text::Index #{self}>"
end

#is_a?(obj) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/ver/text/index.rb', line 98

def is_a?(obj)
  obj == String || super
end

#lineendObject



44
45
46
# File 'lib/ver/text/index.rb', line 44

def lineend
  "#{idx} lineend"
end

#linestartObject



40
41
42
# File 'lib/ver/text/index.rb', line 40

def linestart
  "#{idx} linestart"
end

#nextObject



56
57
58
# File 'lib/ver/text/index.rb', line 56

def next
  self.class.new(widget, "#{y + 1}.#{x}")
end

#prevObject



60
61
62
# File 'lib/ver/text/index.rb', line 60

def prev
  self.class.new(widget, "#{y - 1}.#{x}")
end

#splitObject



64
65
66
# File 'lib/ver/text/index.rb', line 64

def split
  return y, x
end

#to_iObject



80
81
82
# File 'lib/ver/text/index.rb', line 80

def to_i
  y
end

#to_indexObject



11
12
13
# File 'lib/ver/text/index.rb', line 11

def to_index
  self
end

#to_sObject



76
77
78
# File 'lib/ver/text/index.rb', line 76

def to_s
  "#{y}.#{x}"
end

#to_strObject



72
73
74
# File 'lib/ver/text/index.rb', line 72

def to_str
  idx
end

#to_tclObject



84
85
86
# File 'lib/ver/text/index.rb', line 84

def to_tcl
  Tk::TclString.new(to_s)
end

#upto(other) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/ver/text/index.rb', line 88

def upto(other)
  if block_given?
    y.upto(other.y) do |line|
      yield self.class.new(widget, "#{line}.0")
    end
  else
    Enumerator.new(self, :upto, other)
  end
end

#wordendObject



52
53
54
# File 'lib/ver/text/index.rb', line 52

def wordend
  "#{idx} wordend"
end

#wordstartObject



48
49
50
# File 'lib/ver/text/index.rb', line 48

def wordstart
  "#{idx} wordstart"
end