Class: Rypper::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/rypper/counter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(match, lower, upper, digits = 1, state = nil) ⇒ Counter

Returns a new instance of Counter.



11
12
13
14
15
16
17
# File 'lib/rypper/counter.rb', line 11

def initialize(match, lower, upper, digits=1, state=nil)
  @match = match
  @lower = lower
  @upper = upper
  @digits = digits
  @state = state || @lower
end

Instance Attribute Details

#digitsObject (readonly)

Returns the value of attribute digits.



8
9
10
# File 'lib/rypper/counter.rb', line 8

def digits
  @digits
end

#lowerObject (readonly)

Returns the value of attribute lower.



6
7
8
# File 'lib/rypper/counter.rb', line 6

def lower
  @lower
end

#matchObject (readonly)

Returns the value of attribute match.



5
6
7
# File 'lib/rypper/counter.rb', line 5

def match
  @match
end

#stateObject (readonly)

Returns the value of attribute state.



9
10
11
# File 'lib/rypper/counter.rb', line 9

def state
  @state
end

#upperObject (readonly)

Returns the value of attribute upper.



7
8
9
# File 'lib/rypper/counter.rb', line 7

def upper
  @upper
end

Instance Method Details

#first!Object



19
20
21
22
# File 'lib/rypper/counter.rb', line 19

def first!
  @state = @lower
  self
end

#first?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/rypper/counter.rb', line 24

def first?
  @state == @lower
end

#last!Object



46
47
48
49
# File 'lib/rypper/counter.rb', line 46

def last!
  @state = @upper
  self
end

#last?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/rypper/counter.rb', line 51

def last?
  @state == @upper
end

#next!Object



37
38
39
40
41
42
43
44
# File 'lib/rypper/counter.rb', line 37

def next!
  if @state < @upper
    @state += 1
  else
    @state = @lower
  end
  self
end

#prev!Object



28
29
30
31
32
33
34
35
# File 'lib/rypper/counter.rb', line 28

def prev!
  if @state > @lower
    @state -= 1
  else
    @state = @upper
  end
  self
end

#to_s(digits = nil) ⇒ Object



55
56
57
58
# File 'lib/rypper/counter.rb', line 55

def to_s(digits=nil)
  digits ||= self.digits
  @state.to_s.rjust(digits, '0')
end