Class: Sirens::ListModel

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Observable
Defined in:
lib/models/list_model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list = []) ⇒ ListModel

Initializing



23
24
25
26
27
# File 'lib/models/list_model.rb', line 23

def initialize(list = [])
    super()

    @list = list
end

Class Method Details

.on(list) ⇒ Object



16
17
18
# File 'lib/models/list_model.rb', line 16

def on(list)
    self.new(list)
end

.with(item) ⇒ Object



8
9
10
# File 'lib/models/list_model.rb', line 8

def with(item)
    self.on([items])
end

.with_all(items) ⇒ Object



12
13
14
# File 'lib/models/list_model.rb', line 12

def with_all(items)
    self.on(items.clone)
end

Instance Method Details

#<<(item) ⇒ Object

Adds the given item at the end of the list.



62
63
64
# File 'lib/models/list_model.rb', line 62

def <<(item)
    add_at(items: [item], index: -1)
end

#[](index) ⇒ Object



124
125
126
# File 'lib/models/list_model.rb', line 124

def [](index)
    @list[index]
end

#[]=(index, value) ⇒ Object



128
129
130
# File 'lib/models/list_model.rb', line 128

def []=(index, value)
    @list[index] = value
end

#add_at(index:, items:) ⇒ Object

Adds the given items at the end of the list.



69
70
71
72
73
74
75
76
77
# 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) ⇒ Object



120
121
122
# File 'lib/models/list_model.rb', line 120

def each(&block)
    @list.each(&block)
end

#listObject

Accessing



31
32
33
# File 'lib/models/list_model.rb', line 31

def list()
    @list
end

#remove_at(indices:) ⇒ Object

Removes the items at the given indices in the list.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object



53
54
55
# File 'lib/models/list_model.rb', line 53

def set_value(new_list)
    set_list(new_list)
end

#update_at(indices:, items:) ⇒ Object

Updates the items at the given indices in the list.



84
85
86
87
88
89
90
91
92
93
94
# 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

#valueObject



49
50
51
# File 'lib/models/list_model.rb', line 49

def value()
    list
end