def_enum_helper 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 6716d54dc234d894fe3c944da6eed2f95a333dcf
4
- data.tar.gz: 97e542fc0a6594f65720686fd52f7a2ed9edc4c6
2
+ SHA256:
3
+ metadata.gz: dc3f927c3dc00d6e938a46358c075ee1d5d2b6ac8f00462a5c7bf6db6a12d863
4
+ data.tar.gz: 3094efeafd6c868035c7ffd1d379580e3ad0347985316f08855ea73f7e589a33
5
5
  SHA512:
6
- metadata.gz: bb0afcc78f37940a3c925177533b80b57f28499092bd0e3c2e2d75c13694f1454f2aab7be55bde68c002d6dabc80f60dab831b44eb071603567ecb0f624dc62f
7
- data.tar.gz: da0bdcf1505507f3c5b62e3ad728c6308a647897ad4cc364baafe86034ee1087b27abfd7d37034b6fb059ab6e14b0fa5ec90d68f1af14a3781281dc63465057c
6
+ metadata.gz: 5c56039dd8d266490ab1e8192c69c463402e8948432fbf2ca6cd8f85f37737256719bd209ac404de33f199b5f92be9758cefb76744267ac0698b350a92add7bf
7
+ data.tar.gz: 2c7e9db0a796f3c0b27f7181a6490c2da13cd0bb3d6476ffd1db7abe7f053e61ceaa87946240d3c6018773b8eadd723379a99bff05c91dff2a23b8b23c9c6545
data/.gitignore CHANGED
@@ -8,4 +8,5 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  *.gem
11
- .DS_Store
11
+ .DS_Store
12
+ rubocopresult
data/.rubocop.yml ADDED
@@ -0,0 +1,48 @@
1
+ Metrics/LineLength:
2
+ Max: 120
3
+ Metrics/MethodLength:
4
+ Max: 50
5
+ Metrics/ParameterLists:
6
+ Max: 12
7
+ Metrics/AbcSize:
8
+ Max: 50
9
+ Metrics/CyclomaticComplexity:
10
+ Max: 10
11
+ Metrics/PerceivedComplexity:
12
+ Max: 10
13
+ Style/GuardClause:
14
+ MinBodyLength: 5
15
+ Style/AsciiComments:
16
+ Enabled: false
17
+ Style/Documentation:
18
+ Enabled: false
19
+ Lint/AmbiguousRegexpLiteral:
20
+ Enabled: false
21
+ Layout/DefEndAlignment:
22
+ AutoCorrect: true
23
+ Layout/EndAlignment:
24
+ AutoCorrect: true
25
+ Style/BracesAroundHashParameters:
26
+ Enabled: false
27
+ Style/ClassAndModuleChildren:
28
+ Enabled: false
29
+ Style/AutoResourceCleanup:
30
+ Enabled: true
31
+ Style/CollectionMethods:
32
+ Enabled: true
33
+ Style/Encoding:
34
+ Enabled: true
35
+ Style/MethodCalledOnDoEndBlock:
36
+ Enabled: true
37
+ Layout/MultilineAssignmentLayout:
38
+ Enabled: true
39
+ Style/OptionHash:
40
+ Enabled: true
41
+ Style/StringMethods:
42
+ Enabled: true
43
+ Style/SymbolArray:
44
+ Enabled: true
45
+ Style/NonNilCheck:
46
+ IncludeSemanticChanges: true
47
+ Style/Send:
48
+ Enabled: true
data/README.md CHANGED
@@ -30,6 +30,7 @@ puts YinYang::YIN # 2
30
30
  y = YinYang[1]
31
31
  puts y.index # 1
32
32
  puts y.display # 阳
33
+ puts y.symbol # :YANG
33
34
  YinYang[:YANG] # same as YinYang[1]
34
35
  YinYang["阳"] # same as YinYang[1]
35
36
  YinYang.all # 所有枚举的struct array [#<struct index=1, display="阳">, # <struct index=2, display="阴">]
@@ -37,7 +38,14 @@ puts YinYang.count # 2
37
38
  YinYang.each{|x| puts x} # <struct index=1, display="阳"> #<struct index=2, display="阴">
38
39
  YinYang.map{|x| x} # [#<struct index=1, display="阳">, #<struct index=2, display="阴">]
39
40
  puts YinYang.to_hash_array # [{:index=>1, :display=>"阳"}, {:index=>2, :display=>"阴"}]
