array_with_priority 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/Rakefile +13 -0
- data/array_with_priority.gemspec +20 -0
- data/lib/array_with_priority.rb +39 -0
- data/lib/array_with_priority/version.rb +3 -0
- data/readme.md +27 -0
- data/spec/array_with_priority_spec.rb +55 -0
- data/spec/spec_helper.rb +2 -0
- metadata +98 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rspec/core/rake_task'
|
6
|
+
RSpec::Core::RakeTask.new(:spec)
|
7
|
+
rescue LoadError
|
8
|
+
task :spec do
|
9
|
+
fail "Couldn't load rspec, please fix to run the tests"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
task :default => :spec
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/array_with_priority/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Caius Durling"]
|
6
|
+
gem.email = ["caius@swedishcampground.com"]
|
7
|
+
gem.description = %q{An array with priority groups within it}
|
8
|
+
gem.summary = %q{Lets you add items to an array in priority to each other. Useful if you need to group a and b before c or d, but don't care about the order of a and b.}
|
9
|
+
gem.homepage = "http://github.com/caius/array_with_priority"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "array_with_priority"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = ArrayWithPriority::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency "active_support", ">= 2.1.0"
|
19
|
+
gem.add_development_dependency "rspec", ">= 2.0"
|
20
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require "array_with_priority/version"
|
2
|
+
require "active_support/core_ext/class/attribute_accessors.rb"
|
3
|
+
|
4
|
+
class ArrayWithPriority
|
5
|
+
cattr_accessor :default_priority
|
6
|
+
self.default_priority = 5
|
7
|
+
|
8
|
+
def add obj, opts={}
|
9
|
+
i = opts[:priority] || self.default_priority
|
10
|
+
self.array[i] ||= []
|
11
|
+
self.array[i] << obj
|
12
|
+
end
|
13
|
+
|
14
|
+
def << obj
|
15
|
+
add obj
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_a
|
19
|
+
array.compact
|
20
|
+
end
|
21
|
+
|
22
|
+
def method_missing meffod, *args
|
23
|
+
if ((a = to_a)) && a.respond_to?(meffod)
|
24
|
+
if block_given?
|
25
|
+
blk = Proc.new
|
26
|
+
a.send(meffod, *args, &blk)
|
27
|
+
else
|
28
|
+
a.send(meffod, *args)
|
29
|
+
end
|
30
|
+
else
|
31
|
+
super
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
protected
|
36
|
+
def array
|
37
|
+
@array ||= []
|
38
|
+
end
|
39
|
+
end
|
data/readme.md
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# Array With Priority
|
2
|
+
|
3
|
+
Lets you add items in groups of priority. Useful if you've got 10 tasks to run, and need 1,2,3 to happen before 4,5,6,7 and then 8,9 before 10 but don't care about the order in each cluster.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
ap = ArrayWithPriority.new
|
8
|
+
ap << :foo # default priority is 5
|
9
|
+
ap.push :bar, :priority => 2
|
10
|
+
ap.push :sed, :priority => 2
|
11
|
+
ap.push :fud, :priority => 10
|
12
|
+
|
13
|
+
ap.to_a # => [[:bar, :sed], [:foo], [:fud]]
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
gem install array_with_priority
|
18
|
+
|
19
|
+
## Licence
|
20
|
+
|
21
|
+
Copyright (c) 2011 Caius Durling <caius@swedishcampground.com>
|
22
|
+
|
23
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
24
|
+
|
25
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
26
|
+
|
27
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -0,0 +1,55 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe ArrayWithPriority do
|
4
|
+
|
5
|
+
before do
|
6
|
+
@a = ArrayWithPriority.new
|
7
|
+
@a.add :highest, :priority => 1
|
8
|
+
@a.add :high, :priority => 2
|
9
|
+
@a.add :default
|
10
|
+
@a << :default_2
|
11
|
+
@a.add :low, :priority => 10
|
12
|
+
@a.add :very_low, :priority => 1024
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#add" do
|
16
|
+
describe "with a priority" do
|
17
|
+
it "should add the element with the specified priority" do
|
18
|
+
@a[0].should include(:highest)
|
19
|
+
@a[1].should include(:high)
|
20
|
+
@a[3].should include(:low)
|
21
|
+
@a[4].should include(:very_low)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
describe "without a priority" do
|
25
|
+
it "should add the element with default priority" do
|
26
|
+
@a[2].should include(:default)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#<<" do
|
32
|
+
it "should add the element with default priority" do
|
33
|
+
@a[2].should include(:default_2)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#method_missing" do
|
38
|
+
describe "with no arguments" do
|
39
|
+
it "should proxy to Array successfully" do
|
40
|
+
@a.first.should == [:highest]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
describe "with arguments" do
|
44
|
+
it "should proxy to Array successfully" do
|
45
|
+
@a.first(2).should == [[:highest], [:high]]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
describe "with a block" do
|
49
|
+
it "should proxy to Array successfully" do
|
50
|
+
@a.each {|x| x.should_not be_nil }.should == [[:highest], [:high], [:default, :default_2], [:low], [:very_low]]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: array_with_priority
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Caius Durling
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-11-12 00:00:00 +00:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: active_support
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 2
|
29
|
+
- 1
|
30
|
+
- 0
|
31
|
+
version: 2.1.0
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rspec
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 2
|
43
|
+
- 0
|
44
|
+
version: "2.0"
|
45
|
+
type: :development
|
46
|
+
version_requirements: *id002
|
47
|
+
description: An array with priority groups within it
|
48
|
+
email:
|
49
|
+
- caius@swedishcampground.com
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files: []
|
55
|
+
|
56
|
+
files:
|
57
|
+
- .gitignore
|
58
|
+
- Gemfile
|
59
|
+
- Rakefile
|
60
|
+
- array_with_priority.gemspec
|
61
|
+
- lib/array_with_priority.rb
|
62
|
+
- lib/array_with_priority/version.rb
|
63
|
+
- readme.md
|
64
|
+
- spec/array_with_priority_spec.rb
|
65
|
+
- spec/spec_helper.rb
|
66
|
+
has_rdoc: true
|
67
|
+
homepage: http://github.com/caius/array_with_priority
|
68
|
+
licenses: []
|
69
|
+
|
70
|
+
post_install_message:
|
71
|
+
rdoc_options: []
|
72
|
+
|
73
|
+
require_paths:
|
74
|
+
- lib
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
segments:
|
80
|
+
- 0
|
81
|
+
version: "0"
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
segments:
|
87
|
+
- 0
|
88
|
+
version: "0"
|
89
|
+
requirements: []
|
90
|
+
|
91
|
+
rubyforge_project:
|
92
|
+
rubygems_version: 1.3.6
|
93
|
+
signing_key:
|
94
|
+
specification_version: 3
|
95
|
+
summary: Lets you add items to an array in priority to each other. Useful if you need to group a and b before c or d, but don't care about the order of a and b.
|
96
|
+
test_files:
|
97
|
+
- spec/array_with_priority_spec.rb
|
98
|
+
- spec/spec_helper.rb
|