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 class should be a inheritance of Inum::Base.

InumBase 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.



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

def initialize(label, value)
  @label        = label
  @label_string = label.to_s
  @value        = value

  @underscore_class_path = self.class.name ? ActiveSupport::Inflector.underscore(self.class.name).gsub('/', '.') : ''
  @underscore_label      = ActiveSupport::Inflector.underscore(label).gsub('/', '.')
end

Class Method Details

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

Note:

Type of usable with a Rails form helper.

Get collection.

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]]`



110
111
112
113
114
115
116
# File 'lib/inum/base.rb', line 110

def self.collection(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.value]
  }.compact
end

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

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

Yields:

  • (enum)

    execute the block with enums.

Yield Parameters:



122
123
124
# File 'lib/inum/base.rb', line 122

def self.each(&block)
  @enums.each(&block)
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.



132
133
134
# File 'lib/inum/base.rb', line 132

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

.inherited(child) ⇒ Object

Initialize inherited class.



212
213
214
# File 'lib/inum/base.rb', line 212

def self.inherited(child)
  child.instance_variable_set(:@enums, Array.new)
end

.labelsArray<Symbol>

get all labels of Enum.

Returns:

  • (Array<Symbol>)

    all labels of Enum.



139
140
141
# File 'lib/inum/base.rb', line 139

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

.lengthInteger

get Enum length.

Returns:

  • (Integer)

    count of Enums.



146
147
148
# File 'lib/inum/base.rb', line 146

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:



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/inum/base.rb', line 154

def self.parse(object)
  case object
  when String
    if /^\d+$/.match(object)
      parse(object.to_i)
    else
      upcase = object.upcase
      find {|e| e.to_s == upcase}
    end
  when Symbol
    upcase = object.upcase
    find {|e| e.label == upcase}
  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:



180
181
182
# File 'lib/inum/base.rb', line 180

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

.to_aArray<Inum>

return array of Enums.

Returns:

  • (Array<Inum>)

    sorted array of Enums.



187
188
189
# File 'lib/inum/base.rb', line 187

def self.to_a
  @enums.dup
end

.valuesArray<Integer>

get all values of Enum.

Returns:

  • (Array<Integer>)

    all values of Enum.



194
195
196
# File 'lib/inum/base.rb', line 194

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

#eql?(object) ⇒ Boolean

Compare object.

Parameters:

  • parsable (Object)

    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.Classname.EnumMember`

Translate Enum to localized string.(use i18n)

Returns:

  • (String)

    localized string of Enum.



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

def translate
  I18n.t(self.class.i18n_key(@underscore_class_path, @underscore_label))
end

#valueInteger Also known as: to_i

Value of Enum.

Returns:

  • (Integer)

    Value of Enum.



94
95
96
# File 'lib/inum/base.rb', line 94

def value
  @value
end