Class: CleverSequence

Inherits:
Object
  • Object
show all
Defined in:
lib/demo_mode/clever_sequence.rb

Defined Under Namespace

Classes: LowerBoundFinder

Constant Summary collapse

DEFAULT_BLOCK =
->(i) { i }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, &block) ⇒ CleverSequence

Returns a new instance of CleverSequence.



29
30
31
32
# File 'lib/demo_mode/clever_sequence.rb', line 29

def initialize(attribute, &block)
  @attribute = attribute.to_s
  @block = block || DEFAULT_BLOCK
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



27
28
29
# File 'lib/demo_mode/clever_sequence.rb', line 27

def attribute
  @attribute
end

#blockObject (readonly)

Returns the value of attribute block.



27
28
29
# File 'lib/demo_mode/clever_sequence.rb', line 27

def block
  @block
end

#klassObject (readonly)

Returns the value of attribute klass.



27
28
29
# File 'lib/demo_mode/clever_sequence.rb', line 27

def klass
  @klass
end

Class Method Details

.last(klass, name) ⇒ Object



15
16
17
# File 'lib/demo_mode/clever_sequence.rb', line 15

def last(klass, name)
  lookup(klass, name)&.last
end

.next(klass, name) ⇒ Object



11
12
13
# File 'lib/demo_mode/clever_sequence.rb', line 11

def next(klass, name)
  lookup(klass, name)&.next
end

.reset!Object



7
8
9
# File 'lib/demo_mode/clever_sequence.rb', line 7

def reset!
  sequences.each_value(&:reset!)
end

Instance Method Details

#lastObject



45
46
47
# File 'lib/demo_mode/clever_sequence.rb', line 45

def last
  block.call(last_value)
end

#nextObject



40
41
42
43
# File 'lib/demo_mode/clever_sequence.rb', line 40

def next
  @last_value = last_value + 1
  last
end

#reset!Object



49
50
51
# File 'lib/demo_mode/clever_sequence.rb', line 49

def reset!
  remove_instance_variable(:@last_value) if instance_variable_defined?(:@last_value)
end

#with_class(klass) ⇒ Object



34
35
36
37
38
# File 'lib/demo_mode/clever_sequence.rb', line 34

def with_class(klass)
  sequences[[klass.name, attribute.to_s]] = self if klass && !@klass
  @klass ||= klass
  self
end