Class: JLDrill::Bin

Inherits:
Object
  • Object
show all
Defined in:
lib/jldrill/model/Bin.rb

Overview

Holds a group of items that are at the same level.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, number) ⇒ Bin

Create a new bin and call it name



8
9
10
11
12
# File 'lib/jldrill/model/Bin.rb', line 8

def initialize(name, number)
    @name = name
    @number = number
    @contents = []
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



5
6
7
# File 'lib/jldrill/model/Bin.rb', line 5

def contents
  @contents
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/jldrill/model/Bin.rb', line 5

def name
  @name
end

#numberObject (readonly)

Returns the value of attribute number.



5
6
7
# File 'lib/jldrill/model/Bin.rb', line 5

def number
  @number
end

Instance Method Details

#[](index) ⇒ Object

Returns the item at the index specified



20
21
22
23
# File 'lib/jldrill/model/Bin.rb', line 20

def [](index)
    item = @contents[index]
    return item
end

#all?(&block) ⇒ Boolean

Calls a block for each item in the bin, stopping if the block returns false. Returns true if all iterations return true.

Returns:

  • (Boolean)


91
92
93
94
95
# File 'lib/jldrill/model/Bin.rb', line 91

def all?(&block)
    @contents.all? do |item|
        block.call(item)
    end
end

#allSeen?Boolean

Returns true if all the items in the bin have been seen

Returns:

  • (Boolean)


140
141
142
143
144
# File 'lib/jldrill/model/Bin.rb', line 140

def allSeen?
    @contents.all? do |item|
        item.schedule.seen?
    end
end

#contain?(object) ⇒ Boolean

Returns true if there is an Item in the bin that contains the object

Returns:

  • (Boolean)


194
195
196
197
198
# File 'lib/jldrill/model/Bin.rb', line 194

def contain?(object)
    !@contents.find do |x|
        x.contain?(object)
    end.nil?
end

#delete(item) ⇒ Object



71
72
73
# File 'lib/jldrill/model/Bin.rb', line 71

def delete(item)
    @contents.delete(item)
end

#each(&block) ⇒ Object

Calls a block for each item in the bin



76
77
78
79
80
# File 'lib/jldrill/model/Bin.rb', line 76

def each(&block)
    @contents.each do |item|
        block.call(item)
    end
end

#empty?Boolean

Returns true if the bin is empty

Returns:

  • (Boolean)


126
127
128
# File 'lib/jldrill/model/Bin.rb', line 126

def empty?
    @contents.empty?
end

#exists?(item) ⇒ Boolean

Returns true if the Item exists in the bin

Returns:

  • (Boolean)


187
188
189
190
191
# File 'lib/jldrill/model/Bin.rb', line 187

def exists?(item)
    !@contents.find do |x|
        item.eql?(x)
    end.nil?
end

#findAll(&block) ⇒ Object

Returns an array of items for which block returns true



106
107
108
109
110
111
112
113
114
# File 'lib/jldrill/model/Bin.rb', line 106

def findAll(&block)
    retVal = []
    @contents.each do |item|
        if block.call(item)
            retVal.push(item)
        end
    end
    return retVal
end

#findUnseen(n) ⇒ Object

Return the nth unseen item in the bin



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/jldrill/model/Bin.rb', line 162

def findUnseen(n)
    retVal = nil
    if n < numUnseen
        i = 0
        0.upto(n) do |m|
            while @contents[i].schedule.seen
                i += 1
            end
            if m != n
                i += 1
            end
        end
        retVal = @contents[i]
    end
    retVal
end

#firstUnseenObject

Return the index of the first item in the bin that hasn’t been seen yet. Returns -1 if there are no unseen items



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/jldrill/model/Bin.rb', line 148

def firstUnseen
    index = 0
    # find the first one that hasn't been seen yet
    while (index < length) && @contents[index].schedule.seen?
        index += 1
    end
    
    if index >= length
        index = -1
    end
    index
end

#insertAt(index, item) ⇒ Object

Insert an item before the index indicated



42
43
44
45
46
47
48
49
# File 'lib/jldrill/model/Bin.rb', line 42

def insertAt(index, item)
    if index >= @contents.size
        @contents.push(item)
    else
        @contents.insert(index, item)
    end
    item.bin = @number
end

#insertBefore(item, &block) ⇒ Object

Inserts an item before the one where the block evaluates true. If the block never evaluates true, put the item at the end



63
64
65
66
67
68
69
# File 'lib/jldrill/model/Bin.rb', line 63

def insertBefore(item, &block)
    i = 0
    while(!contents[i].nil? && !block.call(i))
        i += 1
    end
    insertAt(i, item)
end

#lastObject



25
26
27
28
29
30
31
32
# File 'lib/jldrill/model/Bin.rb', line 25

def last
    if length > 0
        item = @contents[length - 1]
        return item
    else
        return nil
    end
end

#lengthObject

Returns the number of items in the bin



15
16
17
# File 'lib/jldrill/model/Bin.rb', line 15

def length()
    return @contents.length
end

#moveBeforeItem(moveItem, beforeItem) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/jldrill/model/Bin.rb', line 51

def moveBeforeItem(moveItem, beforeItem)
    index = @contents.find_index(beforeItem)
    if !index.nil?
        delete(moveItem)
        insertAt(index, moveItem)
    end
end

#numUnseenObject

Returns the number of unseen items in the bin



131
132
133
134
135
136
137
# File 'lib/jldrill/model/Bin.rb', line 131

def numUnseen
    total = 0
    @contents.each do |item|
        total += 1 if !item.schedule.seen
    end
    total
end

#push(item) ⇒ Object

Pushes a item to the end of the bin Also sets the bin number of the item



36
37
38
39
# File 'lib/jldrill/model/Bin.rb', line 36

def push(item)
    item.bin = @number
    @contents.push(item)
end

#reverse_each(&block) ⇒ Object

Calls a block for each item in the bin in the reverse order



83
84
85
86
87
# File 'lib/jldrill/model/Bin.rb', line 83

def reverse_each(&block)
    @contents.reverse_each do |item|
        block.call(item)
    end
end

#setUnseenObject

Sets the schedule of each item in the bin to unseen



180
181
182
183
184
# File 'lib/jldrill/model/Bin.rb', line 180

def setUnseen
    @contents.each do |item|
        item.schedule.seen = false
    end
end

#sort!(&block) ⇒ Object

Sorts the bin according to the criteria specified in the passed in block



99
100
101
102
103
# File 'lib/jldrill/model/Bin.rb', line 99

def sort!(&block)
    @contents.sort! do |x,y|
      block.call(x,y)
    end
end

#to_sObject

Returns a string containing all the item strings in the bin



201
202
203
# File 'lib/jldrill/model/Bin.rb', line 201

def to_s
    @name + "\n" + @contents.join
end