Class: Workarea::SwappableList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/workarea/swappable_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(source = []) ⇒ SwappableList

Returns a new instance of SwappableList.



7
8
9
# File 'lib/workarea/swappable_list.rb', line 7

def initialize(source = [])
  @source = Array(source)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/workarea/swappable_list.rb', line 44

def method_missing(method, *args, &block)
  if @source.respond_to?(method)
    @source.send(method, *args, &block)
  else
    super
  end
end

Instance Method Details

#+(other) ⇒ Object



32
33
34
35
36
# File 'lib/workarea/swappable_list.rb', line 32

def +(other)
  self.class.new(
    @source + Array(other)
  )
end

#-(other) ⇒ Object



38
39
40
41
42
# File 'lib/workarea/swappable_list.rb', line 38

def -(other)
  self.class.new(
    @source - Array(other)
  )
end

#deep_dupObject



56
57
58
# File 'lib/workarea/swappable_list.rb', line 56

def deep_dup
  self.class.new(@source.deep_dup)
end

#delete(target) ⇒ Object



28
29
30
# File 'lib/workarea/swappable_list.rb', line 28

def delete(target)
  @source.delete(target)
end

#insert(index, new_val) ⇒ Object Also known as: insert_before



11
12
13
14
# File 'lib/workarea/swappable_list.rb', line 11

def insert(index, new_val)
  index = assert_index(index, :before)
  @source.insert(index, new_val)
end

#insert_after(index, new_val) ⇒ Object



17
18
19
20
# File 'lib/workarea/swappable_list.rb', line 17

def insert_after(index, new_val)
  index = assert_index(index, :after)
  insert(index + 1, new_val)
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/workarea/swappable_list.rb', line 52

def respond_to_missing?(method_name, include_private = false)
  super || @source.respond_to?(method_name)
end

#swap(target, new_val) ⇒ Object



22
23
24
25
26
# File 'lib/workarea/swappable_list.rb', line 22

def swap(target, new_val)
  index = assert_index(target, :before)
  insert(index, new_val)
  @source.delete_at(index + 1)
end