negonicrac-magic_enums 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/Manifest +7 -0
- data/README.textile +44 -0
- data/Rakefile +14 -0
- data/init.rb +1 -0
- data/lib/magic_enums.rb +49 -0
- data/magic_enums.gemspec +32 -0
- data/test/magic_enums_test.rb +27 -0
- data/test/test_helper.rb +20 -0
- metadata +68 -0
data/MIT-LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2008 [name of plugin creator]
|
|
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/Manifest
ADDED
data/README.textile
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
h1. MagicEnums
|
|
2
|
+
|
|
3
|
+
h2. Introduction
|
|
4
|
+
|
|
5
|
+
*RAILS 2.1/EDGE only!*
|
|
6
|
+
|
|
7
|
+
A nice way to have enums in Rails.
|
|
8
|
+
|
|
9
|
+
Enumerations are easy as integers, however I want to take a text approach to it as soon as rearranging integers can get fussy.
|
|
10
|
+
|
|
11
|
+
h2. Usage
|
|
12
|
+
|
|
13
|
+
h3. In your models
|
|
14
|
+
|
|
15
|
+
<pre>
|
|
16
|
+
<code>
|
|
17
|
+
enum_column :status, %w(draft private published)
|
|
18
|
+
</code>
|
|
19
|
+
</pre>
|
|
20
|
+
_or_
|
|
21
|
+
<pre>
|
|
22
|
+
<code>
|
|
23
|
+
enum_column :gender, ['male', 'female']
|
|
24
|
+
</code>
|
|
25
|
+
</pre>
|
|
26
|
+
|
|
27
|
+
h3. In your controller
|
|
28
|
+
|
|
29
|
+
In your controller, you can check the status <code>post_record.status_is_published?</code> or simply by calling <code>person_record.gender</code> to find it out in words, as you would normally.
|
|
30
|
+
|
|
31
|
+
You can also do find calls like so:
|
|
32
|
+
<pre>
|
|
33
|
+
<code>
|
|
34
|
+
Post.status_is_published?
|
|
35
|
+
</code>
|
|
36
|
+
</pre>
|
|
37
|
+
_or_
|
|
38
|
+
<pre>
|
|
39
|
+
<code>
|
|
40
|
+
Person.gender_is_male?
|
|
41
|
+
</code>
|
|
42
|
+
</pre>
|
|
43
|
+
|
|
44
|
+
Copyright (c) 2008 "Zach Inglis":http://zachinglis.com, released under the MIT license
|
data/Rakefile
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require 'rubygems'
|
|
2
|
+
require 'rake'
|
|
3
|
+
require 'echoe'
|
|
4
|
+
|
|
5
|
+
Echoe.new('magic_enums', '0.0.1') do |p|
|
|
6
|
+
p.description = "A nice way to have enums in Rails."
|
|
7
|
+
p.url = "http://github.com/zachinglis/magic_enums/tree/master"
|
|
8
|
+
p.author = "Zach Inglis"
|
|
9
|
+
p.email = "zach@lt3media.com"
|
|
10
|
+
p.ignore_pattern = ["tmp/*","script/*"]
|
|
11
|
+
p.development_dependencies = []
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
data/init.rb
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
require 'magic_enums'
|
data/lib/magic_enums.rb
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
module MagicEnums
|
|
2
|
+
def self.included(base)
|
|
3
|
+
base.extend(ClassMethods)
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
module ClassMethods
|
|
7
|
+
def enum_column(column_name, choices=[], options={})
|
|
8
|
+
configuration = { :message => "is not a valid choice" }
|
|
9
|
+
configuration.merge!(options)
|
|
10
|
+
|
|
11
|
+
array_in_text = "[#{choices.collect { |choice| "'#{choice}'" }.join(", ")}]" # Make the array into text form.
|
|
12
|
+
|
|
13
|
+
class_eval "
|
|
14
|
+
class_inheritable_accessor :#{column_name}_choices
|
|
15
|
+
write_inheritable_attribute :#{column_name}_choices, #{array_in_text}
|
|
16
|
+
|
|
17
|
+
validate :validate_#{column_name}_choice
|
|
18
|
+
|
|
19
|
+
def validate_#{column_name}_choice
|
|
20
|
+
errors.add(\"#{column_name}\", \"#{configuration[:message]}\") unless self.#{column_name}_choices.include?(self.#{column_name})
|
|
21
|
+
end
|
|
22
|
+
"
|
|
23
|
+
|
|
24
|
+
choices.each do |choice|
|
|
25
|
+
class_eval "
|
|
26
|
+
named_scope :#{column_name}_is_#{clean(choice)}, :conditions => {:#{column_name} => \"#{choice}\"}
|
|
27
|
+
|
|
28
|
+
def #{column_name}_is_#{clean(choice)}?
|
|
29
|
+
self.#{column_name} == '#{choice}'
|
|
30
|
+
end
|
|
31
|
+
"
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
private
|
|
36
|
+
|
|
37
|
+
def clean(text)
|
|
38
|
+
return nil if text.blank?
|
|
39
|
+
str = Iconv.iconv('ascii//translit', 'utf-8', text).to_s
|
|
40
|
+
str.gsub!(/\W+/, ' ')
|
|
41
|
+
str.strip!
|
|
42
|
+
str.downcase!
|
|
43
|
+
str.gsub!(/\ +/, '_')
|
|
44
|
+
return str
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
ActiveRecord::Base.send :include, MagicEnums rescue nil
|
data/magic_enums.gemspec
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |s|
|
|
4
|
+
s.name = %q{magic_enums}
|
|
5
|
+
s.version = "0.0.1"
|
|
6
|
+
|
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
|
8
|
+
s.authors = ["Zach Inglis"]
|
|
9
|
+
s.date = %q{2008-11-23}
|
|
10
|
+
s.description = %q{A nice way to have enums in Rails.}
|
|
11
|
+
s.email = %q{zach@lt3media.com}
|
|
12
|
+
s.extra_rdoc_files = ["README.textile", "lib/magic_enums.rb"]
|
|
13
|
+
s.files = ["init.rb", "README.textile", "Rakefile", "MIT-LICENSE", "lib/magic_enums.rb", "test/magic_enums_test.rb", "Manifest", "magic_enums.gemspec", "test/test_helper.rb"]
|
|
14
|
+
s.has_rdoc = true
|
|
15
|
+
s.homepage = %q{http://github.com/zachinglis/magic_enums/tree/master}
|
|
16
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Magic_enums", "--main", "README.textile"]
|
|
17
|
+
s.require_paths = ["lib"]
|
|
18
|
+
s.rubyforge_project = %q{magic_enums}
|
|
19
|
+
s.rubygems_version = %q{1.3.1}
|
|
20
|
+
s.summary = %q{A nice way to have enums in Rails.}
|
|
21
|
+
s.test_files = ["test/magic_enums_test.rb", "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
|
+
else
|
|
29
|
+
end
|
|
30
|
+
else
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require File.join(File.dirname(__FILE__), "test_helper.rb")
|
|
2
|
+
|
|
3
|
+
class MagicEnumsTest < Test::Unit::TestCase
|
|
4
|
+
def setup
|
|
5
|
+
@person = Person.new
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def test_should_recognize_column_and_associated_class_variables
|
|
9
|
+
assert_respond_to Person, :gender_choices
|
|
10
|
+
assert_respond_to Person, :gender_choices=
|
|
11
|
+
assert_respond_to Person, :gender_is_male
|
|
12
|
+
assert_respond_to Person, :gender_is_female
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def test_should_recognize_instance_variables
|
|
16
|
+
assert_respond_to @person, :gender
|
|
17
|
+
assert_respond_to @person, :gender_is_male?
|
|
18
|
+
assert_respond_to @person, :gender_is_female?
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def test_should_return_true_on_choices
|
|
22
|
+
@person.gender = "male"
|
|
23
|
+
assert @person.gender_is_male?
|
|
24
|
+
@person.gender = "female"
|
|
25
|
+
assert @person.gender_is_female?
|
|
26
|
+
end
|
|
27
|
+
end
|
data/test/test_helper.rb
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require "test/unit"
|
|
2
|
+
require "rubygems"
|
|
3
|
+
require "active_record"
|
|
4
|
+
require File.join(File.dirname(__FILE__), "..", "lib", "magic_enums")
|
|
5
|
+
|
|
6
|
+
ActiveRecord::Schema.verbose = false
|
|
7
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
|
|
8
|
+
ActiveRecord::Base.configurations = true
|
|
9
|
+
ActiveRecord::Schema.define(:version => 1) do
|
|
10
|
+
create_table :people do |t|
|
|
11
|
+
t.datetime :created_at
|
|
12
|
+
t.datetime :updated_at
|
|
13
|
+
t.string :name
|
|
14
|
+
t.string :gender
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
class Person < ActiveRecord::Base
|
|
19
|
+
enum_column :gender, %w(male female)
|
|
20
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: negonicrac-magic_enums
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Zach Inglis
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
|
|
12
|
+
date: 2008-11-23 00:00:00 -08:00
|
|
13
|
+
default_executable:
|
|
14
|
+
dependencies: []
|
|
15
|
+
|
|
16
|
+
description: A nice way to have enums in Rails.
|
|
17
|
+
email: zach@lt3media.com
|
|
18
|
+
executables: []
|
|
19
|
+
|
|
20
|
+
extensions: []
|
|
21
|
+
|
|
22
|
+
extra_rdoc_files:
|
|
23
|
+
- README.textile
|
|
24
|
+
- lib/magic_enums.rb
|
|
25
|
+
files:
|
|
26
|
+
- init.rb
|
|
27
|
+
- README.textile
|
|
28
|
+
- Rakefile
|
|
29
|
+
- MIT-LICENSE
|
|
30
|
+
- lib/magic_enums.rb
|
|
31
|
+
- test/magic_enums_test.rb
|
|
32
|
+
- Manifest
|
|
33
|
+
- magic_enums.gemspec
|
|
34
|
+
- test/test_helper.rb
|
|
35
|
+
has_rdoc: true
|
|
36
|
+
homepage: http://github.com/zachinglis/magic_enums/tree/master
|
|
37
|
+
post_install_message:
|
|
38
|
+
rdoc_options:
|
|
39
|
+
- --line-numbers
|
|
40
|
+
- --inline-source
|
|
41
|
+
- --title
|
|
42
|
+
- Magic_enums
|
|
43
|
+
- --main
|
|
44
|
+
- README.textile
|
|
45
|
+
require_paths:
|
|
46
|
+
- lib
|
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - ">="
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: "0"
|
|
52
|
+
version:
|
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
|
+
requirements:
|
|
55
|
+
- - ">="
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: "1.2"
|
|
58
|
+
version:
|
|
59
|
+
requirements: []
|
|
60
|
+
|
|
61
|
+
rubyforge_project: magic_enums
|
|
62
|
+
rubygems_version: 1.2.0
|
|
63
|
+
signing_key:
|
|
64
|
+
specification_version: 2
|
|
65
|
+
summary: A nice way to have enums in Rails.
|
|
66
|
+
test_files:
|
|
67
|
+
- test/magic_enums_test.rb
|
|
68
|
+
- test/test_helper.rb
|