silly_list 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +55 -0
- data/ROADMAP.md +10 -0
- data/Rakefile +8 -0
- data/lib/silly_list.rb +6 -0
- data/lib/silly_list/uniq_lifo.rb +96 -0
- data/lib/silly_list/version.rb +3 -0
- data/silly_list.gemspec +24 -0
- data/spec/spec_helper.rb +1 -0
- data/spec/uniq_lifo_spec.rb +108 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d55d1fdb579cf3a572aef1336337d493b0778a19
|
4
|
+
data.tar.gz: 0d7a8e2d8bef6c1e4e6dd1f8e35806d81a899726
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 60659c3c6e9104eb508bce1f726225f308f638e84e3b91c1fdf4b4436ebcad87624566d7b9da2325492868d88ab3fb0fef2c1438a9c95b4fd44bb5fc0ad14c31
|
7
|
+
data.tar.gz: 80165e6456dcb512de190c7573028eb345413cc5fe434f30c94704cf8707aa0624e60c405c3c80024f8cc501011431c290ce44ebea8885518da2d6f8cb717b14
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
silly_list
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.1.5
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Silicon Salad
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# SillyList
|
2
|
+
|
3
|
+
Array like classes. Add Stack behavior, Queue is coming.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'silly_list'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install silly_list
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
``` ruby
|
24
|
+
class NumberList << SillyList::UniqLifo
|
25
|
+
end
|
26
|
+
|
27
|
+
number_list = NumberList.new([1,2,3], max_size: 2)
|
28
|
+
number_list.list #=> [1,2]
|
29
|
+
number_list.add(3)
|
30
|
+
number_list.list #=> [3,1]
|
31
|
+
number_list.add(3)
|
32
|
+
number_list.list #=> [3,1]
|
33
|
+
number_list.add([4,5])
|
34
|
+
number_list.list #=> [4,5]
|
35
|
+
number_list.remove #=> 4
|
36
|
+
number_list.list #=> [5]
|
37
|
+
```
|
38
|
+
|
39
|
+
## Development
|
40
|
+
|
41
|
+
Documenation:
|
42
|
+
|
43
|
+
$ bundle exec yard server --port 8828 --reload
|
44
|
+
|
45
|
+
Tests:
|
46
|
+
|
47
|
+
$ rake spec
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it ( https://github.com/siliconsalad/silly_list/fork )
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
53
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
55
|
+
5. Create a new Pull Request
|
data/ROADMAP.md
ADDED
data/Rakefile
ADDED
data/lib/silly_list.rb
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
module SillyList
|
2
|
+
# Class UniqLifo designed to manage Last In First Out array (stack)
|
3
|
+
# It ensures that items are uniq.
|
4
|
+
#
|
5
|
+
# The array can have a max size or be illimited (then max_size must be 0)
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# my_array = SillyList::UniqLifo.new([1,2,3], max_size: 2)
|
9
|
+
# my_array.list #=> [1,2]
|
10
|
+
# my_array.add(3)
|
11
|
+
# my_array.list #=> [3,1]
|
12
|
+
# my_array.add([4,5])
|
13
|
+
# my_array.list #=> [4,5]
|
14
|
+
# my_array.remove #=> 4
|
15
|
+
# my_array.list #=> [5]
|
16
|
+
class UniqLifo
|
17
|
+
|
18
|
+
attr_reader :list, :max_size
|
19
|
+
|
20
|
+
def initialize(list=[], max_size: 0)
|
21
|
+
@list = init_list(list, max_size)
|
22
|
+
@max_size = max_size
|
23
|
+
end
|
24
|
+
|
25
|
+
# Checks if list is empty
|
26
|
+
#
|
27
|
+
# @return [Boolean]
|
28
|
+
def empty?
|
29
|
+
list.empty?
|
30
|
+
end
|
31
|
+
|
32
|
+
# Adds an item to the list
|
33
|
+
#
|
34
|
+
# @param [<Object, Array>] object to be added
|
35
|
+
# @return [UniqLifo]
|
36
|
+
def add(item)
|
37
|
+
return unless item
|
38
|
+
|
39
|
+
delete(item)
|
40
|
+
add_at_beginning(item)
|
41
|
+
remove_last if size_exceeded?
|
42
|
+
|
43
|
+
self
|
44
|
+
end
|
45
|
+
|
46
|
+
# Deletes the first item of the list
|
47
|
+
#
|
48
|
+
# @return [Object] removed item
|
49
|
+
def remove
|
50
|
+
list.delete_at(0)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
# Removes the excess of items
|
56
|
+
#
|
57
|
+
# @return [Array<Object>] array of removed item
|
58
|
+
def remove_last
|
59
|
+
index = list.size - max_size
|
60
|
+
list.pop(index)
|
61
|
+
end
|
62
|
+
|
63
|
+
# Ensures that the list does not exceed the max size
|
64
|
+
#
|
65
|
+
# @param [Array<Object>] list to be initialized
|
66
|
+
# @param [Integer] max size
|
67
|
+
# @return [Array<Object>] list
|
68
|
+
def init_list(list, max_size)
|
69
|
+
list[0..max_size-1]
|
70
|
+
end
|
71
|
+
|
72
|
+
# Delete all the occurence of item in the list
|
73
|
+
#
|
74
|
+
# @param [Object] item to be deleted
|
75
|
+
# @return [Object, nil] deleted item or nil
|
76
|
+
def delete(item)
|
77
|
+
list.delete(item)
|
78
|
+
end
|
79
|
+
|
80
|
+
# Insert item at the beginning of the list
|
81
|
+
#
|
82
|
+
# @param [Object] item to be added
|
83
|
+
# @return [Array<Object>] list
|
84
|
+
def add_at_beginning(item)
|
85
|
+
list.unshift(*item)
|
86
|
+
end
|
87
|
+
|
88
|
+
# Checks if list have exceeded the max size
|
89
|
+
#
|
90
|
+
# @return [Boolean]
|
91
|
+
def size_exceeded?
|
92
|
+
return false if max_size.zero?
|
93
|
+
list.size > max_size
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/silly_list.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'silly_list/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'silly_list'
|
8
|
+
spec.version = SillyList::VERSION
|
9
|
+
spec.authors = ['SiliconSalad Tech Team']
|
10
|
+
spec.email = ['tech@siliconsalad.com']
|
11
|
+
spec.summary = 'Array like classes. Add Stack behavior, Queue is coming.'
|
12
|
+
spec.homepage = 'https://github.com/siliconsalad/silly_list'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.7'
|
21
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
22
|
+
spec.add_development_dependency 'rspec', '~> 3.2.0'
|
23
|
+
spec.add_development_dependency 'yard'
|
24
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'silly_list'
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class FakeList < SillyList::UniqLifo
|
4
|
+
end
|
5
|
+
|
6
|
+
describe SillyList::UniqLifo do
|
7
|
+
|
8
|
+
subject { FakeList.new }
|
9
|
+
|
10
|
+
describe '#empty?' do
|
11
|
+
it 'is empty after creation if we pass nothing to it' do
|
12
|
+
expect(subject.empty?).to eq true
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'is not empty when not empty list is given' do
|
16
|
+
list = subject.class.new([double, double])
|
17
|
+
expect(list.empty?).to eq false
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'is no more empty after an addition' do
|
21
|
+
subject.add(double)
|
22
|
+
expect(subject.empty?).to eq false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#list' do
|
27
|
+
it 'limits size of list at initialization' do
|
28
|
+
limit = 3
|
29
|
+
list = [double, double, double, double]
|
30
|
+
uniq_fifo_list = subject.class.new(list, max_size: limit)
|
31
|
+
expect(uniq_fifo_list.list.size).to eq limit
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'does not limit size of the list when max size is not defined' do
|
35
|
+
list = [double, double, double, double]
|
36
|
+
uniq_fifo_list = subject.class.new(list)
|
37
|
+
expect(uniq_fifo_list.list.size).to eq list.size
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#add' do
|
42
|
+
it 'adds an item' do
|
43
|
+
item_1 = double
|
44
|
+
subject.add(item_1)
|
45
|
+
expect(subject.list).to eq([item_1])
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'does not add nil' do
|
49
|
+
subject.add(nil)
|
50
|
+
expect(subject.empty?).to eq(true)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'adds an array' do
|
54
|
+
arr_1 = [double]
|
55
|
+
subject.add(arr_1)
|
56
|
+
expect(subject.list).to eq(arr_1)
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'adds an object when max size is reached' do
|
60
|
+
limit = 2
|
61
|
+
list = [double, double]
|
62
|
+
uniq_fifo_list = subject.class.new(list, max_size: limit)
|
63
|
+
subject.add(double)
|
64
|
+
expect(uniq_fifo_list.list.size).to eq 2
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'adds the last insertion at the first position of the list' do
|
68
|
+
item_1 = double
|
69
|
+
item_2 = double
|
70
|
+
subject.add(item_1)
|
71
|
+
subject.add(item_2)
|
72
|
+
expect(subject.list).to eq([item_2, item_1])
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'item is added twice' do
|
76
|
+
before do
|
77
|
+
@an_item = double
|
78
|
+
subject.add(@an_item)
|
79
|
+
subject.add(@an_item)
|
80
|
+
end
|
81
|
+
|
82
|
+
it 'keeps only one item' do
|
83
|
+
expect(subject.list.count(@an_item)).to eq(1)
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'keeps the last insertion and remove the first' do
|
87
|
+
expect(subject.list.index(@an_item)).to eq(0)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe '#remove' do
|
93
|
+
it 'returns the last added item' do
|
94
|
+
item = double
|
95
|
+
subject.add(item)
|
96
|
+
expect(subject.remove).to eq(item)
|
97
|
+
end
|
98
|
+
|
99
|
+
it 'removes the last added item' do
|
100
|
+
item_1 = double
|
101
|
+
item_2 = double
|
102
|
+
subject.add(item_1)
|
103
|
+
subject.add(item_2)
|
104
|
+
subject.remove
|
105
|
+
expect(subject.list).to eq [item_1]
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: silly_list
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- SiliconSalad Tech Team
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-02-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.2.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.2.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: yard
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- tech@siliconsalad.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".ruby-gemset"
|
78
|
+
- ".ruby-version"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- ROADMAP.md
|
83
|
+
- Rakefile
|
84
|
+
- lib/silly_list.rb
|
85
|
+
- lib/silly_list/uniq_lifo.rb
|
86
|
+
- lib/silly_list/version.rb
|
87
|
+
- silly_list.gemspec
|
88
|
+
- spec/spec_helper.rb
|
89
|
+
- spec/uniq_lifo_spec.rb
|
90
|
+
homepage: https://github.com/siliconsalad/silly_list
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.4.4
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Array like classes. Add Stack behavior, Queue is coming.
|
114
|
+
test_files:
|
115
|
+
- spec/spec_helper.rb
|
116
|
+
- spec/uniq_lifo_spec.rb
|
117
|
+
has_rdoc:
|