class Uh::Layout::Container
Public Class Methods
new(entries = [])
click to toggle source
# File lib/uh/layout/container.rb, line 10 def initialize entries = [] @entries = entries @current_index = 0 end
Public Instance Methods
current()
click to toggle source
# File lib/uh/layout/container.rb, line 17 def current @entries[@current_index] end
current=(entry)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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) { |e| ... }
click to toggle source
# 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)
click to toggle source
# File lib/uh/layout/container.rb, line 46 def sel direction @current_index = @current_index.send(direction) % @entries.size end
set(direction)
click to toggle source
# 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)
click to toggle source
# File lib/uh/layout/container.rb, line 62 def swap a, b @entries[a], @entries[b] = @entries[b], @entries[a] end
Private Instance Methods
remove_entry(entry)
click to toggle source
# File lib/uh/layout/container.rb, line 68 def remove_entry entry fail ArgumentError, 'unknown entry' unless include? entry @entries.delete_at (index = @entries.index(entry)) if @current_index != 0 && @current_index >= index @current_index -= 1 end end
rotate(direction)
click to toggle source
# File lib/uh/layout/container.rb, line 76 def rotate direction @entries = @entries.rotate case direction when :pred then 1 when :succ then -1 end end