40
- puts YingYang.to_json_array # [{"index":1,"display":"阳"},{"index":2,"display":"阴"}]
41
+ puts YinYang.to_json_array # [{"index":1,"display":"阳"},{"index":2,"display":"阴"}]
42
+ puts YinYang.keys # [:YANG, :YIN]
43
+ puts YinYang.indexes # [1, 2]
44
+ ```
45
+
46
+ ```ruby
47
+ # 自定义字段名
48
+ def_enum :YinYang, {YANG: "阳", YIN: "阴"}, symbol_name: :s, index_name: :i, display_name: :d
41
49
  ```
42
50
 
43
51
  ```ruby
@@ -49,7 +57,17 @@ def_enum_with_index(:DataOperationType,
49
57
  [:DELETE, 5, '删除'])
50
58
 
51
59
  puts DataOperationType::INSERT # 3
60
+ ```
52
61
 
62
+ ```ruby
63
+ # 自定义字段名
64
+ def_enum_with_index(:DataOperationType,
65
+ [:INSERT, 3, '插入'],
66
+ [:UPDATE, 4, '修改'],
67
+ [:DELETE, 5, '删除'],
68
+ symbol_name: :s,
69
+ index_name: :i,
70
+ display_name: :d)
53
71
  ```
54
72
 
55
73
  ```ruby
@@ -63,7 +81,6 @@ def_enum_struct_with_index(:Country,
63
81
  })
64
82
 
65
83
  Country[:CHINA] # <struct CustomStruct index=1, display="中华人民共和国", short_name="中国">
66
-
67
84
  ```
68
85
 
69
86
  ### License
data/Rakefile CHANGED
@@ -1,2 +1,2 @@
1
- require "bundler/gem_tasks"
2
- task :default => :spec
1
+ require 'bundler/gem_tasks'
2
+ task default: :spec
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "def_enum_helper"
3
+ require 'bundler/setup'
4
+ require 'def_enum_helper'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "def_enum_helper"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start
data/check_code.sh ADDED
@@ -0,0 +1,3 @@
1
+ #!/bin/sh
2
+
3
+ rubocop -a -D -f simple -o rubocopresult
@@ -1,24 +1,27 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
1
+
2
+ lib = File.expand_path('lib', __dir__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'def_enum_helper/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "def_enum_helper"
7
+ spec.name = 'def_enum_helper'
8
8
  spec.version = DefEnumHelper::VERSION
9
- spec.authors = ["Charles Zhang"]
10
- spec.email = ["gis05zc@163.com"]
9
+ spec.authors = ['Charles Zhang']
10
+ spec.email = ['gis05zc@163.com']
11
11
 
12
- spec.summary = %q{define very powerful enum class}
13
- spec.homepage = "https://github.com/chinazhangchao/def_enum_helper"
12
+ spec.summary = 'Define very powerful enum class.'
13
+ spec.description = 'A tool help to define very powerful ruby enum class.'
14
+ spec.homepage = 'https://github.com/chinazhangchao/def_enum_helper'
15
+ spec.license = 'MIT'
14
16
 
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"
17
+ spec.files =
18
+ `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
19
22
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
- spec.require_paths = ["lib"]
23
+ spec.require_paths = ['lib']
21
24
 
22
- spec.add_development_dependency "bundler", "~> 1.13"
23
- spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency 'bundler', '~> 1.13'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
24
27
  end
@@ -1,3 +1,3 @@
1
1
  module DefEnumHelper
2
- VERSION = "1.0.0"
2
+ VERSION = '1.0.1'.freeze
3
3
  end
@@ -1,4 +1,4 @@
1
- require "def_enum_helper/version"
1
+ require 'def_enum_helper/version'
2
2
 
3
3
  # Usage
4
4
 
@@ -11,6 +11,7 @@ require "def_enum_helper/version"
11
11
  # y = YinYang[1]
12
12
  # puts y.index # 1
13
13
  # puts y.display # 阳
14
+ # puts y.symbol # :YANG
14
15
  # YinYang[:YANG] # same as YinYang[1]
15
16
  # YinYang["阳"] # same as YinYang[1]
16
17
  # YinYang.all # 所有枚举的struct array [#<struct index=1, display="阳">, # <struct index=2, display="阴">]
@@ -18,7 +19,11 @@ require "def_enum_helper/version"
18
19
  # YinYang.each{|x| puts x} # <struct index=1, display="阳"> #<struct index=2, display="阴">
