Top Level Namespace

Defined Under Namespace

Modules: DefEnumHelper Classes: EnumHelperStruct

Constant Summary collapse

DEFAULT_ENUM_BASE_INDEX =

Country # <struct CustomStruct index=1, display=“中华人民共和国”, short_name=“中国”>

1

Instance Method Summary collapse

Instance Method Details

#def_enum(module_name, enum_hash, enum_base: DEFAULT_ENUM_BASE_INDEX, index_name: :index, display_name: :display) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/def_enum_helper.rb', line 44

def def_enum(module_name, enum_hash, enum_base: DEFAULT_ENUM_BASE_INDEX, index_name: :index, display_name: :display)
  temp_struct = Struct.new(index_name, display_name)
  new_enum_hash = {}
  enum_hash.each do |k, v|
    new_enum_hash[k] = temp_struct.new(0, v)
  end
  def_enum_struct(module_name, new_enum_hash, enum_base: enum_base)
end

#def_enum_struct(module_name, enum_hash, enum_base: DEFAULT_ENUM_BASE_INDEX, index_name: :index, display_name: :display) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/def_enum_helper.rb', line 53

def def_enum_struct(module_name, enum_hash, enum_base: DEFAULT_ENUM_BASE_INDEX, index_name: :index, display_name: :display)
  i = 0
  set_index = (index_name.to_s + '=').to_sym
  enum_hash.each do |_k, v|
    enum_value = enum_base + i
    v.public_send(set_index, enum_value)
    i += 1
  end

  def_enum_struct_with_index(module_name, enum_hash, index_name: index_name, display_name: display_name)
end

#def_enum_struct_with_index(module_name, enum_hash, index_name: :index, display_name: :display) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/def_enum_helper.rb', line 124

def def_enum_struct_with_index(module_name, enum_hash, index_name: :index, display_name: :display)
  m = Module.new
  enum_index_hash = {}
  enum_display_hash = {}
  i = 0
  enum_hash.each do |k, v|
    enum_value = v.public_send(index_name)
    m.const_set(k, enum_value)
    enum_index_hash[enum_value] = v
    enum_display_hash[v.public_send(display_name)] = v
    i += 1
  end

  define_enum_methods(m, enum_index_hash, enum_display_hash)

  if self.is_a?(Module)
    const_set(module_name, m)
  else
    Kernel.const_set(module_name, m)
  end
end

#def_enum_with_index(module_name, *value_array) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/def_enum_helper.rb', line 67

def def_enum_with_index(module_name, *value_array)
  h = {}
  value_array.each do |variable|
    h[variable[0]] = EnumHelperStruct.new(variable[1], variable[2])
  end

  def_enum_struct_with_index module_name, h
end

#define_convert_methods(m, enum_index_hash) ⇒ Object



104
105
106
107
108
# File 'lib/def_enum_helper.rb', line 104

def define_convert_methods(m, enum_index_hash)
  m.define_singleton_method(:to_hash_array) { enum_index_hash.each_value.map(&:to_h) }

  m.define_singleton_method(:to_json_array) { to_hash_array.to_json }
end

#define_enum_methods(m, enum_index_hash, enum_display_hash) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/def_enum_helper.rb', line 110

def define_enum_methods(m, enum_index_hash, enum_display_hash)
  define_iterator_methods(m, enum_index_hash)

  m.define_singleton_method(:include?) { |n| n.is_a?(Symbol) ? constants.include?(n) : enum_index_hash.key?(n) }

  m.define_singleton_method(:member?) { |n| include?(n) }

  m.define_singleton_method(:count) { enum_index_hash.count }

  define_convert_methods(m, enum_index_hash)

  define_subscript_method(m, enum_index_hash, enum_display_hash)
end

#define_iterator_methods(m, enum_index_hash) ⇒ Object



76
77
78
79
80
81
# File 'lib/def_enum_helper.rb', line 76

def define_iterator_methods(m, enum_index_hash)
  m.define_singleton_method(:each) { |&blk| enum_index_hash.each_value { |v| blk.call(v) } }
  m.define_singleton_method(:map) { |&blk| enum_index_hash.values.map { |v| blk.call(v) } }

  m.define_singleton_method(:all) { enum_index_hash.values }
end

#define_subscript_method(m, enum_index_hash, enum_display_hash) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/def_enum_helper.rb', line 83

def define_subscript_method(m, enum_index_hash, enum_display_hash)
  divisor = enum_index_hash.count

  # m.define_singleton_method(:from_number) { |n| (n - DEFAULT_ENUM_BASE_INDEX) % divisor + DEFAULT_ENUM_BASE_INDEX }
  m.define_singleton_method(:[]) do |k|
    case k
    when String
      enum_display_hash[k]
    when Symbol
      index = if self.is_a?(Module)
        const_get(k)
      else
        Kernel.const_get(k)
      end
      enum_index_hash[index]
    else
      enum_index_hash[k]
    end
  end
end