acts_as_enum 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README.rdoc +51 -0
- data/Rakefile +23 -0
- data/VERSION +1 -0
- data/init.rb +1 -0
- data/lib/acts_as_enum.rb +82 -0
- data/lib/tasks/acts_as_enum_tasks.rake +4 -0
- data/test/acts_as_enum_test.rb +8 -0
- data/test/test_helper.rb +3 -0
- metadata +64 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 [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/README.rdoc
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
== ActsAsEnum
|
2
|
+
|
3
|
+
主要应用于有枚举类型属性的Model,这个插件会帮我们生成一些常用到的方法。
|
4
|
+
|
5
|
+
如:枚举数组常量,每个元素的常量和布尔方法,属性的名字,Named scope方法
|
6
|
+
|
7
|
+
Support rails 3
|
8
|
+
|
9
|
+
== Usage
|
10
|
+
|
11
|
+
class User < ActiveRecord::Base
|
12
|
+
acts_as_enum :status, :enum => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
|
13
|
+
end
|
14
|
+
|
15
|
+
Also can usage alias enum_attr
|
16
|
+
enum_attr :status, :enum => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
|
17
|
+
|
18
|
+
|
19
|
+
Will generate bellow:
|
20
|
+
|
21
|
+
Constants: User::STATUSES, User::DISABLE, User::ENABLE
|
22
|
+
|
23
|
+
Named scopes: User.enable, User.disable
|
24
|
+
|
25
|
+
Class methods: User.status_options
|
26
|
+
|
27
|
+
Instance methods: user.status_name, user.enable?, user.disable?
|
28
|
+
|
29
|
+
|
30
|
+
If with option prefix is true:
|
31
|
+
acts_as_enum :status, :enum => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ], :prefix => true
|
32
|
+
|
33
|
+
Will generate bellow:
|
34
|
+
|
35
|
+
User::STATUS_DISABLE, User::STATUS_ENABLE, User.status_enable, User.status_disable, user.status_enable?, user.status_disable?
|
36
|
+
|
37
|
+
|
38
|
+
== Install
|
39
|
+
|
40
|
+
git clone git://github.com/wenke/acts_as_enum.git OR
|
41
|
+
|
42
|
+
rails 2
|
43
|
+
./script/plugin install git://github.com/wenke/acts_as_enum.git
|
44
|
+
|
45
|
+
rails 3
|
46
|
+
rails plugin install git://github.com/wenke/acts_as_enum.git
|
47
|
+
|
48
|
+
|
49
|
+
== Note
|
50
|
+
|
51
|
+
Copyright (c) 2010 liangwenke8@gmail.com, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the acts_as_enum plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the acts_as_enum plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'ActsAsEnum'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README.rdoc')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'acts_as_enum'
|
data/lib/acts_as_enum.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# EnumAttr# == Example
|
2
|
+
#
|
3
|
+
# class User < ActiveRecord::Base
|
4
|
+
# acts_as_enum :status, :enum => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
|
5
|
+
# end
|
6
|
+
#
|
7
|
+
# Also can usage alias enum_attr
|
8
|
+
# enum_attr :status, :enum => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ]
|
9
|
+
#
|
10
|
+
# Will generate bellow:
|
11
|
+
#
|
12
|
+
# Constants: User::STATUSES(return hash { 0 => "冻结", 1 => "激活" }), User::DISABLE, User::ENABLE
|
13
|
+
#
|
14
|
+
# Named scopes: User.enable, User.disable
|
15
|
+
#
|
16
|
+
# Class methods: User.status_options => [["冻结", 0], ["激活", 1]]
|
17
|
+
#
|
18
|
+
# Instance methods: user.status_name, user.enable?, user.disable?
|
19
|
+
#
|
20
|
+
#
|
21
|
+
# If with option prefix is true:
|
22
|
+
# acts_as_enum :status, :enum => [ ['disable', 0, '冻结'], ['enable', 1, '激活'] ], :prefix => true
|
23
|
+
#
|
24
|
+
# Will generate bellow:
|
25
|
+
# User::STATUS_DISABLE, User::STATUS_ENABLE, User.status_enable, User.status_disable, user.status_enable?, user.status_disable?
|
26
|
+
|
27
|
+
module ActsAsEnum
|
28
|
+
def self.included(base)
|
29
|
+
base.extend ClassMethods
|
30
|
+
end
|
31
|
+
|
32
|
+
module ClassMethods
|
33
|
+
def acts_as_enum(attr, opts = { :enum => [], :prefix => false })
|
34
|
+
attr = attr.to_s
|
35
|
+
plural_upcase_attr = attr.pluralize.upcase
|
36
|
+
enum = opts[:enum]
|
37
|
+
|
38
|
+
rails "Can not load Rails" unless defined?(Rails)
|
39
|
+
rails "Options enum can not be empty" if enum.blank?
|
40
|
+
rails "Options enum must be an array object" unless enum.is_a?(Array)
|
41
|
+
|
42
|
+
validates_inclusion_of attr, :in => enum.collect { |arr| arr[1] }, :allow_blank => true
|
43
|
+
|
44
|
+
const_set(plural_upcase_attr, enum.inject({}) { |hash, arr| hash[arr[1]] = arr[2].to_s; hash })
|
45
|
+
|
46
|
+
enum.each do |arr|
|
47
|
+
enum_name, attr_value = arr[0].to_s, arr[1]
|
48
|
+
method_name = opts[:prefix] ? "#{attr}_#{enum_name}" : enum_name
|
49
|
+
|
50
|
+
const_set("#{method_name}".upcase, attr_value)
|
51
|
+
|
52
|
+
if Rails.version =~ /^2/
|
53
|
+
named_scope method_name.to_sym, :conditions => { attr.to_sym => attr_value }
|
54
|
+
else
|
55
|
+
scope method_name.to_sym, where(["#{attr} = ?", attr_value])
|
56
|
+
end
|
57
|
+
|
58
|
+
class_eval(%Q{
|
59
|
+
def #{method_name}?
|
60
|
+
#{attr} == #{attr_value}
|
61
|
+
end
|
62
|
+
})
|
63
|
+
end
|
64
|
+
|
65
|
+
class_eval(%Q{
|
66
|
+
def self.#{attr}_options
|
67
|
+
#{plural_upcase_attr}.inject([]){ |arr, obj| arr << obj.reverse }
|
68
|
+
end
|
69
|
+
|
70
|
+
def #{attr}_name
|
71
|
+
# #{plural_upcase_attr}.detect { |arr| arr[1] == #{attr} }[0] unless #{attr}.blank?
|
72
|
+
#{plural_upcase_attr}[#{attr}] unless #{attr}.blank?
|
73
|
+
end
|
74
|
+
})
|
75
|
+
end
|
76
|
+
|
77
|
+
alias enum_attr acts_as_enum
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
ActiveRecord::Base.send(:include, ActsAsEnum)
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_enum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- LiangWenKe
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-03-20 00:00:00 +08:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: "\xE4\xB8\xBB\xE8\xA6\x81\xE5\xBA\x94\xE7\x94\xA8\xE4\xBA\x8E\xE6\x9C\x89\xE6\x9E\x9A\xE4\xB8\xBE\xE7\xB1\xBB\xE5\x9E\x8B\xE5\xB1\x9E\xE6\x80\xA7\xE7\x9A\x84Model\xEF\xBC\x8C\xE8\xBF\x99\xE4\xB8\xAA\xE6\x8F\x92\xE4\xBB\xB6\xE4\xBC\x9A\xE5\xB8\xAE\xE6\x88\x91\xE4\xBB\xAC\xE7\x94\x9F\xE6\x88\x90\xE4\xB8\x80\xE4\xBA\x9B\xE5\xB8\xB8\xE7\x94\xA8\xE5\x88\xB0\xE7\x9A\x84\xE6\x96\xB9\xE6\xB3\x95\xE3\x80\x82"
|
18
|
+
email: liangwenke8@gmail.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- lib/acts_as_enum.rb
|
27
|
+
- lib/tasks/acts_as_enum_tasks.rake
|
28
|
+
- test/acts_as_enum_test.rb
|
29
|
+
- test/test_helper.rb
|
30
|
+
- MIT-LICENSE
|
31
|
+
- Rakefile
|
32
|
+
- README.rdoc
|
33
|
+
- VERSION
|
34
|
+
- init.rb
|
35
|
+
has_rdoc: true
|
36
|
+
homepage: http://github.com/wenke/acts_as_enum
|
37
|
+
licenses: []
|
38
|
+
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 1.3.4
|
56
|
+
requirements: []
|
57
|
+
|
58
|
+
rubyforge_project: acts_as_enum
|
59
|
+
rubygems_version: 1.6.2
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Enum Attribute for Rails ActiveRecord
|
63
|
+
test_files: []
|
64
|
+
|