19
20
  # YinYang.map{|x| x} # [#<struct index=1, display="阳">, #<struct index=2, display="阴">]
20
21
  # puts YinYang.to_hash_array # [{:index=>1, :display=>"阳"}, {:index=>2, :display=>"阴"}]
21
- # puts YingYang.to_json_array # [{"index":1,"display":"阳"},{"index":2,"display":"阴"}]
22
+ # puts YinYang.to_json_array # [{"index":1,"display":"阳"},{"index":2,"display":"阴"}]
23
+ # puts YinYang.keys # [:YANG, :YIN]
24
+ # puts YinYang.indexes # [1, 2]
25
+
26
+ # def_enum :YinYang, {YANG: "阳", YIN: "阴"}, symbol_name: :s, index_name: :i, display_name: :d
22
27
 
23
28
  # # 自定义枚举整数值
24
29
 
@@ -29,6 +34,14 @@ require "def_enum_helper/version"
29
34
 
30
35
  # puts DataOperationType::INSERT # 3
31
36
 
37
+ # def_enum_with_index(:DataOperationType,
38
+ # [:INSERT, 3, '插入'],
39
+ # [:UPDATE, 4, '修改'],
40
+ # [:DELETE, 5, '删除'],
41
+ # symbol_name: :s,
42
+ # index_name: :i,
43
+ # display_name: :d)
44
+
32
45
  # # 自定义枚举对象
33
46
  # CustomStruct = Struct.new(:index, :display, :short_name)
34
47
 
@@ -42,16 +55,20 @@ require "def_enum_helper/version"
42
55
 
43
56
  DEFAULT_ENUM_BASE_INDEX = 1
44
57
 
45
- def def_enum(module_name, enum_hash, enum_base: DEFAULT_ENUM_BASE_INDEX, index_name: :index, display_name: :display)
46
- temp_struct = Struct.new(index_name, display_name)
58
+ def def_enum(module_name, enum_hash, enum_base: DEFAULT_ENUM_BASE_INDEX,
59
+ symbol_name: :symbol, index_name: :index, display_name: :display)
60
+ temp_struct = Struct.new(symbol_name, index_name, display_name)
47
61
  new_enum_hash = {}
48
62
  enum_hash.each do |k, v|
49
- new_enum_hash[k] = temp_struct.new(0, v)
63
+ new_enum_hash[k] = temp_struct.new(k, 0, v)
50
64
  end
51
- def_enum_struct(module_name, new_enum_hash, enum_base: enum_base)
65
+ def_enum_struct(module_name, new_enum_hash,
66
+ enum_base: enum_base, symbol_name: symbol_name,
67
+ index_name: index_name, display_name: display_name)
52
68
  end
53
69
 
54
- def def_enum_struct(module_name, enum_hash, enum_base: DEFAULT_ENUM_BASE_INDEX, index_name: :index, display_name: :display)
70
+ def def_enum_struct(module_name, enum_hash, enum_base: DEFAULT_ENUM_BASE_INDEX,
71
+ symbol_name: :symbol, index_name: :index, display_name: :display)
55
72
  i = 0
56
73
  set_index = (index_name.to_s + '=').to_sym
57
74
  enum_hash.each do |_k, v|
