def_enum_helper 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/Gemfile +4 -0
- data/README.md +70 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/def_enum_helper.gemspec +24 -0
- data/lib/def_enum_helper/version.rb +3 -0
- data/lib/def_enum_helper.rb +111 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 590ddea64979d3f00a57ed5bf26f468377ca13c7
|
4
|
+
data.tar.gz: a362fd79ea34dbe0f1e0ff72190637acf44a7988
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b9c5c97a0201046f5bcb2e08eff89a21dc2ad9fd202d3e46cfa3d234497ca3e24fc88ab0135dba49db08fb3a8f14856c40263f2d404fb3bb6c2ab0a8bd14202d
|
7
|
+
data.tar.gz: 04aeb46bad251b7ef006b1fd1ac9b5ca5501110e8c6dc0b93d24e24cc7f8767008322aa50c1112d167164850884c3c62423ab5a70aa6435fef3e0fe214a94581
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
# DefEnumHelper
|
2
|
+
|
3
|
+
Define very powerful enum class.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'def_enum_helper'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install def_enum_helper
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
# 默认枚举从1开始递增
|
25
|
+
|
26
|
+
def_enum :YinYang, {YANG: "阳", YIN: "阴"}
|
27
|
+
|
28
|
+
puts YinYang::YANG # 1
|
29
|
+
puts YinYang::YIN # 2
|
30
|
+
y = YinYang[1]
|
31
|
+
puts y.index # 1
|
32
|
+
puts y.display # 阳
|
33
|
+
YinYang[:YANG] # same as YinYang[1]
|
34
|
+
YinYang["阳"] # same as YinYang[1]
|
35
|
+
YinYang.all # 所有枚举的struct array [#<struct index=1, display="阳">, # <struct index=2, display="阴">]
|
36
|
+
puts YinYang.count # 2
|
37
|
+
YinYang.each{|x| puts x} # <struct index=1, display="阳"> #<struct index=2, display="阴">
|
38
|
+
puts YinYang.to_hash_array # [{:index=>1, :display=>"阳"}, {:index=>2, :display=>"阴"}]
|
39
|
+
puts YingYang.to_json_array # [{"index":1,"display":"阳"},{"index":2,"display":"阴"}]
|
40
|
+
```
|
41
|
+
|
42
|
+
```ruby
|
43
|
+
# 自定义枚举整数值
|
44
|
+
|
45
|
+
def_enum_with_index(:DataOperationType,
|
46
|
+
[:INSERT, 3, '插入'],
|
47
|
+
[:UPDATE, 4, '修改'],
|
48
|
+
[:DELETE, 5, '删除'])
|
49
|
+
|
50
|
+
puts DataOperationType::INSERT # 3
|
51
|
+
|
52
|
+
```
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
# 自定义枚举对象
|
56
|
+
CustomStruct = Struct.new(:index, :display, :short_name)
|
57
|
+
|
58
|
+
def_enum_struct_with_index(:Country,
|
59
|
+
{
|
60
|
+
CHINA: CustomStruct.new(1, '中华人民共和国', '中国'),
|
61
|
+
USA: CustomStruct.new(2, '美利坚合众国', '美国')
|
62
|
+
})
|
63
|
+
|
64
|
+
Country[:CHINA] # <struct CustomStruct index=1, display="中华人民共和国", short_name="中国">
|
65
|
+
|
66
|
+
```
|
67
|
+
|
68
|
+
### License
|
69
|
+
|
70
|
+
(MIT License) - Copyright (c) 2016 Charles Zhang
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "def_enum_helper"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'def_enum_helper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "def_enum_helper"
|
8
|
+
spec.version = DefEnumHelper::VERSION
|
9
|
+
spec.authors = ["Charles Zhang"]
|
10
|
+
spec.email = ["gis05zc@163.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{define very powerful enum class}
|
13
|
+
spec.homepage = "https://github.com/chinazhangchao/def_enum_helper"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require "def_enum_helper/version"
|
2
|
+
|
3
|
+
# example
|
4
|
+
|
5
|
+
# def_enum :YinYang, {YANG: "阳", YIN: "阴"}
|
6
|
+
|
7
|
+
# puts YinYang::YANG #1
|
8
|
+
# puts YinYang::YIN #2
|
9
|
+
# y = YinYang[1]
|
10
|
+
# puts y.index #1
|
11
|
+
# puts y.display #阳
|
12
|
+
# puts YinYang[:YANG] #same as YinYang[1]
|
13
|
+
# puts YinYang["阳"] #same as YinYang[1]
|
14
|
+
# YinYang.all #所有枚举的struct array [#<struct index=1, display="阳">, #<struct index=2, display="阴">]
|
15
|
+
# puts YinYang.count #2
|
16
|
+
# YinYang.each{|x| puts x} #<struct index=1, display="阳"> #<struct index=2, display="阴">
|
17
|
+
# puts YingYang.to_json_array #[{"index":1,"display":"阳"},{"index":2,"display":"阴"}]
|
18
|
+
|
19
|
+
DEFAULT_ENUM_BASE_INDEX = 1
|
20
|
+
|
21
|
+
def def_enum(module_name, enum_hash, enum_base: DEFAULT_ENUM_BASE_INDEX, index_name: :index, display_name: :display)
|
22
|
+
temp_struct = Struct.new(index_name, display_name)
|
23
|
+
new_enum_hash = {}
|
24
|
+
enum_hash.each do |k, v|
|
25
|
+
new_enum_hash[k] = temp_struct.new(0, v)
|
26
|
+
end
|
27
|
+
def_enum_struct(module_name, new_enum_hash, enum_base: enum_base)
|
28
|
+
end
|
29
|
+
|
30
|
+
def def_enum_struct(module_name, enum_hash, enum_base: DEFAULT_ENUM_BASE_INDEX, index_name: :index, display_name: :display)
|
31
|
+
i = 0
|
32
|
+
set_index = (index_name.to_s + '=').to_sym
|
33
|
+
enum_hash.each do |_k, v|
|
34
|
+
enum_value = enum_base + i
|
35
|
+
v.public_send(set_index, enum_value)
|
36
|
+
i += 1
|
37
|
+
end
|
38
|
+
|
39
|
+
def_enum_struct_with_index(module_name, enum_hash, index_name: index_name, display_name: display_name)
|
40
|
+
end
|
41
|
+
|
42
|
+
EnumHelperStruct = Struct.new(:index, :display)
|
43
|
+
|
44
|
+
def def_enum_with_index(module_name, *value_array)
|
45
|
+
h = {}
|
46
|
+
value_array.each do |variable|
|
47
|
+
h[variable[0]] = EnumHelperStruct.new(variable[1], variable[2])
|
48
|
+
end
|
49
|
+
|
50
|
+
def_enum_struct_with_index module_name, h
|
51
|
+
end
|
52
|
+
|
53
|
+
def define_iterator_methods(m, enum_index_hash)
|
54
|
+
m.define_singleton_method(:each) { |&blk| enum_index_hash.each_value { |v| blk.call(v) } }
|
55
|
+
|
56
|
+
m.define_singleton_method(:all) { enum_index_hash.values }
|
57
|
+
end
|
58
|
+
|
59
|
+
def define_subscript_method(m, enum_index_hash, enum_display_hash)
|
60
|
+
divisor = enum_index_hash.count
|
61
|
+
|
62
|
+
m.define_singleton_method(:from_number) { |n| (n - DEFAULT_ENUM_BASE_INDEX) % divisor + DEFAULT_ENUM_BASE_INDEX }
|
63
|
+
m.define_singleton_method(:[]) do |k|
|
64
|
+
case k
|
65
|
+
when String
|
66
|
+
enum_display_hash[k]
|
67
|
+
when Symbol
|
68
|
+
enum_index_hash[const_get(k)]
|
69
|
+
else
|
70
|
+
enum_index_hash[from_number(k)]
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def define_convert_methods(m, enum_index_hash)
|
76
|
+
m.define_singleton_method(:to_hash_array) { enum_index_hash.each_value.map(&:to_h) }
|
77
|
+
|
78
|
+
m.define_singleton_method(:to_json_array) { to_hash_array.to_json }
|
79
|
+
end
|
80
|
+
|
81
|
+
def define_enum_methods(m, enum_index_hash, enum_display_hash)
|
82
|
+
define_iterator_methods(m, enum_index_hash)
|
83
|
+
|
84
|
+
m.define_singleton_method(:include?) { |n| n.is_a?(Symbol) ? constants.include?(n) : enum_index_hash.key?(n) }
|
85
|
+
|
86
|
+
m.define_singleton_method(:member?) { |n| include?(n) }
|
87
|
+
|
88
|
+
m.define_singleton_method(:count) { enum_index_hash.count }
|
89
|
+
|
90
|
+
define_convert_methods(m, enum_index_hash)
|
91
|
+
|
92
|
+
define_subscript_method(m, enum_index_hash, enum_display_hash)
|
93
|
+
end
|
94
|
+
|
95
|
+
def def_enum_struct_with_index(module_name, enum_hash, index_name: :index, display_name: :display)
|
96
|
+
m = Module.new
|
97
|
+
enum_index_hash = {}
|
98
|
+
enum_display_hash = {}
|
99
|
+
i = 0
|
100
|
+
enum_hash.each do |k, v|
|
101
|
+
enum_value = v.public_send(index_name)
|
102
|
+
m.const_set(k, enum_value)
|
103
|
+
enum_index_hash[enum_value] = v
|
104
|
+
enum_display_hash[v.public_send(display_name)] = v
|
105
|
+
i += 1
|
106
|
+
end
|
107
|
+
|
108
|
+
define_enum_methods(m, enum_index_hash, enum_display_hash)
|
109
|
+
|
110
|
+
Kernel.const_set(module_name, m)
|
111
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: def_enum_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Charles Zhang
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- gis05zc@163.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- README.md
|
51
|
+
- Rakefile
|
52
|
+
- bin/console
|
53
|
+
- bin/setup
|
54
|
+
- def_enum_helper.gemspec
|
55
|
+
- lib/def_enum_helper.rb
|
56
|
+
- lib/def_enum_helper/version.rb
|
57
|
+
homepage: https://github.com/chinazhangchao/def_enum_helper
|
58
|
+
licenses: []
|
59
|
+
metadata: {}
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options: []
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project:
|
76
|
+
rubygems_version: 2.6.6
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: define very powerful enum class
|
80
|
+
test_files: []
|