Class: StateMachina::StatesCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/state_machina/states_collection.rb

Instance Method Summary collapse

Constructor Details

#initialize(states) ⇒ StatesCollection

Returns a new instance of StatesCollection.



5
6
7
# File 'lib/state_machina/states_collection.rb', line 5

def initialize(states)
  @states = states
end

Instance Method Details

#before(state_name) ⇒ Object



23
24
25
26
27
28
# File 'lib/state_machina/states_collection.rb', line 23

def before(state_name)
  index = index_by_name(state_name) - 1
  return [] if index.negative?

  to_a[..index]
end

#before_inclusive(state_name) ⇒ Object



30
31
32
# File 'lib/state_machina/states_collection.rb', line 30

def before_inclusive(state_name)
  to_a[..index_by_name(state_name)]
end

#between(from_state_name, to_state_name) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
# File 'lib/state_machina/states_collection.rb', line 42

def between(from_state_name, to_state_name)
  indexes = [index_by_name(from_state_name), index_by_name(to_state_name)]
  raise(ArgumentError, "to_state should be higher than from_state") if indexes != indexes.sort

  to_a[indexes[0]...indexes[1]]
end

#between_inclusive(from_state_name, to_state_name) ⇒ Object

Raises:

  • (ArgumentError)


49
50
51
52
53
54
# File 'lib/state_machina/states_collection.rb', line 49

def between_inclusive(from_state_name, to_state_name)
  indexes = [index_by_name(from_state_name), index_by_name(to_state_name)]
  raise(ArgumentError, "to_state should be higher than from_state") if indexes != indexes.sort

  to_a[indexes[0]..indexes[1]]
end

#each(&block) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/state_machina/states_collection.rb', line 9

def each(&block)
  if block_given?
    @states.each(&block)
  else
    to_enum(:each)
  end
end

#find_by_name(state_name) ⇒ Object



17
18
19
20
21
# File 'lib/state_machina/states_collection.rb', line 17

def find_by_name(state_name)
  find do |state|
    state.name == state_name.to_s
  end
end

#past(state_name) ⇒ Object



34
35
36
# File 'lib/state_machina/states_collection.rb', line 34

def past(state_name)
  to_a[(index_by_name(state_name) + 1)..]
end

#past_inclusive(state_name) ⇒ Object



38
39
40
# File 'lib/state_machina/states_collection.rb', line 38

def past_inclusive(state_name)
  to_a[index_by_name(state_name)..]
end