Class: Usine::Sequence

Inherits:
Object
  • Object
show all
Defined in:
lib/usine/sequence.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, *args, &block) ⇒ Sequence

Returns a new instance of Sequence.



5
6
7
8
9
10
11
12
13
# File 'lib/usine/sequence.rb', line 5

def initialize(attribute, *args, &block)
  @attribute = attribute
  @block = block
  @value = args.first || 1

  unless @value.respond_to?(:next)
    raise(UsineError::SequenceInvalidInitialValue, "Invalid initial value, it must respond to #next")
  end
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



3
4
5
# File 'lib/usine/sequence.rb', line 3

def attribute
  @attribute
end

Instance Method Details

#nextObject



15
16
17
18
19
# File 'lib/usine/sequence.rb', line 15

def next
  @block.call(@value)
ensure
  @value = @value.next
end