light_enum 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/Rakefile +38 -0
- data/lib/light_enum.rb +63 -0
- data/lib/light_enum/version.rb +3 -0
- data/lib/tasks/light_enum_tasks.rake +4 -0
- data/test/light_enum_test.rb +66 -0
- metadata +84 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2013 Ddl1st
|
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/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
begin
|
3
|
+
require 'bundler/setup'
|
4
|
+
rescue LoadError
|
5
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'rdoc/task'
|
9
|
+
rescue LoadError
|
10
|
+
require 'rdoc/rdoc'
|
11
|
+
require 'rake/rdoctask'
|
12
|
+
RDoc::Task = Rake::RDocTask
|
13
|
+
end
|
14
|
+
|
15
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
16
|
+
rdoc.rdoc_dir = 'rdoc'
|
17
|
+
rdoc.title = 'LightEnum'
|
18
|
+
rdoc.options << '--line-numbers'
|
19
|
+
rdoc.rdoc_files.include('README.rdoc')
|
20
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
Bundler::GemHelper.install_tasks
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
|
30
|
+
Rake::TestTask.new(:test) do |t|
|
31
|
+
t.libs << 'lib'
|
32
|
+
t.libs << 'test'
|
33
|
+
t.pattern = 'test/**/*_test.rb'
|
34
|
+
t.verbose = false
|
35
|
+
end
|
36
|
+
|
37
|
+
|
38
|
+
task :default => :test
|
data/lib/light_enum.rb
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
module LightEnum
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
## light_enum :status , ["Open","Close","ReOpen"]
|
9
|
+
# ["Open","Close","ReOpen"] will be convert to [["Open",0],["Close",1],["ReOpen",2]]
|
10
|
+
## :validate * true * use validator if included ActiveModel::Validations. default is false
|
11
|
+
def light_enum(*args)
|
12
|
+
options = {:validate => false}
|
13
|
+
options.merge!(args.pop) if args.last.is_a?(Hash)
|
14
|
+
raise ArgumentError, "You have to supply at least two format like :attr_name,[['China',1],.....]" if args.empty?
|
15
|
+
|
16
|
+
_attr = args.shift
|
17
|
+
const, summary = _attr.upcase, args.pop.to_td
|
18
|
+
|
19
|
+
vals = summary.map(&:last)
|
20
|
+
if vals.uniq.length != vals.length
|
21
|
+
raise ArgumentError, "The last argument of each array must be unique"
|
22
|
+
end
|
23
|
+
const_value = "#{const}_VALUES"
|
24
|
+
|
25
|
+
[[const,summary],[const_value,summary.map(&:last)]].each do |name,val|
|
26
|
+
silence_warnings {const_set(name,val)} if !const_defined?(name) || val != const_get(name)
|
27
|
+
end
|
28
|
+
|
29
|
+
if respond_to?(:validates) && options[:validate]
|
30
|
+
validates_presence_of _attr.to_sym , :if => proc { respond_to?(:columns_hash) && self.columns_hash[_attr.to_s].type == :boolean }
|
31
|
+
validates_inclusion_of _attr.to_sym , :in => const_get(const_value)
|
32
|
+
end
|
33
|
+
|
34
|
+
define_method("#{_attr}_name") do
|
35
|
+
raise NoMethodError,"Not defined #{_attr}" if !respond_to?("#{_attr}")
|
36
|
+
if ary = summary.rassoc(send(_attr))
|
37
|
+
ary.__send__(:first)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
summary.each do |name,target|
|
42
|
+
define_method("#{_attr}_#{target.to_s.downcase}?") { __send__(_attr) == target }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
class ::Object
|
47
|
+
def silence_warnings
|
48
|
+
with_warnings(nil) { yield }
|
49
|
+
end
|
50
|
+
|
51
|
+
def with_warnings(flag)
|
52
|
+
old_verbose, $VERBOSE = $VERBOSE, flag
|
53
|
+
yield
|
54
|
+
ensure
|
55
|
+
$VERBOSE = old_verbose
|
56
|
+
end
|
57
|
+
end
|
58
|
+
class ::Array
|
59
|
+
def to_td
|
60
|
+
each_with_index {|enum,index| self[index] = [enum,index] unless enum.is_a?(Array) }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
require 'minitest'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'light_enum'
|
5
|
+
require 'active_model'
|
6
|
+
|
7
|
+
class TestLightEnum < Minitest::Test
|
8
|
+
|
9
|
+
class Foo
|
10
|
+
include LightEnum
|
11
|
+
attr_accessor :status
|
12
|
+
light_enum :status, ["Open","Close","Reopen",["Done","D"]]
|
13
|
+
end
|
14
|
+
|
15
|
+
class Bar
|
16
|
+
include ActiveModel::Validations
|
17
|
+
include LightEnum
|
18
|
+
attr_accessor :status,:published
|
19
|
+
light_enum :status, ["Open","Close","Reopen",["Done","D"]],:validate => true
|
20
|
+
light_enum :published,[["公开",1],["私人",2]]
|
21
|
+
end
|
22
|
+
|
23
|
+
def setup
|
24
|
+
@foo = Foo.new
|
25
|
+
@bar = Bar.new
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_light_enum
|
29
|
+
assert_equal [["Open",0],["Close",1],["Reopen",2],["Done","D"]],Foo::STATUS
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_enum_values
|
33
|
+
assert_equal [0,1,2,"D"],Foo::STATUS_VALUES
|
34
|
+
@foo.status = 0
|
35
|
+
assert_equal true, @foo.status_0?
|
36
|
+
assert_equal false, @foo.status_1?
|
37
|
+
@foo.status = "D"
|
38
|
+
assert_equal true, @foo.status_d?
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_enum_name
|
42
|
+
assert_equal nil,@foo.status_name
|
43
|
+
@foo.status = 2
|
44
|
+
assert_equal "Reopen", @foo.status_name
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_validate
|
48
|
+
assert_equal false, @bar.valid?
|
49
|
+
|
50
|
+
assert_equal ["Status is not included in the list"], @bar.errors.full_messages
|
51
|
+
|
52
|
+
@bar.status = 4
|
53
|
+
assert_equal false, @bar.valid?
|
54
|
+
assert_equal ["Status is not included in the list"], @bar.errors.full_messages
|
55
|
+
|
56
|
+
@bar.status = 1
|
57
|
+
assert_equal true, @bar.valid?
|
58
|
+
assert_equal [], @bar.errors.full_messages
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_not_validate
|
62
|
+
@bar.status = 1
|
63
|
+
assert_equal true,@bar.valid?
|
64
|
+
assert_equal nil,@bar.published_name
|
65
|
+
end
|
66
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: light_enum
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- ddl1st
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-16 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: minitest
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activemodel
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 3.0.0
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.0.0
|
46
|
+
description: LightEnum.
|
47
|
+
email:
|
48
|
+
- idolifetime@gmail.com
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- MIT-LICENSE
|
54
|
+
- Rakefile
|
55
|
+
- lib/light_enum/version.rb
|
56
|
+
- lib/light_enum.rb
|
57
|
+
- lib/tasks/light_enum_tasks.rake
|
58
|
+
- test/light_enum_test.rb
|
59
|
+
homepage: https://github.com/ddl1st/light_enum
|
60
|
+
licenses: []
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
requirements: []
|
78
|
+
rubyforge_project:
|
79
|
+
rubygems_version: 1.8.25
|
80
|
+
signing_key:
|
81
|
+
specification_version: 3
|
82
|
+
summary: LightEnum.
|
83
|
+
test_files:
|
84
|
+
- test/light_enum_test.rb
|