haz_enum 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
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
22
+ .rake_tasks
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Galaxy Cats IT Consulting GmbH
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,17 @@
1
+ = haz_enum
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Galaxy Cats IT Consulting GmbH. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "haz_enum"
8
+ gem.summary = %Q{has_set and has_enum for ActiveRecord}
9
+ gem.description = %Q{use has_set and has_enum in your ActiveRecord models if you want to have one (has_enum) value from a defined enumeration or more (has_set))}
10
+ gem.email = "andi@galaxycats.com"
11
+ gem.homepage = "http://github.com/galaxycats/haz_enum"
12
+ gem.authors = ["thyphoon"]
13
+ gem.add_dependency "activerecord", ">= 3.0.0.beta3"
14
+ gem.add_development_dependency "rspec", ">= 1.2.9"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "haz_enum #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
data/haz_enum.gemspec ADDED
@@ -0,0 +1,62 @@
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{haz_enum}
8
+ s.version = "0.2.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["thyphoon"]
12
+ s.date = %q{2010-06-15}
13
+ s.description = %q{use has_set and has_enum in your ActiveRecord models if you want to have one (has_enum) value from a defined enumeration or more (has_set))}
14
+ s.email = %q{andi@galaxycats.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "haz_enum.gemspec",
27
+ "lib/haz_enum.rb",
28
+ "lib/haz_enum/enum.rb",
29
+ "lib/haz_enum/set.rb",
30
+ "spec/enum_spec.rb",
31
+ "spec/set_spec.rb",
32
+ "spec/spec.opts",
33
+ "spec/spec_helper.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/galaxycats/haz_enum}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.6}
39
+ s.summary = %q{has_set and has_enum for ActiveRecord}
40
+ s.test_files = [
41
+ "spec/enum_spec.rb",
42
+ "spec/set_spec.rb",
43
+ "spec/spec_helper.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
51
+ s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0.beta3"])
52
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
53
+ else
54
+ s.add_dependency(%q<activerecord>, [">= 3.0.0.beta3"])
55
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<activerecord>, [">= 3.0.0.beta3"])
59
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
60
+ end
61
+ end
62
+
@@ -0,0 +1,38 @@
1
+ module HazEnum
2
+ module Enum
3
+ def has_enum(enum_name, options={})
4
+ enum_name = enum_name.to_s
5
+ enum_column = options.has_key?(:column_name) ? options[:column_name].to_s : enum_name
6
+ enum_class = options.has_key?(:class_name) ? Object.const_get(options[:class_name].to_s.classify) : Object.const_get(enum_name.pluralize.camelize)
7
+
8
+ self.attr_protected("#{enum_column}") if enum_name != enum_column
9
+ self.validate "#{enum_column}_check_for_valid_enum_value"
10
+
11
+ define_method "#{enum_name}" do
12
+ begin
13
+ return enum_class.const_get(self["#{enum_column}"]) if self["#{enum_column}"]
14
+ return nil
15
+ rescue NameError => e
16
+ return nil
17
+ end
18
+ end
19
+
20
+ define_method "#{enum_name}=" do |enum_to_set|
21
+ self["#{enum_column}"] = enum_to_set.name
22
+ end
23
+
24
+ define_method "#{enum_name}_changed?" do
25
+ send("#{enum_column}_changed?")
26
+ end if enum_name != enum_column
27
+
28
+ define_method "#{enum_column}_check_for_valid_enum_value" do
29
+ return true if self["#{enum_column}"].nil?
30
+ begin
31
+ enum_class.const_get(self["#{enum_column}"])
32
+ rescue NameError => e
33
+ self.errors.add("#{enum_name}".to_sym, I18n.t("activerecord.errors.messages.enum_value_invalid", :value => self["#{enum_column}"], :name => enum_name))
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,59 @@
1
+ module HazEnum
2
+ module Set
3
+ def has_set(set_name, options={})
4
+
5
+ field_type = options.has_key?(:field_type) ? options[:field_type].to_s : "bitfield"
6
+ set_column = options.has_key?(:column_name) ? options[:column_name].to_s : "#{set_name}_#{field_type}"
7
+ enum_class = options.has_key?(:class_name) ? Object.const_get(options[:class_name].to_s.camelize) : Object.const_get(set_name.to_s.camelize)
8
+
9
+ after_find "initialize_#{set_name}_from_bitfield"
10
+ before_save "convert_#{set_name}_to_bitfield"
11
+
12
+ enum_class.class_eval do
13
+ def bitfield_index
14
+ index + 1
15
+ end
16
+ end unless enum_class.method_defined?(:bitfield_index)
17
+
18
+ define_method("#{set_name}") do
19
+ instance_variable_get("@#{set_name}")
20
+ end
21
+
22
+ define_method("#{set_name}=") do |value|
23
+ class <<value
24
+ yield if block_given?
25
+ end
26
+ instance_variable_set("@#{set_name}", value)
27
+ end
28
+
29
+ define_method("has_#{set_name.to_s.singularize}?") do |set_value|
30
+ send(set_name).include?(set_value)
31
+ end
32
+
33
+ define_method("#{set_name}_changed?") do
34
+ send("convert_#{set_name}_to_bitfield")
35
+ send("#{set_column}_changed?")
36
+ end
37
+
38
+ define_method("convert_#{set_name}_to_bitfield") do
39
+ self[set_column] = 0
40
+ if send(set_name)
41
+ send(set_name).each do |element|
42
+ 2**element.bitfield_index & self[set_column] == 2**element.bitfield_index ? next : self[set_column] += 2**element.bitfield_index
43
+ end
44
+ end
45
+ end
46
+
47
+ define_method("initialize_#{set_name}_from_bitfield") do
48
+ if self[set_column]
49
+ set_elements = enum_class.values.collect do |enum_element|
50
+ enum_element if ((2**enum_element.bitfield_index & self[set_column]) == 2**enum_element.bitfield_index)
51
+ end.compact
52
+ send("#{set_name}=", set_elements)
53
+ else
54
+ self[set_column] = 0
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
data/lib/haz_enum.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "haz_enum/enum"
2
+ require "haz_enum/set"
3
+
4
+ module HazEnum
5
+ end
6
+
7
+ ActiveRecord::Base.extend HazEnum::Enum
8
+ ActiveRecord::Base.extend HazEnum::Set
data/spec/enum_spec.rb ADDED
@@ -0,0 +1,63 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "HazEnum" do
4
+
5
+ describe "Enum" do
6
+
7
+ before(:all) do
8
+ setup_db
9
+ end
10
+
11
+ it "should have class method has_enum" do
12
+ ClassWithEnum.should respond_to(:has_enum)
13
+ end
14
+
15
+ it "should be able to set enum-attribute via hash in initializer" do
16
+ ClassWithEnum.new(:product => Products::Silver).product.should be(Products::Silver)
17
+ ClassWithCustomNameEnum.new(:product => Products::Silver).product.should be(Products::Silver)
18
+ end
19
+
20
+ it "should not be able to set enum-attribute by colum-name via hash in initializer" do
21
+ ClassWithCustomNameEnum.new(:custom_name => Products::Silver).product.should_not be(Products::Silver)
22
+ ClassWithCustomNameEnum.new(:custom_name => Products::Silver.name).product.should_not be(Products::Silver)
23
+ end
24
+
25
+ it "should have a enum_value attribute" do
26
+ ClassWithCustomNameEnum.new(:product => Products::Gold).custom_name.should be(Products::Gold.name)
27
+ end
28
+
29
+ it "colum_name should be configurable" do
30
+ ClassWithCustomNameEnum.new(:product => Products::Gold).custom_name.should be(Products::Gold.name)
31
+ end
32
+
33
+ it "should know if enum-attribute has changed" do
34
+ product_enum = ClassWithEnum.new(:product => Products::Silver)
35
+ product_enum.product_changed?.should be(true)
36
+ product_enum.save
37
+ product_enum.product_changed?.should be(false)
38
+ product_enum.product = Products::Gold
39
+ product_enum.product_changed?.should be(true)
40
+ end
41
+
42
+ it "should know if enum-attribute has not really changed" do
43
+ product_enum = ClassWithEnum.new(:product => Products::Silver)
44
+ product_enum.save
45
+ product_enum.product = Products::Silver
46
+ product_enum.product_changed?.should be(false)
47
+ end
48
+
49
+ it "should validate enum-value" do
50
+ enum_mixin = ClassWithEnum.new
51
+ platin = "Platin"
52
+ class <<platin; def name;"Platin";end;end
53
+ enum_mixin.product = platin
54
+ enum_mixin.valid?.should_not be(true)
55
+ enum_mixin.errors[:product].size.should > 0
56
+ end
57
+
58
+ after(:all) do
59
+ teardown_db
60
+ end
61
+
62
+ end
63
+ end
data/spec/set_spec.rb ADDED
@@ -0,0 +1,64 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "HazEnum" do
4
+
5
+ describe "Set" do
6
+
7
+ before(:all) do
8
+ setup_db
9
+ end
10
+
11
+ it "should have class method has_enum" do
12
+ ClassWithSet.should respond_to(:has_set)
13
+ end
14
+
15
+ it "should be able to set set-values via hash in initializer" do
16
+ ClassWithSet.new(:roles => [Roles::User, Roles::Admin]).roles[0].should be(Roles::User)
17
+ ClassWithSet.new(:roles => [Roles::User, Roles::Admin]).roles[1].should be(Roles::Admin)
18
+ end
19
+
20
+ it "should not be able to set set-values by colum-name via hash in initializer" do
21
+ lambda { ClassWithEnum.new(:roles_bitfield => 3) }.should raise_error
22
+ end
23
+
24
+ it "colum_name should be configurable" do
25
+ ClassWithCustomNameSet.new(:roles => [Roles::Admin]).roles.first.should be(Roles::Admin)
26
+ end
27
+
28
+ it "should know if set-attribute has changed" do
29
+ roles_set = ClassWithSet.new(:roles => [Roles::Admin])
30
+ roles_set.roles_changed?.should be(true)
31
+ roles_set.save
32
+ roles_set.roles_changed?.should be(false)
33
+ roles_set.roles = [Roles::Supervisor]
34
+ roles_set.roles_changed?.should be(true)
35
+ end
36
+
37
+ it "should know if enum-attribute has not really changed" do
38
+ roles_set = ClassWithSet.new(:roles => [Roles::Admin])
39
+ roles_set.save
40
+ roles_set.roles = [Roles::Admin]
41
+ roles_set.roles_changed?.should be(false)
42
+ end
43
+
44
+ it "should be able to add set-value" do
45
+ roles_set = ClassWithSet.new(:roles => [Roles::Admin])
46
+ roles_set.save
47
+ roles_set.roles << Roles::Supervisor
48
+ roles_set.save
49
+ roles_set.reload
50
+ roles_set.has_role?(Roles::Admin).should == true
51
+ roles_set.has_role?(Roles::Supervisor).should == true
52
+ end
53
+
54
+ it "should be able to define association methods" do
55
+ extended = ClassWithSet.new(:extended_roles => [Roles::Admin, Roles::Supervisor, Roles::User])
56
+ extended.to_s.should == "Admin, Supervisor, User"
57
+ end
58
+
59
+ after(:all) do
60
+ teardown_db
61
+ end
62
+
63
+ end
64
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,86 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require "rubygems"
4
+ require "active_record"
5
+ require 'renum'
6
+ require 'haz_enum'
7
+ require 'spec'
8
+ require 'spec/autorun'
9
+
10
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
11
+ ActiveRecord::Migration.verbose = false
12
+
13
+ def setup_db
14
+ ActiveRecord::Base.silence do
15
+ ActiveRecord::Schema.define(:version => 1) do
16
+ create_table :class_with_enums do |t|
17
+ t.column :title, :string
18
+ t.column :product, :string
19
+ t.column :created_at, :datetime
20
+ t.column :updated_at, :datetime
21
+ end
22
+
23
+ create_table :class_with_custom_name_enums do |t|
24
+ t.column :title, :string
25
+ t.column :custom_name, :string
26
+ t.column :created_at, :datetime
27
+ t.column :updated_at, :datetime
28
+ end
29
+
30
+ create_table :class_with_sets do |t|
31
+ t.column :title, :string
32
+ t.column :roles_bitfield, :integer
33
+ t.column :created_at, :datetime
34
+ t.column :updated_at, :datetime
35
+ end
36
+
37
+ create_table :class_with_custom_name_sets do |t|
38
+ t.column :title, :string
39
+ t.column :custom_name, :string
40
+ t.column :created_at, :datetime
41
+ t.column :updated_at, :datetime
42
+ end
43
+
44
+ end
45
+ end
46
+ end
47
+
48
+ def teardown_db
49
+ ActiveRecord::Base.connection.tables.each do |table|
50
+ ActiveRecord::Base.connection.drop_table(table)
51
+ end
52
+ end
53
+
54
+ enum :Fakes, [:NOT_DEFINIED]
55
+ enum :Products, [:Silver, :Gold, :Titanium]
56
+
57
+ enum :Roles, [:Admin, :Supervisor, :User]
58
+
59
+ setup_db # Init the database for class creation
60
+
61
+ class ClassWithEnum < ActiveRecord::Base
62
+ has_enum :product
63
+ end
64
+
65
+ class ClassWithCustomNameEnum < ActiveRecord::Base
66
+ has_enum :product, :column_name => :custom_name
67
+ end
68
+
69
+ class ClassWithSet < ActiveRecord::Base
70
+ has_set :roles
71
+ has_set :extended_roles, :class_name => :roles do
72
+ def to_s
73
+ extended_roles.collect(&:name).join(", ")
74
+ end
75
+ end
76
+ end
77
+
78
+ class ClassWithCustomNameSet < ActiveRecord::Base
79
+ has_set :roles, :column_name => :custom_name
80
+ end
81
+
82
+ teardown_db # And drop them right afterwards
83
+
84
+ Spec::Runner.configure do |config|
85
+
86
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: haz_enum
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 2
8
+ - 0
9
+ version: 0.2.0
10
+ platform: ruby
11
+ authors:
12
+ - thyphoon
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-06-15 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: activerecord
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 3
29
+ - 0
30
+ - 0
31
+ - beta3
32
+ version: 3.0.0.beta3
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rspec
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ segments:
43
+ - 1
44
+ - 2
45
+ - 9
46
+ version: 1.2.9
47
+ type: :development
48
+ version_requirements: *id002
49
+ description: use has_set and has_enum in your ActiveRecord models if you want to have one (has_enum) value from a defined enumeration or more (has_set))
50
+ email: andi@galaxycats.com
51
+ executables: []
52
+
53
+ extensions: []
54
+
55
+ extra_rdoc_files:
56
+ - LICENSE
57
+ - README.rdoc
58
+ files:
59
+ - .document
60
+ - .gitignore
61
+ - LICENSE
62
+ - README.rdoc
63
+ - Rakefile
64
+ - VERSION
65
+ - haz_enum.gemspec
66
+ - lib/haz_enum.rb
67
+ - lib/haz_enum/enum.rb
68
+ - lib/haz_enum/set.rb
69
+ - spec/enum_spec.rb
70
+ - spec/set_spec.rb
71
+ - spec/spec.opts
72
+ - spec/spec_helper.rb
73
+ has_rdoc: true
74
+ homepage: http://github.com/galaxycats/haz_enum
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options:
79
+ - --charset=UTF-8
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ segments:
94
+ - 0
95
+ version: "0"
96
+ requirements: []
97
+
98
+ rubyforge_project:
99
+ rubygems_version: 1.3.6
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: has_set and has_enum for ActiveRecord
103
+ test_files:
104
+ - spec/enum_spec.rb
105
+ - spec/set_spec.rb
106
+ - spec/spec_helper.rb