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
24
# File 'lib/uh/layout/container.rb', line 21

def current= entry
  fail ArgumentError, 'unknown entry' unless include? entry
  @current_index = @entries.index entry
end

#get(direction, cycle: false) ⇒ Object



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

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

#insert_after_current(entry) ⇒ Object



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

def insert_after_current entry
  fail RuntimeError, 'no current entry' unless current
  @entries.insert @current_index + 1, entry
end

#remove(*entries) ⇒ Object



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

def remove *entries
  entries.each { |e| remove_entry e }
  @entries.each { |e| remove_entry e if yield e } if block_given?
  self
end

#sel(direction) ⇒ Object



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

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

#set(direction) ⇒ Object



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

def set direction
  fail RuntimeError unless @entries.size >= 2
  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



62
63
64
# File 'lib/uh/layout/container.rb', line 62

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