tm-flagged 0.0.1.beta

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
@@ -0,0 +1,9 @@
1
+ == master
2
+
3
+ == 0.0.1.alpha / 2010-11-10
4
+
5
+ * fixed key checking
6
+
7
+ == 0.0.1 / 2010-11-09
8
+
9
+ * Initial version
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Tom Meinlschmidt
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.
@@ -0,0 +1,56 @@
1
+ = tm-flagged
2
+
3
+ Original idea by github.com/xing/flag_shin_tzu
4
+
5
+ warning:
6
+
7
+ NOT FOR PRODUCTION USE NOW
8
+
9
+ ==Installation
10
+
11
+ gem install tm-flagged
12
+
13
+ ==Usage
14
+
15
+ ===ActiveRecord
16
+
17
+ You have to add one field to your model, eg. named +flags+
18
+
19
+ add_column :transactions, :flags, :integer, :default => 0
20
+
21
+ ===DataMapper
22
+
23
+ just add new property
24
+
25
+ property :flags, Integer, :default => 0
26
+
27
+ ===In model
28
+
29
+ just add this +has_flags+ to your model to define flags
30
+
31
+ has_flags 1 => :sun,
32
+ 2 => :clouds,
33
+ 3 => :rain,
34
+ 4 => :frogs,
35
+ 5 => :ufo
36
+
37
+ Then you could use autogenerated methods like
38
+
39
+ model.sun? # => true/false
40
+ model.sun=true
41
+ model.sun # => true/false
42
+
43
+ If you want to use another column than +flags+, use +:column+ option
44
+
45
+ property :weather, Integer, :default => 0
46
+
47
+ has_flags 1 => :sun,
48
+ 2 => :clouds,
49
+ :column => :weather
50
+
51
+ model.clouds?
52
+
53
+ ==License
54
+
55
+ Copyright (c) 2010 Tom Meinlschmidt, see LICENSE file
56
+
@@ -0,0 +1,55 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ require './lib/version.rb'
7
+ Jeweler::Tasks.new do |gem|
8
+ gem.name = "tm-flagged"
9
+ gem.summary = %Q{Add flags support to your model}
10
+ gem.description = %Q{Do not create more boolean fields :)}
11
+ gem.email = "tomas@meinlschmidt.com"
12
+ gem.homepage = "http://github.com/tmeinlschmidt/tm-flagged"
13
+ gem.authors = ["Tom Meinlschmidt"]
14
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
15
+ gem.version = FlaggedModel::Version::STRING
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'rake/testtask'
24
+ Rake::TestTask.new(:test) do |test|
25
+ test.libs << 'lib' << 'test'
26
+ test.pattern = 'test/**/test_*.rb'
27
+ test.verbose = true
28
+ end
29
+
30
+ begin
31
+ require 'rcov/rcovtask'
32
+ Rcov::RcovTask.new do |test|
33
+ test.libs << 'test'
34
+ test.pattern = 'test/**/test_*.rb'
35
+ test.verbose = true
36
+ end
37
+ rescue LoadError
38
+ task :rcov do
39
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
40
+ end
41
+ end
42
+
43
+ task :test => :check_dependencies
44
+
45
+ task :default => :test
46
+
47
+ require 'rake/rdoctask'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "tm-flagged #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'tm-flagged'
@@ -0,0 +1,160 @@
1
+ # flagged model
2
+ # original idea from xing/flag_shin_tzu
3
+ # add fields "flags" as :integer
4
+ # add "include FlaggedModel"
5
+ # add has_flags 1 => :active, 2 => :common, 3 => :another, :column => 'flags'
6
+ module FlaggedModel
7
+
8
+ TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE']
9
+
10
+ # def self.included(base)
11
+ # base.extend ClassMethods
12
+ # end
13
+
14
+ module ClassMethods
15
+
16
+ def has_flags(*args)
17
+ # process options
18
+ flag_hash, options = parse_options(*args)
19
+ options = {:named_scopes => true, :column => 'flags'}.update(options)
20
+
21
+ # create class variables
22
+ class_inheritable_reader :flag_column
23
+ write_inheritable_attribute(:flag_column, options[:column])
24
+ flag_column = options[:column]
25
+ # check column
26
+
27
+ raise ArgumentError, "has_flags: column named '#{flag_column}' doesn't exist" unless check_column_presence
28
+
29
+ # mappings
30
+ class_inheritable_hash :flag_mapping
31
+ write_inheritable_attribute(:flag_mapping, {}) unless flag_mapping
32
+ flag_mapping[flag_column] ||= {}
33
+
34
+ # check & set the mappings values
35
+ flag_hash.each do |key, name|
36
+ # invalid attributes
37
+ raise ArgumentError, "has_flags: invalid key '#{name}'" unless valid_key?(key)
38
+ raise ArgumentError, "has_flags: invalid name '#{name}'" unless valid_name?(name)
39
+ raise ArgumentError, "has_flags: name exists '#{name}'" if method_defined?(name)
40
+ flag_mapping[flag_column][name] = to_bits(key)
41
+
42
+ class_eval <<-EVAL
43
+ def #{name}
44
+ flag_enabled?(:#{name})
45
+ end
46
+
47
+ def #{name}?
48
+ flag_enabled?(:#{name})
49
+ end
50
+
51
+ def #{name}=(value)
52
+ FlaggedModel::TRUE_VALUES.include?(value) ? enable_flag(:#{name}) : disable_flag(:#{name})
53
+ end
54
+ EVAL
55
+ end
56
+ # pretty print
57
+ class_eval <<-EVAL
58
+ def pp_#{flag_column.to_s}
59
+ pretty_print_flags
60
+ end
61
+ EVAL
62
+ end
63
+
64
+ private
65
+
66
+ def to_bits(key)
67
+ (1 << (key-1))
68
+ end
69
+
70
+ def check_column_presence
71
+ # ActiveRecord
72
+ if respond_to?(:columns) && defined?(ActiveRecord)
73
+ return columns.any?{|col| (col.name == flag_column.to_s && col.type == :integer)}
74
+ end
75
+ if respond_to?(:properties)
76
+ return (properties[flag_column.to_sym] && properties[flag_column.to_sym].class == DataMapper::Property::Integer)
77
+ end
78
+ false
79
+ end
80
+
81
+ # is key valid?
82
+ # suppose 64bit bigint ("select ~0;")
83
+ def valid_key?(key)
84
+ (key > 0 && key<=64 && key == key.to_i && !flag_mapping[flag_column].values.include?(to_bits(key)))
85
+ end
86
+
87
+ # is name valid?
88
+ def valid_name?(name)
89
+ (name.is_a?(Symbol) && !flag_mapping[flag_column].keys.include?(name))
90
+ end
91
+
92
+ # parse options
93
+ def parse_options(*args)
94
+ options = args.shift
95
+ if args.size >= 1
96
+ add_options = args.shift
97
+ else
98
+ add_options = options.keys.select {|key| !key.is_a?(Fixnum)}.inject({}) do |hash, key|
99
+ hash[key] = options.delete(key)
100
+ hash
101
+ end
102
+ end
103
+ return options, add_options
104
+ end
105
+
106
+ end
107
+
108
+ # pretty print flags set
109
+ def pretty_print_flags
110
+ get_flags.each do |name, val|
111
+ STDERR.puts "%1d :%s" % [flag_enabled?(name)?1:0, name]
112
+ end
113
+ nil
114
+ end
115
+
116
+ # returns true if specified flag is TRUE
117
+ def flag_enabled?(flag)
118
+ (_flags & get_flag(flag)) == get_flag(flag)
119
+ end
120
+
121
+ # !dtto
122
+ def flag_disabled?(flag)
123
+ !flag_enabled?(flag)
124
+ end
125
+
126
+ # set flag to 1
127
+ def enable_flag(flag)
128
+ set_flags(_flags | get_flag(flag))
129
+ end
130
+
131
+ # reset flag to 0
132
+ def disable_flag(flag)
133
+ set_flags(_flags & ~get_flag(flag))
134
+ end
135
+
136
+ # get all the flags
137
+ def get_flags
138
+ self.class.flag_mapping[self.class.flag_column]
139
+ end
140
+
141
+ # get specified flag
142
+ def get_flag(flag)
143
+ get_flags[flag]
144
+ end
145
+
146
+ # get flag column
147
+ def _flags
148
+ self[self.class.flag_column] || 0
149
+ end
150
+
151
+ # get flags
152
+ def set_flags(value)
153
+ self[self.class.flag_column] = value
154
+ end
155
+ end
156
+
157
+ Class.class_eval do
158
+ include FlaggedModel::ClassMethods
159
+ end
160
+
@@ -0,0 +1,10 @@
1
+ module FlaggedModel
2
+ module Version
3
+ MAJOR = 0
4
+ MINOR = 0
5
+ PATCH = 1
6
+ BUILD = 'beta'
7
+
8
+ STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'tm-flagged'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestTmFlagged < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,56 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{tm-flagged}
8
+ s.version = "0.0.1.beta"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tom Meinlschmidt"]
12
+ s.date = %q{2010-11-10}
13
+ s.description = %q{Do not create more boolean fields :)}
14
+ s.email = %q{tomas@meinlschmidt.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "CHANGELOG.rdoc",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "init.rb",
27
+ "lib/tm-flagged.rb",
28
+ "lib/version.rb",
29
+ "test/helper.rb",
30
+ "test/test_tm-flagged.rb",
31
+ "tm-flagged.gemspec"
32
+ ]
33
+ s.homepage = %q{http://github.com/tmeinlschmidt/tm-flagged}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.7}
37
+ s.summary = %q{Add flags support to your model}
38
+ s.test_files = [
39
+ "test/helper.rb",
40
+ "test/test_tm-flagged.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
48
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
54
+ end
55
+ end
56
+
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tm-flagged
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: true
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ - beta
10
+ version: 0.0.1.beta
11
+ platform: ruby
12
+ authors:
13
+ - Tom Meinlschmidt
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-11-10 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: thoughtbot-shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Do not create more boolean fields :)
35
+ email: tomas@meinlschmidt.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.rdoc
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - CHANGELOG.rdoc
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - init.rb
51
+ - lib/tm-flagged.rb
52
+ - lib/version.rb
53
+ - test/helper.rb
54
+ - test/test_tm-flagged.rb
55
+ - tm-flagged.gemspec
56
+ has_rdoc: true
57
+ homepage: http://github.com/tmeinlschmidt/tm-flagged
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options:
62
+ - --charset=UTF-8
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">"
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 1
80
+ - 3
81
+ - 1
82
+ version: 1.3.1
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.3.7
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Add flags support to your model
90
+ test_files:
91
+ - test/helper.rb
92
+ - test/test_tm-flagged.rb