Class: Inum::Base Abstract

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Includes:
Comparable
Defined in:
lib/inum/base.rb

Overview

This class is abstract.

Inum::Base class should be a inheritance of Inum::Base.

Inum base class.

Examples:

class FruitType < Inum::Base
  define :APPLE,  0
  define :BANANA, 1
  define :ORANGE, 2
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label, value) ⇒ Base

Note:

The instance of Enum Member is singleton.

initialize Inum::Base with value.

Parameters:

  • label (Symbol)

    label of Enum.

  • value (Integer)

    value of Enum.


22
23
24
25
26
27
28
29
30
# File 'lib/inum/base.rb', line 22

def initialize(label, value)
  @label            = label.freeze
  @label_string     = label.to_s.freeze
  @label_downcase   = @label_string.downcase.freeze
  @label_upcase     = @label_string.upcase.freeze
  @label_underscore = ActiveSupport::Inflector.underscore(@label_string).freeze
  @value            = value.freeze
  @i18n_namespace   = (self.class.name ? ActiveSupport::Inflector.underscore(self.class.name).tr('/', '.') : '').freeze
end

Class Method Details

.each {|enum| ... } ⇒ Object

Execute the yield(block) with each member of enum.

Yields:

  • (enum)

    execute the block with enums.

Yield Parameters:


124
125
126
# File 'lib/inum/base.rb', line 124

def self.each(&block)
  @enums.each(&block)
end

.form_items(option = {}) ⇒ Array<Array>

Note:

Type of usable with a Rails form helper.

Get items for rails form.

Examples:

f.select :name, Enum.form_items
f.select :name, Enum.form_items(except:[:HOGE])      # Except Enum::HOGE
f.select :name, Enum.form_items(only:[:HOGE, :FUGA]) # Only Enum::HOGE and Enum::FUGA

Parameters:

  • option (Hash) (defaults to: {})

    Options.

Options Hash (option):

  • except (Array<Symbol>)

    Except enum.

  • only (Array<Symbol>)

    Limit enum.

Returns:

  • (Array<Array>)

    collection. ex `[[“HOGE”, 0], [“FUGA”, 1]]`


139
140
141
142
143
144
145
# File 'lib/inum/base.rb', line 139

def self.form_items(option = {})
  map { |e|
    next if option[:except] and option[:except].include?(e.label)
    next if option[:only]   and !option[:only].include?(e.label)
    [e.translate, e.to_s]
  }.compact
end

.i18n_key(underscore_class_path, underscore_label) ⇒ String

This method is abstract.

if change the rule of i18n keys.

Override the rule of i18n keys.

Parameters:

  • underscore_class_path (String)

    underscore class name.

  • underscore_label (String)

    underscore label.

Returns:

  • (String)

    i18n key.


153
154
155
# File 'lib/inum/base.rb', line 153

def self.i18n_key(underscore_class_path, underscore_label)
  "#{underscore_class_path}.#{underscore_label}"
end

.inherited(child) ⇒ Object

Initialize inherited class.


233
234
235
236
237
238
239
# File 'lib/inum/base.rb', line 233

def self.inherited(child)
  if self == Inum::Base
    child.instance_variable_set(:@enums, Array.new)
  else
    child.instance_variable_set(:@enums, self.to_a)
  end
end

.labelsArray<Symbol>

get all labels of Enum.

Returns:

  • (Array<Symbol>)

    all labels of Enum.


160
161
162
# File 'lib/inum/base.rb', line 160

def self.labels
  @enums.map(&:label)
end

.lengthInteger

get Enum length.

Returns:

  • (Integer)

    count of Enums.


167
168
169
# File 'lib/inum/base.rb', line 167

def self.length
  @enums.length
end

.parse(object) ⇒ Inum::Base, Nil

Parse object to Enum.

Parameters:

  • object (Object)

    string or symbol or integer or Inum::Base.

Returns:


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/inum/base.rb', line 175

def self.parse(object)
  case object
  when String
    if /^\d+$/.match(object)
      parse(object.to_i)
    else
      underscore = object.underscore
      find { |e| e.underscore == underscore }
    end
  when Symbol
    parse(object.to_s)
  when Integer
    find { |e| e.value == object }
  when self
    object
  else
    nil
  end
end

.parse!(object) ⇒ Inum::Base

Parse object to Enum.

Parameters:

  • object (Object)

    string or symbol or integer or Inum::Base.

Returns:

Raises:


200
201
202
# File 'lib/inum/base.rb', line 200

def self.parse!(object)
  parse(object) || raise(Inum::NotDefined)
end

.to_aArray<Inum>

return array of Enums.

Returns:

  • (Array<Inum>)

    sorted array of Enums.


207
208
209
# File 'lib/inum/base.rb', line 207

def self.to_a
  @enums.dup
end

.valuesArray<Integer>

get all values of Enum.

Returns:

  • (Array<Integer>)

    all values of Enum.


214
215
216
# File 'lib/inum/base.rb', line 214

def self.values
  @enums.map(&:value)
end

Instance Method Details

#+(value) ⇒ Inum::Base

plus object.

Parameters:

  • value (Integer)

    plus value.(call to_i in this method.)

Returns:


48
49
50
# File 'lib/inum/base.rb', line 48

def + (value)
  self.class.parse(@value + value.to_i)
end

#-(value) ⇒ Inum::Base

minus object.

Parameters:

  • value (Integer)

    plus value.(call to_i in this method.)

Returns:


56
57
58
# File 'lib/inum/base.rb', line 56

def - (value)
  self.class.parse(@value - value.to_i)
end

#<=>(object) ⇒ Integer

Compare object.

Parameters:

  • object (Object)

    parsable object.

Returns:

  • (Integer)

    same normal <=>.


36
37
38
39
40
41
42
# File 'lib/inum/base.rb', line 36

def <=> (object)
  if (other = self.class.parse(object))
    @value <=> other.to_i
  else
    nil
  end
end

#downcaseString

Downcase label.

Returns:

  • (String)

    downcase label.


85
86
87
# File 'lib/inum/base.rb', line 85

def downcase
  @label_downcase
end

#eql?(object) ⇒ Boolean

Compare object.

Parameters:

  • object (Object)

    parsable object.

Returns:

  • (Boolean)

    result.


64
65
66
# File 'lib/inum/base.rb', line 64

def eql?(object)
  self.equal?(self.class.parse(object))
end

#labelSymbol

Label of Enum.

Returns:

  • (Symbol)

    Label of Enum.


71
72
73
# File 'lib/inum/base.rb', line 71

def label
  @label
end

#to_sString

Enum to String.

Returns:

  • (String)

    Label(String).


78
79
80
# File 'lib/inum/base.rb', line 78

def to_s
  @label_string
end

#translateString Also known as: t

Note:

find default `namespace.enum_class_name.enum_label`

Translate Enum to localized string.(use i18n)

Returns:

  • (String)

    localized string of Enum.


107
108
109
# File 'lib/inum/base.rb', line 107

def translate
  I18n.t(self.class.i18n_key(@i18n_namespace, @label_underscore))
end

#underscoreString

Underscore label.

Returns:

  • (String)

    underscore label.


99
100
101
# File 'lib/inum/base.rb', line 99

def underscore
  @label_underscore
end

#upcaseString

Upcase label.

Returns:

  • (String)

    uppercase label.


92
93
94
# File 'lib/inum/base.rb', line 92

def upcase
  @label_upcase
end

#valueInteger Also known as: to_i

Value of Enum.

Returns:

  • (Integer)

    Value of Enum.


115
116
117
# File 'lib/inum/base.rb', line 115

def value
  @value
end