Gem 写了一个简单易用的定义枚举库

charleszhang · 2016年11月23日 · 1715 次阅读

源码:https://github.com/chinazhangchao/def_enum_helper

DefEnumHelper

Define very powerful enum class.

Installation

Add this line to your application's Gemfile:

gem 'def_enum_helper'

And then execute:

$ bundle

Or install it yourself as:

$ gem install def_enum_helper

Usage

# 默认枚举从1开始递增

def_enum :YinYang, {YANG: "阳", YIN: "阴"}

puts YinYang::YANG      # 1
puts YinYang::YIN       # 2
y = YinYang[1]
puts y.index            # 1
puts y.display          # 阳
YinYang[:YANG]          # same as YinYang[1]
YinYang["阳"]          # same as YinYang[1]
YinYang.all             # 所有枚举的struct array [#<struct index=1, display="阳">, # <struct index=2, display="阴">]
puts YinYang.count      # 2
YinYang.each{|x| puts x}  # <struct index=1, display="阳"> #<struct index=2, display="阴">
puts YinYang.to_hash_array # [{:index=>1, :display=>"阳"}, {:index=>2, :display=>"阴"}]
puts YingYang.to_json_array  # [{"index":1,"display":"阳"},{"index":2,"display":"阴"}]
# 自定义枚举整数值

def_enum_with_index(:DataOperationType,
                    [:INSERT, 3, '插入'],
                    [:UPDATE, 4, '修改'],
                    [:DELETE, 5, '删除'])

puts DataOperationType::INSERT  # 3

# 自定义枚举对象
CustomStruct = Struct.new(:index, :display, :short_name)

def_enum_struct_with_index(:Country,
                           {
                             CHINA: CustomStruct.new(1, '中华人民共和国', '中国'),
                             USA: CustomStruct.new(2, '美利坚合众国', '美国')
                         })

Country[:CHINA]         # <struct CustomStruct index=1, display="中华人民共和国", short_name="中国">

License

(MIT License) - Copyright (c) 2016 Charles Zhang

暂无回复。
需要 登录 后方可回复, 如果你还没有账号请 注册新账号