Class: Uh::Layout::Container

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/uh/layout/container.rb

Instance Method Summary collapse

Constructor Details

#initialize(entries = []) ⇒ Container

Returns a new instance of Container.



10
11
12
13
# File 'lib/uh/layout/container.rb', line 10

def initialize(entries = [])
  @entries        = entries
  @current_index  = 0
end

Instance Method Details

#currentObject



17
18
19
# File 'lib/uh/layout/container.rb', line 17

def current
  @entries[@current_index]
end

#current=(entry) ⇒ Object



21
22
23
# File 'lib/uh/layout/container.rb', line 21

def current=(entry)
  @current_index = @entries.index entry if include? entry
end

#get(direction, cycle: false) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/uh/layout/container.rb', line 36

def get(direction, cycle: false)
  index = @current_index.send direction
  if cycle
    @entries[index % @entries.size]
  else
    index >= 0 ? self[index] : nil
  end
end

#remove(entry) ⇒ Object



25
26
27
28
29
30
# File 'lib/uh/layout/container.rb', line 25

def remove(entry)
  fail ArgumentError, 'unknown entry' unless include? entry
  @entries.delete_at @entries.index entry
  @current_index -= 1 unless @current_index == 0
  self
end

#remove_ifObject



32
33
34
# File 'lib/uh/layout/container.rb', line 32

def remove_if
  @entries.each { |e| remove e if yield e }
end

#sel(direction) ⇒ Object



45
46
47
# File 'lib/uh/layout/container.rb', line 45

def sel(direction)
  @current_index = @current_index.send(direction) % @entries.size
end

#set(direction) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/uh/layout/container.rb', line 49

def set(direction)
  new_index = @current_index.send direction
  if new_index.between? 0, @entries.size - 1
    swap @current_index, new_index
    @current_index = new_index
  else
    rotate direction
    @current_index = new_index % @entries.size
  end
end

#swap(a, b) ⇒ Object



60
61
62
# File 'lib/uh/layout/container.rb', line 60

def swap(a, b)
  @entries[a], @entries[b] = @entries[b], @entries[a]
end