enum_attr 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile +0 -0
- data/MIT-LICENSE +21 -0
- data/README.rdoc +32 -0
- data/Rakefile +18 -0
- data/VERSION +1 -0
- data/init.rb +2 -0
- data/lib/enum_attr.rb +21 -0
- metadata +71 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.svn
|
data/Gemfile
ADDED
File without changes
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2010 [Qichunren]
|
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.
|
21
|
+
|
data/README.rdoc
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
= A rails plugin for active_record model
|
2
|
+
manage mapping column
|
3
|
+
|
4
|
+
= Usage
|
5
|
+
In your model,for example in Post,you have a column named status(integer), 1 means normal, 2 means draft, 3 means deleted.
|
6
|
+
Now,with enum_attr plugin,you just decalre:
|
7
|
+
|
8
|
+
enum_attr :status, [ ["正常", 1, "normal"], ["草稿", 2, "draft"], ["已删除", 3, "deleted"] ]
|
9
|
+
|
10
|
+
with this declare, you have these code below:
|
11
|
+
|
12
|
+
consts:
|
13
|
+
STATUS_NORMAL = 1; STATUS_DRAFT = 2; STATUS_DELETED = 3
|
14
|
+
ENUMS_STATUS = [ ["正常", 1], ["草稿", 2], ["已删除", 3] ] # this is for select tag
|
15
|
+
|
16
|
+
valitates:
|
17
|
+
validates_inclusion_of "status", :in => [1,2,3], :allow_nil => true
|
18
|
+
|
19
|
+
methods:
|
20
|
+
# instant_method
|
21
|
+
def status_name
|
22
|
+
# ... return readable status name according to stauts value(integer)
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
= Inspiration
|
27
|
+
Thanks Quakewang
|
28
|
+
http://quake.javaeye.com/blog/448235
|
29
|
+
|
30
|
+
= Copyright
|
31
|
+
|
32
|
+
Copyright © 2010 Qichunren. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
|
7
|
+
Jeweler::Tasks.new do |gemspec|
|
8
|
+
gemspec.name = "enum_attr"
|
9
|
+
gemspec.summary = "enum_attr is a Ruby gem to manage enum column for active_record."
|
10
|
+
gemspec.description = "A Rails plugin which brings easy-to-use enum-like functionality to ActiveRecord models (now compatible with rails 3, ruby 1.9 and jruby). ."
|
11
|
+
gemspec.email = "whyruby@gmail.com"
|
12
|
+
gemspec.homepage = "https://github.com/qichunren/enum_attr"
|
13
|
+
gemspec.authors = ["qichunren"]
|
14
|
+
end
|
15
|
+
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
18
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/init.rb
ADDED
data/lib/enum_attr.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module EnumAttr
|
2
|
+
module Mixin
|
3
|
+
# for model Contract:
|
4
|
+
# enum_attr :contract_type, [['拍品合同', '0', "auction"], ['商品合同', '1', "goods"], ["台湾合同", '2', "taiwan"]]
|
5
|
+
#
|
6
|
+
def enum_attr(attr, enums)
|
7
|
+
attr = attr.to_s
|
8
|
+
enums.each do |enum|
|
9
|
+
const_set("#{attr.upcase}_#{enum[2].upcase}", enum[1] )
|
10
|
+
end
|
11
|
+
self.class_eval(%Q{
|
12
|
+
|
13
|
+
ENUMS_#{attr.upcase} = enums.collect{|item| [item[0], item[1]]}
|
14
|
+
validates_inclusion_of attr, :in => enums.map{|e| e[1]}, :allow_nil => true
|
15
|
+
def #{attr}_name
|
16
|
+
ENUMS_#{attr.upcase}.find{|option| option[1] == #{attr}}[0] unless #{attr}.nil?
|
17
|
+
end
|
18
|
+
})
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: enum_attr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- qichunren
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-11-12 00:00:00 +08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: A Rails plugin which brings easy-to-use enum-like functionality to ActiveRecord models (now compatible with rails 3, ruby 1.9 and jruby). .
|
22
|
+
email: whyruby@gmail.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.rdoc
|
29
|
+
files:
|
30
|
+
- .gitignore
|
31
|
+
- Gemfile
|
32
|
+
- MIT-LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
- Rakefile
|
35
|
+
- VERSION
|
36
|
+
- init.rb
|
37
|
+
- lib/enum_attr.rb
|
38
|
+
has_rdoc: true
|
39
|
+
homepage: https://github.com/qichunren/enum_attr
|
40
|
+
licenses: []
|
41
|
+
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options:
|
44
|
+
- --charset=UTF-8
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
version: "0"
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.3.7
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: enum_attr is a Ruby gem to manage enum column for active_record.
|
70
|
+
test_files: []
|
71
|
+
|