acts_as_flags 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in acts_as_flags.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "acts_as_flags/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "acts_as_flags"
7
+ s.version = ActsAsFlags::VERSION
8
+ s.authors = ["alepaez"]
9
+ s.email = ["alepaezseq@gmail.com"]
10
+ s.homepage = "https://github.com/iugu/acts_as_flags"
11
+ s.summary = %q{Make ActiveRecord Attributes to act as flags}
12
+ s.description = %q{Make ActiveRecord Attributes to act as flags}
13
+
14
+ s.rubyforge_project = "acts_as_flags"
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
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'acts_as_flags'
@@ -0,0 +1,3 @@
1
+ module ActsAsFlags
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ module ActsAsFlags
2
+ def acts_as_flags( field, flags )
3
+
4
+ flag_variable = '@' + field.to_s.upcase() + '_FLAGS'
5
+ instance_variable_set flag_variable, flags
6
+
7
+ flags.each do |flag|
8
+ (class << self; self; end).send(:define_method, "only_#{flag}") do
9
+ where(["#{field}_mask & ? > 0", 2**flags.index( flag.to_s ) ] )
10
+ end
11
+ end
12
+
13
+ define_method "#{field}=" do |new_flags|
14
+ self.send("#{field}_mask=", (new_flags & flags).map { |r| 2**flags.index(r) }.sum )
15
+ end
16
+
17
+ define_method "#{field}" do
18
+ flags.reject { |r| (( self.send("#{field}_mask") || 0 ) & 2**flags.index(r)).zero? }
19
+ end
20
+
21
+ define_method "#{field}?" do |flag|
22
+ self.send("#{field}").include? flag.to_s
23
+ end
24
+ end
25
+ end
26
+
27
+ if defined?(ActiveRecord::Base)
28
+ ActiveRecord::Base.extend ActsAsFlags
29
+ end
data/readme.md ADDED
@@ -0,0 +1,34 @@
1
+ Acts As Flags
2
+ ===============
3
+
4
+ Installation
5
+ ---------------
6
+ **In your Project's Gemfile**
7
+ > gem 'acts_as_flags'
8
+
9
+ How to use it
10
+ ---------------
11
+ **Add it to your model**
12
+ > acts_as_flags :flags, %w[ sent re-sent ]
13
+
14
+ **Add flag mask to your table**
15
+ > \#rails g migration AddFlagsMaskToFoo
16
+ >
17
+ > add_column :foos, :flags_mask, :integer
18
+ >
19
+ > \#rake db:migrate
20
+
21
+ Instance Methods
22
+ ----------------
23
+
24
+ **Flags setter and getter**
25
+
26
+ > @foo.flags = [ "sent" ]
27
+ >
28
+ > @foo.flags
29
+ >
30
+ > => [ "sent" ]
31
+
32
+
33
+
34
+
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_flags
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - alepaez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-27 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Make ActiveRecord Attributes to act as flags
15
+ email:
16
+ - alepaezseq@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - Rakefile
24
+ - acts_as_flags.gemspec
25
+ - init.rb
26
+ - lib/acts_as_flags.rb
27
+ - lib/acts_as_flags/version.rb
28
+ - readme.md
29
+ homepage: https://github.com/iugu/acts_as_flags
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project: acts_as_flags
49
+ rubygems_version: 1.8.23
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Make ActiveRecord Attributes to act as flags
53
+ test_files: []