belongs_to_enum 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,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.tmproj
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format doc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in belongs_to_enum.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,2 @@
1
+ Adds support for transparent enums to ActiveRecord.
2
+ Adds a belongs_to_enum to ActiveRecord that takes an array of possible values.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "belongs_to_enum/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "belongs_to_enum"
7
+ s.version = BelongsToEnum::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jeremy walker"]
10
+ s.email = ["jez.walker@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Adds support for transparent enums to ActiveRecord}
13
+ s.description = %q{Adds a belongs_to_enum to ActiveRecord that takes an array of possible values.}
14
+
15
+ s.rubyforge_project = "belongs_to_enum"
16
+
17
+ s.add_dependency "rails"
18
+ s.add_development_dependency "rspec-rails"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+ end
@@ -0,0 +1,78 @@
1
+ module BelongsToEnum
2
+ class Enum
3
+ def Enum.add_item(key,value)
4
+ @hash ||= {}
5
+ @hash[key]=value
6
+ end
7
+
8
+ def Enum.method_missing(key, *args)
9
+ @hash[key] || super
10
+ end
11
+
12
+ def Enum.respond_to?(key)
13
+ return true if super(key)
14
+ return @hash && @hash[key]
15
+ end
16
+
17
+ def Enum.each
18
+ @hash.each {|key,value| yield(key,value)}
19
+ end
20
+
21
+ def Enum.map
22
+ @hash.map {|key,value| yield(key,value)}
23
+ end
24
+
25
+ def Enum.keys
26
+ @hash.values
27
+ end
28
+
29
+ def Enum.pretty_items
30
+ @hash.sort{|a,b| a[1]<=>b[1]}.map{|i|[i[0].to_s.gsub("_", " ").capitalize,i[1]]}
31
+ end
32
+
33
+ def Enum.items
34
+ warn "DEPRECIATED belongs_to_enum: Use pretty items instead"
35
+ pretty_items
36
+ end
37
+
38
+ def Enum.get(value)
39
+ @hash.key(value)
40
+ end
41
+
42
+ def Enum.display(value)
43
+ get(value).to_s.gsub("_", " ").titleize
44
+ end
45
+
46
+ def Enum.raw_hash
47
+ @hash
48
+ end
49
+
50
+ def self.create(name, parent, keys)
51
+
52
+ # Check to see if this has already been defined...
53
+ return if parent.const_defined?(name.to_s.camelize)
54
+
55
+ klass = Class.new(Enum)
56
+ parent.const_set(name.to_s.camelize, klass)
57
+
58
+ if keys.respond_to? :raw_hash
59
+ keys.raw_hash.each do |k,v|
60
+ klass.add_item(k,v)
61
+ end
62
+
63
+ elsif(keys.is_a? Hash)
64
+ keys.each do |k,v|
65
+ klass.add_item(k,v)
66
+ end
67
+ #elsif(keys.respond_to? :raw_hash)
68
+ # keys.send(:raw_hash).each do |k,v|
69
+ # klass.add_item(k,v)
70
+ # end
71
+ else
72
+ keys.each_with_index do |key, index|
73
+ klass.add_item(key, index + 1)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,19 @@
1
+ require File.join(File.dirname(__FILE__), "enum")
2
+
3
+ module BelongsToEnum
4
+ module Hook
5
+ def belongs_to_enum(name, keys)
6
+ BelongsToEnum::Enum.create(name, self, keys)
7
+ name = name.to_s
8
+ class_eval <<-EOS
9
+ def #{name}?
10
+ #{name}_id.to_i > 0
11
+ end
12
+
13
+ def #{name}
14
+ #{name.camelize}.display(#{name}_id)
15
+ end
16
+ EOS
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ require 'rails'
2
+ require 'belongs_to_enum'
3
+
4
+ module BelongsToEnum
5
+ class Railtie < Rails::Railtie
6
+ railtie_name :belongs_to_enum
7
+
8
+ config.to_prepare do
9
+ ActiveRecord::Base.send(:extend, BelongsToEnum::Hook)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module BelongsToEnum
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'active_support/core_ext'
2
+ require File.join(File.dirname(__FILE__), "belongs_to_enum/hook")
3
+ require File.join(File.dirname(__FILE__), "belongs_to_enum/railtie")
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Belongs To Enum models" do
4
+
5
+ it "constants should be set" do
6
+ User.const_defined?("Role").should be_true
7
+ end
8
+
9
+ it "constant has keys" do
10
+ User::Role.should respond_to(:admin)
11
+ end
12
+
13
+ it "keys should start at 1" do
14
+ User::Role.normal.should == 1
15
+ end
16
+
17
+ it "keys should respect hash values" do
18
+ Vehicle::Make.honda.should == 10
19
+ end
20
+
21
+ it "keys can be iterated through" do
22
+ User::Role.each do |role, role_id|
23
+ role.should == :normal
24
+ role_id.should == 1
25
+ break
26
+ end
27
+ end
28
+
29
+ it "has pretty items" do
30
+ User::Role.pretty_items.first[0].should == "Normal"
31
+ User::Role.pretty_items.first[1].should == 1
32
+ end
33
+
34
+ it "has depreciated items method that maps to pretty_items" do
35
+ User::Role.items.first[0].should == "Normal"
36
+ User::Role.items.first[1].should == 1
37
+ end
38
+
39
+ it "can get item from id" do
40
+ User::Role.get(1).should == :normal
41
+ end
42
+
43
+ it "pretty displays item" do
44
+ User::Role.display(1).should == "Normal"
45
+ end
46
+
47
+ it "can access raw hash" do
48
+ User::Role.raw_hash.is_a?(Hash)
49
+ User::Role.raw_hash.keys[0].should == :normal
50
+ User::Role.raw_hash.values[0].should == 1
51
+ end
52
+
53
+
54
+ end
@@ -0,0 +1,25 @@
1
+ ENV["RAILS_ENV"] ||= 'test'
2
+
3
+ require 'rails'
4
+ require 'rspec-rails'
5
+ require 'active_record'
6
+
7
+ $:.unshift File.dirname(__FILE__) + '/../lib'
8
+
9
+ # Thie first line isn't working so I have added the second...
10
+ require File.dirname(__FILE__) + '/../lib/belongs_to_enum'
11
+ ActiveRecord::Base.send(:extend, BelongsToEnum::Hook)
12
+
13
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
14
+
15
+ # AR keeps printing annoying schema statements
16
+ $stdout = StringIO.new
17
+
18
+ ActiveRecord::Base.logger
19
+ ActiveRecord::Schema.define(:version => 1) do
20
+ create_table :users do |t|
21
+ t.integer :role_id, :null => false
22
+ end
23
+ end
24
+
25
+ require 'test_classes'
@@ -0,0 +1,7 @@
1
+ class User < ActiveRecord::Base
2
+ belongs_to_enum :role, [:normal, :admin, :superadmin, :suspended]
3
+ end
4
+
5
+ class Vehicle < ActiveRecord::Base
6
+ belongs_to_enum :make, {:honda => 10, :toyota => 20, :ford => 30}
7
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: belongs_to_enum
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: "0.1"
6
+ platform: ruby
7
+ authors:
8
+ - Jeremy walker
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-17 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-rails
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
38
+ description: Adds a belongs_to_enum to ActiveRecord that takes an array of possible values.
39
+ email:
40
+ - jez.walker@gmail.com
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - .gitignore
49
+ - .rspec
50
+ - Gemfile
51
+ - README
52
+ - Rakefile
53
+ - belongs_to_enum.gemspec
54
+ - lib/belongs_to_enum.rb
55
+ - lib/belongs_to_enum/enum.rb
56
+ - lib/belongs_to_enum/hook.rb
57
+ - lib/belongs_to_enum/railtie.rb
58
+ - lib/belongs_to_enum/version.rb
59
+ - spec/belongs_to_enum.rb
60
+ - spec/spec_helper.rb
61
+ - spec/test_classes.rb
62
+ has_rdoc: true
63
+ homepage: ""
64
+ licenses: []
65
+
66
+ post_install_message:
67
+ rdoc_options: []
68
+
69
+ require_paths:
70
+ - lib
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project: belongs_to_enum
86
+ rubygems_version: 1.6.2
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Adds support for transparent enums to ActiveRecord
90
+ test_files:
91
+ - spec/belongs_to_enum.rb
92
+ - spec/spec_helper.rb
93
+ - spec/test_classes.rb