class Sirens::ListModel

Public Class Methods

new(list = []) click to toggle source

Initializing

Calls superclass method
# File lib/models/list_model.rb, line 23
def initialize(list = [])
    super()

    @list = list
end
on(list) click to toggle source
# File lib/models/list_model.rb, line 16
def on(list)
    self.new(list)
end
with(item) click to toggle source
# File lib/models/list_model.rb, line 8
def with(item)
    self.on([items])
end
with_all(items) click to toggle source
# File lib/models/list_model.rb, line 12
def with_all(items)
    self.on(items.clone)
end

Public Instance Methods

<<(item) click to toggle source

Adds the given item at the end of the list.

# File lib/models/list_model.rb, line 62
def <<(item)
    add_at(items: [item], index: -1)
end
[](index) click to toggle source
# File lib/models/list_model.rb, line 124
def [](index)
    @list[index]
end
[]=(index, value) click to toggle source
# File lib/models/list_model.rb, line 128
def []=(index, value)
    @list[index] = value
end
add_at(index:, items:) click to toggle source

Adds the given items at the end of the list.

# File lib/models/list_model.rb, line 69
def add_at(index:, items:)
    list.insert(index, *items)

    changed

    notify_observers(
        ItemsAdded.new(list: list, index: index, items: items)
    )
end
each(&block) click to toggle source
# File lib/models/list_model.rb, line 120
def each(&block)
    @list.each(&block)
end
list() click to toggle source

Accessing

# File lib/models/list_model.rb, line 31
def list()
    @list
end
remove_at(indices:) click to toggle source

Removes the items at the given indices in the list.

# File lib/models/list_model.rb, line 101
def remove_at(indices:)
    indices = indices.sort.reverse
    items = []

    indices.sort.reverse.each do |i|
        items << list.delete_at(i)
    end

    changed

    notify_observers(
        ItemsRemoved.new(list: list, indices: indices, items: items)
    )
end
set_list(new_list) click to toggle source
# File lib/models/list_model.rb, line 35
def set_list(new_list)
    return if @list == new_list

    old_list = @list

    @list = new_list

    changed

    notify_observers(
        ListChanged.new(new_list: new_list, old_list: old_list)
    )
end
set_value(new_list) click to toggle source
# File lib/models/list_model.rb, line 53
def set_value(new_list)
    set_list(new_list)
end
update_at(indices:, items:) click to toggle source

Updates the items at the given indices in the list.

# File lib/models/list_model.rb, line 84
def update_at(indices:, items:)
    (0 ... items.size).each do |i|
        list[ indices[i] ] = items[i]
    end

    changed

    notify_observers(
        ItemsUpdated.new(list: list, indices: indices, items: items)
    )
end
value() click to toggle source
# File lib/models/list_model.rb, line 49
def value()
    list
end