pkwde-has_set 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/Manifest.txt +11 -0
- data/README.txt +74 -0
- data/Rakefile +37 -0
- data/has_set.gemspec +53 -0
- data/lib/has_set.rb +81 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/test/has_set_test.rb +87 -0
- data/test/test_helper.rb +91 -0
- metadata +136 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
= has_set
|
2
|
+
|
3
|
+
* http://github.com/pkwde/has_set
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
A simple Gem to enable any `ActiveRecord::Base` object to store a set of attributes in a set like structure represented through a bitfield on the database level.
|
8
|
+
|
9
|
+
You only have to specify the name of the set to hold the attributes in question an the rest is done for you through some fine selected Ruby magic. Here is a simple example of how you could use the gem:
|
10
|
+
|
11
|
+
class Person < ActiveRecord::Base
|
12
|
+
has_set :interests
|
13
|
+
end
|
14
|
+
|
15
|
+
To get this to work you need some additional work done first:
|
16
|
+
|
17
|
+
1. You need an unsigned 8-Byte integer column in your database to store the bitfield. It is expected that the column is named after the name of the set with the suffix `_bitfield` appended (e.g. `interests_bitfield`). You can change that default behavior by providing the option `:column_name` (e.g. `has_set :interests, :column_name => :my_custom_column`).
|
18
|
+
2. You need a class that provides the valid values to be stored within the set and map the single bits back to something meaningful. The class should be named after the name of the set (you can change this through the `:enum_class` option). This class could be seen as an enumeration and must implement the following simple interface:
|
19
|
+
* There must be a class method `values` to return all valid enumerators in the defined enumeration.
|
20
|
+
* Each enumerator must implement a `name` method to return a literal representation for identification. The literal must be of the type `String`.
|
21
|
+
* Each enumerator must implement a `bitfield_index` method to return the exponent of the number 2 for calculation the position of this enumerator in the bitfield. **Attention** Changing this index afterwards will destroy your data integrity.
|
22
|
+
|
23
|
+
Here is a simple example of how to implement such a enumeration type while using the the `renum` gem for simplicity. You are free to use anything else that matches the described interface.
|
24
|
+
|
25
|
+
enum :Interests do
|
26
|
+
attr_reader :bitfield_index
|
27
|
+
|
28
|
+
Art(0)
|
29
|
+
Golf(1)
|
30
|
+
Sleeping(2)
|
31
|
+
Drinking(3)
|
32
|
+
Dating(4)
|
33
|
+
Shopping(5)
|
34
|
+
|
35
|
+
def init(bitfield_index)
|
36
|
+
@bitfield_index = bitfield_index
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
== REQUIREMENTS:
|
42
|
+
|
43
|
+
* `ActiveRecord`
|
44
|
+
* `ActiveSupport`
|
45
|
+
* `pkwde-renum` (*optionally*)
|
46
|
+
|
47
|
+
== INSTALL:
|
48
|
+
|
49
|
+
sudo gem install pkwde-has_set --source http://gems.github.com
|
50
|
+
|
51
|
+
== LICENSE:
|
52
|
+
|
53
|
+
(The MIT License)
|
54
|
+
|
55
|
+
Copyright (c) 2009 pkw.de Dev Team
|
56
|
+
|
57
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
58
|
+
a copy of this software and associated documentation files (the
|
59
|
+
'Software'), to deal in the Software without restriction, including
|
60
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
61
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
62
|
+
permit persons to whom the Software is furnished to do so, subject to
|
63
|
+
the following conditions:
|
64
|
+
|
65
|
+
The above copyright notice and this permission notice shall be
|
66
|
+
included in all copies or substantial portions of the Software.
|
67
|
+
|
68
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
69
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
70
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
71
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
72
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
73
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
74
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rubygems' unless ENV['NO_RUBYGEMS']
|
2
|
+
%w[rake rake/clean fileutils newgem rubigen].each { |f| require f }
|
3
|
+
require File.dirname(__FILE__) + '/lib/has_set'
|
4
|
+
|
5
|
+
# Generate all the Rake tasks
|
6
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
7
|
+
$hoe = Hoe.new('has_set', HasSet::VERSION) do |p|
|
8
|
+
p.developer('pkw.de Dev Team', 'dev@pkw.de')
|
9
|
+
p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
|
10
|
+
p.extra_deps = [
|
11
|
+
['activerecord', '= 2.2.2'],
|
12
|
+
['activesupport', '= 2.2.2']
|
13
|
+
]
|
14
|
+
p.extra_dev_deps = [
|
15
|
+
['newgem', ">= #{::Newgem::VERSION}"],
|
16
|
+
['mocha'],
|
17
|
+
['pkwde-renum'],
|
18
|
+
['sqlite3-ruby']
|
19
|
+
]
|
20
|
+
|
21
|
+
p.name = "has_set"
|
22
|
+
p.clean_globs |= %w[**/.DS_Store tmp *.log]
|
23
|
+
p.summary = "A Gem that enables ActiveRecord models to have a set of values defined in a certain class and stored in an integer bitfield on the database level."
|
24
|
+
end
|
25
|
+
|
26
|
+
require 'newgem/tasks' # load /tasks/*.rake
|
27
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
28
|
+
|
29
|
+
task :default => [:test]
|
30
|
+
|
31
|
+
desc "Run unit tests"
|
32
|
+
Rake::TestTask.new(:test) do |t|
|
33
|
+
t.libs << 'test'
|
34
|
+
t.test_files = FileList['test/**/*_test.rb']
|
35
|
+
t.verbose = true
|
36
|
+
t.warning = false
|
37
|
+
end
|
data/has_set.gemspec
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{has_set}
|
5
|
+
s.version = "0.0.1"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["pkw.de Dev Team"]
|
9
|
+
s.date = %q{2009-04-16}
|
10
|
+
s.description = %q{A simple Gem to enable any `ActiveRecord::Base` object to store a set of attributes in a set like structure represented through a bitfield on the database level. You only have to specify the name of the set to hold the attributes in question an the rest is done for you through some fine selected Ruby magic. Here is a simple example of how you could use the gem: class Person < ActiveRecord::Base has_set :interests end To get this to work you need some additional work done first: 1. You need an unsigned 8-Byte integer column in your database to store the bitfield. It is expected that the column is named after the name of the set with the suffix `_bitfield` appended (e.g. `interests_bitfield`). You can change that default behavior by providing the option `:column_name` (e.g. `has_set :interests, :column_name => :my_custom_column`). 2. You need a class that provides the valid values to be stored within the set and map the single bits back to something meaningful. The class should be named after the name of the set (you can change this through the `:enum_class` option). This class could be seen as an enumeration and must implement the following simple interface: * There must be a class method `values` to return all valid enumerators in the defined enumeration. * Each enumerator must implement a `name` method to return a literal representation for identification. The literal must be of the type `String`. * Each enumerator must implement a `bitfield_index` method to return the exponent of the number 2 for calculation the position of this enumerator in the bitfield. **Attention** Changing this index afterwards will destroy your data integrity. Here is a simple example of how to implement such a enumeration type while using the the `renum` gem for simplicity. You are free to use anything else that matches the described interface. enum :Interests do attr_reader :bitfield_index Art(0) Golf(1) Sleeping(2) Drinking(3) Dating(4) Shopping(5) def init(bitfield_index) @bitfield_index = bitfield_index end end}
|
11
|
+
s.email = ["dev@pkw.de"]
|
12
|
+
s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
|
13
|
+
s.files = ["History.txt", "Manifest.txt", "README.txt", "Rakefile", "has_set.gemspec", "lib/has_set.rb", "script/console", "script/destroy", "script/generate", "test/has_set_test.rb", "test/test_helper.rb"]
|
14
|
+
s.has_rdoc = true
|
15
|
+
s.homepage = %q{http://github.com/pkwde/has_set}
|
16
|
+
s.rdoc_options = ["--main", "README.txt"]
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.rubyforge_project = %q{has_set}
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
20
|
+
s.summary = %q{A Gem that enables ActiveRecord models to have a set of values defined in a certain class and stored in an integer bitfield on the database level.}
|
21
|
+
s.test_files = ["test/test_helper.rb"]
|
22
|
+
|
23
|
+
if s.respond_to? :specification_version then
|
24
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
25
|
+
s.specification_version = 2
|
26
|
+
|
27
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
28
|
+
s.add_runtime_dependency(%q<activerecord>, ["= 2.2.2"])
|
29
|
+
s.add_runtime_dependency(%q<activesupport>, ["= 2.2.2"])
|
30
|
+
s.add_development_dependency(%q<newgem>, [">= 1.3.0"])
|
31
|
+
s.add_development_dependency(%q<mocha>, [">= 0"])
|
32
|
+
s.add_development_dependency(%q<pkwde-renum>, [">= 0"])
|
33
|
+
s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
|
34
|
+
s.add_development_dependency(%q<hoe>, [">= 1.8.0"])
|
35
|
+
else
|
36
|
+
s.add_dependency(%q<activerecord>, ["= 2.2.2"])
|
37
|
+
s.add_dependency(%q<activesupport>, ["= 2.2.2"])
|
38
|
+
s.add_dependency(%q<newgem>, [">= 1.3.0"])
|
39
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
40
|
+
s.add_dependency(%q<pkwde-renum>, [">= 0"])
|
41
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
42
|
+
s.add_dependency(%q<hoe>, [">= 1.8.0"])
|
43
|
+
end
|
44
|
+
else
|
45
|
+
s.add_dependency(%q<activerecord>, ["= 2.2.2"])
|
46
|
+
s.add_dependency(%q<activesupport>, ["= 2.2.2"])
|
47
|
+
s.add_dependency(%q<newgem>, [">= 1.3.0"])
|
48
|
+
s.add_dependency(%q<mocha>, [">= 0"])
|
49
|
+
s.add_dependency(%q<pkwde-renum>, [">= 0"])
|
50
|
+
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
51
|
+
s.add_dependency(%q<hoe>, [">= 1.8.0"])
|
52
|
+
end
|
53
|
+
end
|
data/lib/has_set.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'activesupport'
|
5
|
+
require 'activerecord'
|
6
|
+
|
7
|
+
module HasSet
|
8
|
+
VERSION = '0.0.1'
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
# Use like this:
|
13
|
+
#
|
14
|
+
# class Person < ActiveRecord::Base
|
15
|
+
# has_set :interests
|
16
|
+
# end
|
17
|
+
def has_set(set_name, options = {})
|
18
|
+
|
19
|
+
set_column = options.has_key?(:column_name) ? options[:column_name].to_s : "#{set_name}_bitfield"
|
20
|
+
|
21
|
+
unless self.columns_hash[set_column.to_s].type == :integer
|
22
|
+
raise ActiveRecord::ActiveRecordError, "The column '#{set_column}' to store the the set must be of type integer, because we store them in a bitfield."
|
23
|
+
end
|
24
|
+
|
25
|
+
begin
|
26
|
+
enum_class = options.has_key?(:enum_class) ? options[:enum_class] : Object.const_get(set_name.to_s.camelcase)
|
27
|
+
rescue NameError => ne
|
28
|
+
raise NameError, "There ist no class to take the set entries from (#{ne.message})."
|
29
|
+
end
|
30
|
+
|
31
|
+
define_method("#{set_name}=") do |set_elements|
|
32
|
+
if set_elements.blank?
|
33
|
+
self[set_column] = 0
|
34
|
+
else
|
35
|
+
[set_elements].flatten.each do |element|
|
36
|
+
unless element.kind_of? enum_class
|
37
|
+
raise ArgumentError, "You must provide an element of the #{enum_class} Enumeration. You provided an element of the class #{element.class}."
|
38
|
+
end
|
39
|
+
|
40
|
+
2**element.bitfield_index & self[set_column] == 2**element.bitfield_index ? next : self[set_column] += 2**element.bitfield_index
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
define_method(set_name) do
|
46
|
+
if self[set_column] == 0
|
47
|
+
return []
|
48
|
+
else
|
49
|
+
enum_class.values.inject([]) do |set_elements, enum_element|
|
50
|
+
set_elements << enum_element if send("#{set_name.to_s.singularize}_#{enum_element.name.underscore}?")
|
51
|
+
set_elements
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
enum_class.values.each do |enum|
|
57
|
+
define_method("#{set_name.to_s.singularize}_#{enum.name.underscore}?") do
|
58
|
+
2**enum.bitfield_index & self[set_column] == 2**enum.bitfield_index ? true : false
|
59
|
+
end
|
60
|
+
|
61
|
+
define_method("#{set_name.to_s.singularize}_#{enum.name.underscore}=") do |true_or_false|
|
62
|
+
current_value = (2**enum.bitfield_index & self[set_column] == 2**enum.bitfield_index)
|
63
|
+
true_or_false = true if true_or_false.to_s == "true" || (true_or_false.respond_to?(:to_i) && true_or_false.to_i == 1)
|
64
|
+
true_or_false = false if true_or_false.to_s == "false" || (true_or_false.respond_to?(:to_i) && true_or_false.to_i == 0)
|
65
|
+
|
66
|
+
if current_value != true_or_false
|
67
|
+
true_or_false ? self[set_column] += 2**enum.bitfield_index : self[set_column] -= 2**enum.bitfield_index
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def self.included(receiver)
|
76
|
+
receiver.extend ClassMethods
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
ActiveRecord::Base.send :include, HasSet
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/has_set.rb'}"
|
9
|
+
puts "Loading has_set gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper.rb'
|
2
|
+
|
3
|
+
class HasSetTest < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
setup_db
|
7
|
+
end
|
8
|
+
|
9
|
+
def teardown
|
10
|
+
teardown_db
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_should_have_has_set_class_method
|
14
|
+
assert_respond_to Person, :has_set
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_should_provide_a_way_of_storing_enums_in_a_set_on_an_ar_model
|
18
|
+
person = Person.new(:fullname => "Jessie Summers", :interests => [Interests::Dating, Interests::Shopping])
|
19
|
+
assert person.save, "Person should save!"
|
20
|
+
person.reload
|
21
|
+
assert person.interest_shopping?, "Person should be interested in shopping."
|
22
|
+
assert person.interest_dating?, "Person should be interested in dating."
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_should_provide_a_way_of_storing_a_single_enum_value_in_a_set_on_an_ar_model
|
26
|
+
person = Person.new(:fullname => "Jessie Summers", :interests => Interests::Dating)
|
27
|
+
assert person.save, "Person should save!"
|
28
|
+
person.reload
|
29
|
+
assert person.interest_dating?, "Person should be interested in dating."
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_should_throw_argument_error_if_there_is_no_enumeratable_class_to_take_the_set_entries_from
|
33
|
+
Person.expects(:columns_hash).returns({"hobbies_bitfield" => stub(:type => :integer)})
|
34
|
+
assert_raise(NameError) { Person.has_set :hobbies }
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_should_set_a_single_set_element
|
38
|
+
person = Person.new
|
39
|
+
person.interest_golf = true
|
40
|
+
assert person.save, "Person should save!"
|
41
|
+
person.reload
|
42
|
+
assert person.interest_golf?, "Person should be interested in golf."
|
43
|
+
person.interest_golf = nil
|
44
|
+
assert !person.interest_golf?, "Person should not be interested in golf."
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_should_reset_all_set_entries
|
48
|
+
person = Person.new(:fullname => "Jessie Summers", :interests => [Interests::Dating, Interests::Shopping])
|
49
|
+
assert person.interest_shopping?, "Person should be interested in shopping."
|
50
|
+
assert person.interest_dating?, "Person should be interested in dating."
|
51
|
+
person.interests = nil
|
52
|
+
assert !person.interest_shopping?, "Person should not be interested in shopping."
|
53
|
+
assert !person.interest_dating?, "Person should not be interested in dating."
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_should_raise_active_record_error_if_the_type_of_the_bitfiel_column_is_not_integer
|
57
|
+
assert_raise(ActiveRecord::ActiveRecordError) { Punk.has_set :bad_habits }
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_should_get_all_set_elements_of_a_certain_object
|
61
|
+
person = Person.new(:fullname => "Jessie Summers", :interests => [Interests::Dating, Interests::Shopping])
|
62
|
+
assert_equal [Interests::Dating, Interests::Shopping], person.interests
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_should_provide_the_name_of_the_bitfield_column
|
66
|
+
party = Party.new(:location => "Beach House", :drinks => [Drinks::Beer, Drinks::CubaLibre])
|
67
|
+
assert party.save, "Party should save!"
|
68
|
+
party.reload
|
69
|
+
assert party.drink_beer?, "Party should offer beer."
|
70
|
+
assert party.drink_cuba_libre?, "Party should offer cuba libre."
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_should_provide_the_name_of_the_enum_class
|
74
|
+
Party.has_set :music, :enum_class => MusicStyles
|
75
|
+
|
76
|
+
party = Party.new(:location => "Penthouse", :music => [MusicStyles::Rock])
|
77
|
+
assert party.save, "Party should save!"
|
78
|
+
party.reload
|
79
|
+
assert party.music_rock?, "Party should have Rock music."
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_should_validate_that_only_elements_from_the_given_enum_are_used_in_the_set
|
83
|
+
person = Person.new(:fullname => "Jessie Summers", :interests => Interests::Dating)
|
84
|
+
assert_raise(ArgumentError) { person.interests = Drinks::Beer }
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'renum'
|
3
|
+
require 'test/unit'
|
4
|
+
require 'mocha'
|
5
|
+
|
6
|
+
require File.dirname(__FILE__) + '/../lib/has_set'
|
7
|
+
|
8
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
9
|
+
ActiveRecord::Migration.verbose = false
|
10
|
+
|
11
|
+
def setup_db
|
12
|
+
ActiveRecord::Base.silence do
|
13
|
+
ActiveRecord::Schema.define(:version => 1) do
|
14
|
+
create_table :people do |t|
|
15
|
+
t.string :fullname
|
16
|
+
t.integer :interests_bitfield, :default => 0
|
17
|
+
end
|
18
|
+
|
19
|
+
create_table :punks do |t|
|
20
|
+
t.string :fullname
|
21
|
+
t.string :bad_habits_bitfield
|
22
|
+
end
|
23
|
+
|
24
|
+
create_table :parties do |t|
|
25
|
+
t.string :location
|
26
|
+
t.integer :drinks_set, :default => 0
|
27
|
+
t.integer :music_bitfield, :default => 0
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def teardown_db
|
34
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
35
|
+
ActiveRecord::Base.connection.drop_table(table)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
enum :Interests do
|
40
|
+
attr_reader :bitfield_index
|
41
|
+
|
42
|
+
Art(0)
|
43
|
+
Golf(1)
|
44
|
+
Sleeping(2)
|
45
|
+
Drinking(3)
|
46
|
+
Dating(4)
|
47
|
+
Shopping(5)
|
48
|
+
|
49
|
+
def init(bitfield_index)
|
50
|
+
@bitfield_index = bitfield_index
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
enum :MusicStyles do
|
55
|
+
attr_reader :bitfield_index
|
56
|
+
|
57
|
+
Rock(0)
|
58
|
+
Pop(1)
|
59
|
+
RnB(2)
|
60
|
+
|
61
|
+
def init(bitfield_index)
|
62
|
+
@bitfield_index = bitfield_index
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
enum :Drinks do
|
67
|
+
attr_reader :bitfield_index
|
68
|
+
|
69
|
+
Beer(0)
|
70
|
+
Wine(1)
|
71
|
+
CubaLibre(2)
|
72
|
+
|
73
|
+
def init(bitfield_index)
|
74
|
+
@bitfield_index = bitfield_index
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
setup_db # Init the database for class creation
|
79
|
+
|
80
|
+
class Person < ActiveRecord::Base
|
81
|
+
has_set :interests
|
82
|
+
end
|
83
|
+
|
84
|
+
class Punk < ActiveRecord::Base; end
|
85
|
+
|
86
|
+
class Party < ActiveRecord::Base
|
87
|
+
has_set :drinks, :column_name => :drinks_set
|
88
|
+
end
|
89
|
+
|
90
|
+
teardown_db # And drop them right afterwards
|
91
|
+
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pkwde-has_set
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- pkw.de Dev Team
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-04-16 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: activerecord
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - "="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.2.2
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activesupport
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.2.2
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: newgem
|
37
|
+
type: :development
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.3.0
|
44
|
+
version:
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: mocha
|
47
|
+
type: :development
|
48
|
+
version_requirement:
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pkwde-renum
|
57
|
+
type: :development
|
58
|
+
version_requirement:
|
59
|
+
version_requirements: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: sqlite3-ruby
|
67
|
+
type: :development
|
68
|
+
version_requirement:
|
69
|
+
version_requirements: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: "0"
|
74
|
+
version:
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: hoe
|
77
|
+
type: :development
|
78
|
+
version_requirement:
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 1.8.0
|
84
|
+
version:
|
85
|
+
description: "A simple Gem to enable any `ActiveRecord::Base` object to store a set of attributes in a set like structure represented through a bitfield on the database level. You only have to specify the name of the set to hold the attributes in question an the rest is done for you through some fine selected Ruby magic. Here is a simple example of how you could use the gem: class Person < ActiveRecord::Base has_set :interests end To get this to work you need some additional work done first: 1. You need an unsigned 8-Byte integer column in your database to store the bitfield. It is expected that the column is named after the name of the set with the suffix `_bitfield` appended (e.g. `interests_bitfield`). You can change that default behavior by providing the option `:column_name` (e.g. `has_set :interests, :column_name => :my_custom_column`). 2. You need a class that provides the valid values to be stored within the set and map the single bits back to something meaningful. The class should be named after the name of the set (you can change this through the `:enum_class` option). This class could be seen as an enumeration and must implement the following simple interface: * There must be a class method `values` to return all valid enumerators in the defined enumeration. * Each enumerator must implement a `name` method to return a literal representation for identification. The literal must be of the type `String`. * Each enumerator must implement a `bitfield_index` method to return the exponent of the number 2 for calculation the position of this enumerator in the bitfield. **Attention** Changing this index afterwards will destroy your data integrity. Here is a simple example of how to implement such a enumeration type while using the the `renum` gem for simplicity. You are free to use anything else that matches the described interface. enum :Interests do attr_reader :bitfield_index Art(0) Golf(1) Sleeping(2) Drinking(3) Dating(4) Shopping(5) def init(bitfield_index) @bitfield_index = bitfield_index end end"
|
86
|
+
email:
|
87
|
+
- dev@pkw.de
|
88
|
+
executables: []
|
89
|
+
|
90
|
+
extensions: []
|
91
|
+
|
92
|
+
extra_rdoc_files:
|
93
|
+
- History.txt
|
94
|
+
- Manifest.txt
|
95
|
+
- README.txt
|
96
|
+
files:
|
97
|
+
- History.txt
|
98
|
+
- Manifest.txt
|
99
|
+
- README.txt
|
100
|
+
- Rakefile
|
101
|
+
- has_set.gemspec
|
102
|
+
- lib/has_set.rb
|
103
|
+
- script/console
|
104
|
+
- script/destroy
|
105
|
+
- script/generate
|
106
|
+
- test/has_set_test.rb
|
107
|
+
- test/test_helper.rb
|
108
|
+
has_rdoc: true
|
109
|
+
homepage: http://github.com/pkwde/has_set
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options:
|
112
|
+
- --main
|
113
|
+
- README.txt
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: "0"
|
121
|
+
version:
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: "0"
|
127
|
+
version:
|
128
|
+
requirements: []
|
129
|
+
|
130
|
+
rubyforge_project: has_set
|
131
|
+
rubygems_version: 1.2.0
|
132
|
+
signing_key:
|
133
|
+
specification_version: 2
|
134
|
+
summary: A Gem that enables ActiveRecord models to have a set of values defined in a certain class and stored in an integer bitfield on the database level.
|
135
|
+
test_files:
|
136
|
+
- test/test_helper.rb
|