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



42
43
44
45
46
47
48
49
# File 'lib/uh/layout/container.rb', line 42

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(entry) ⇒ Object



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

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



38
39
40
# File 'lib/uh/layout/container.rb', line 38

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

#sel(direction) ⇒ Object



51
52
53
# File 'lib/uh/layout/container.rb', line 51

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

#set(direction) ⇒ Object

Raises:



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/uh/layout/container.rb', line 55

def set(direction)
  raise 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



67
68
69
# File 'lib/uh/layout/container.rb', line 67

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