@@ -60,18 +77,21 @@ def def_enum_struct(module_name, enum_hash, enum_base: DEFAULT_ENUM_BASE_INDEX,
60
77
  i += 1
61
78
  end
62
79
 
63
- def_enum_struct_with_index(module_name, enum_hash, index_name: index_name, display_name: display_name)
80
+ def_enum_struct_with_index(module_name, enum_hash,
81
+ symbol_name: symbol_name, index_name: index_name,
82
+ display_name: display_name)
64
83
  end
65
84
 
66
- EnumHelperStruct = Struct.new(:index, :display)
67
-
68
- def def_enum_with_index(module_name, *value_array)
85
+ def def_enum_with_index(module_name, *value_array,
86
+ symbol_name: :symbol, index_name: :index, display_name: :display)
69
87
  h = {}
88
+ temp_struct = Struct.new(symbol_name, index_name, display_name)
70
89
  value_array.each do |variable|
71
- h[variable[0]] = EnumHelperStruct.new(variable[1], variable[2])
90
+ h[variable[0]] = temp_struct.new(variable[0], variable[1], variable[2])
72
91
  end
73
92
 
74
- def_enum_struct_with_index module_name, h
93
+ def_enum_struct_with_index(module_name, h, symbol_name: symbol_name,
94
+ index_name: index_name, display_name: display_name)
75
95
  end
76
96
 
77
97
  def define_iterator_methods(m, enum_index_hash)
@@ -79,23 +99,24 @@ def define_iterator_methods(m, enum_index_hash)
79
99
  m.define_singleton_method(:map) { |&blk| enum_index_hash.values.map { |v| blk.call(v) } }
80
100
 
81
101
  m.define_singleton_method(:all) { enum_index_hash.values }
82
- m.define_singleton_method(:keys) { enum_index_hash.keys }
102
+ m.define_singleton_method(:keys) { m.constants }
103
+ m.define_singleton_method(:indexes) { enum_index_hash.keys }
83
104
  end
84
105
 
85
106
  def define_subscript_method(m, enum_index_hash, enum_display_hash)
86
107
  divisor = enum_index_hash.count
87
108
 
88
- # m.define_singleton_method(:from_number) { |n| (n - DEFAULT_ENUM_BASE_INDEX) % divisor + DEFAULT_ENUM_BASE_INDEX }
89
109
  m.define_singleton_method(:[]) do |k|
90
110
  case k
91
111
  when String
92
112
  enum_display_hash[k]
93
113
  when Symbol
94
- index = if self.is_a?(Module)
95
- const_get(k)
96
- else
97
- Kernel.const_get(k)
98
- end
114
+ index =
115
+ if is_a?(Module)
116
+ const_get(k)
117
+ else
118
+ Kernel.const_get(k)
119
+ end
99
120
  enum_index_hash[index]
100
121
  else
101
122
  enum_index_hash[k]
@@ -123,12 +144,15 @@ def define_enum_methods(m, enum_index_hash, enum_display_hash)
123
144
  define_subscript_method(m, enum_index_hash, enum_display_hash)
124
145
  end
125
146
 
126
- def def_enum_struct_with_index(module_name, enum_hash, index_name: :index, display_name: :display)
147
+ def def_enum_struct_with_index(module_name, enum_hash, symbol_name: :symbol,
148
+ index_name: :index, display_name: :display)
127
149
  m = Module.new
128
150
  enum_index_hash = {}
129
151
  enum_display_hash = {}
130
152
  i = 0
131
153
  enum_hash.each do |k, v|
154
+ v.define_singleton_method(symbol_name) { k } unless v.respond_to?(symbol_name)
155
+
132
156
  enum_value = v.public_send(index_name)
133
157
  m.const_set(k, enum_value)
134
158
  enum_index_hash[enum_value] = v
@@ -138,7 +162,7 @@ def def_enum_struct_with_index(module_name, enum_hash, index_name: :index, displ
138
162
 
139
163
  define_enum_methods(m, enum_index_hash, enum_display_hash)
140
164
 
141
- if self.is_a?(Module)
165
+ if is_a?(Module)
142
166
  const_set(module_name, m)
143
167
  else
144
168
  Kernel.const_set(module_name, m)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: def_enum_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charles Zhang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-30 00:00:00.000000000 Z
11
+ date: 2018-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
- description:
41
+ description: A tool help to define very powerful ruby enum class.
42
42
  email:
43
43
  - gis05zc@163.com
44
44
  executables: []
@@ -46,16 +46,19 @@ extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
48
  - ".gitignore"
49
+ - ".rubocop.yml"
49
50
  - Gemfile
50
51
  - README.md
51
52
  - Rakefile
52
53
  - bin/console
53
54
  - bin/setup
55
+ - check_code.sh
54
56
  - def_enum_helper.gemspec
55
57
  - lib/def_enum_helper.rb
56
58
  - lib/def_enum_helper/version.rb
57
59
  homepage: https://github.com/chinazhangchao/def_enum_helper
58
- licenses: []
60
+ licenses:
61
+ - MIT
59
62
  metadata: {}
60
63
  post_install_message:
61
64
  rdoc_options: []
@@ -73,8 +76,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
76
  version: '0'
74
77
  requirements: []
75
78
  rubyforge_project:
76
- rubygems_version: 2.6.13
79
+ rubygems_version: 2.7.3
77
80
  signing_key:
78
81
  specification_version: 4
79
- summary: define very powerful enum class
82
+ summary: Define very powerful enum class.
80
83
  test_files: []