prioritizable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ test/dummy/db/*.sqlite3
5
+ test/dummy/log/*.log
6
+ test/dummy/tmp/
7
+ .idea/
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = Prioritizable
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Prioritizable'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
@@ -0,0 +1,4 @@
1
+ require "prioritizable/acts_as_prioritizable"
2
+
3
+ module Prioritizable
4
+ end
@@ -0,0 +1,65 @@
1
+ module ActsAsPrioritizable
2
+ def self.included(base)
3
+ base.extend(ClassMethods)
4
+ end
5
+
6
+ module ClassMethods
7
+ def acts_as_prioritizable(prioritizable_parent = :prioritizable_parent, prioritizables = :all)
8
+ class_variable_set :@@prioritizable_parent, prioritizable_parent
9
+ class_variable_set :@@prioritizables, prioritizables
10
+ attr_accessible :priority
11
+ before_save :before_save
12
+ after_destroy :after_destroy
13
+ include ActsAsPrioritizable::InstanceMethods
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+ def map_for_select(method)
19
+ [self.send(method), priority]
20
+ end
21
+
22
+ def prioritizable_parent
23
+ logger.info self.class.send(:class_variable_get, :@@prioritizable_parent) + "\n"
24
+ if self.class.send(:class_variable_get, :@@prioritizable_parent) == 'class'
25
+ return self.class
26
+ else
27
+ self.send self.class.send(:class_variable_get, :@@prioritizable_parent), true
28
+ end
29
+ end
30
+
31
+ def prioritizables
32
+ prioritizable_parent.send self.class.send(:class_variable_get, :@@prioritizables)
33
+ end
34
+
35
+ def set_priority
36
+ if (priority.nil? || priority == 0 || priority.blank?)
37
+ update_attribute(:priority, lowest_priority + 1)
38
+ end
39
+ end
40
+
41
+ def before_save
42
+ unless prioritizable_parent.nil?
43
+ bump = (prioritizables - [self]).compact.select{|owner| owner.priority.to_i == priority.to_i}
44
+ bump.first.update_attribute(:priority, priority.to_i+1) unless bump.empty?
45
+ end
46
+ end
47
+
48
+ def after_destroy
49
+ if(priority < lowest_priority && !prioritizable_parent.nil? rescue false)
50
+ prioritizables.select{|p| p.priority > priority}.each{|p| p.update_attribute(:priority, p.priority-1)}
51
+ end
52
+ end
53
+
54
+ def reset_all_priorities
55
+ if(!prioritizable_parent.nil?)
56
+ i = 0
57
+ prioritizables.each{|p| p.update_attribute(:priority, (i+=1))}
58
+ end
59
+ end
60
+
61
+ def lowest_priority
62
+ prioritizables.map(&:priority).compact.max || 0
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module Prioritizable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :prioritizable do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "prioritizable/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "prioritizable"
7
+ s.version = Prioritizable::VERSION
8
+ s.authors = ["Adam Olsen"]
9
+ s.email = ["aosalias@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Prioritizable}
12
+ s.description = %q{Prioritizable}
13
+
14
+ s.rubyforge_project = "sbdev"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: prioritizable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Olsen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-19 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Prioritizable
15
+ email:
16
+ - aosalias@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - MIT-LICENSE
23
+ - README.rdoc
24
+ - Rakefile
25
+ - lib/prioritizable.rb
26
+ - lib/prioritizable/acts_as_prioritizable.rb
27
+ - lib/prioritizable/version.rb
28
+ - lib/tasks/prioritizable_tasks.rake
29
+ - prioritizable.gemspec
30
+ homepage: ''
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project: sbdev
50
+ rubygems_version: 1.8.6
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: Prioritizable
54
+ test_files: []