Class: Inum::Base Abstract
- Inherits:
-
Object
- Object
- Inum::Base
- Extended by:
- Enumerable
- Includes:
- Comparable
- Defined in:
- lib/inum/base.rb
Overview
Inum class should be a inheritance of Inum::Base.
InumBase class.
Class Method Summary collapse
-
.collection(option = {}) ⇒ Array<Array>
Get collection.
-
.each {|enum| ... } ⇒ Object
Execute the yield(block) with each member of enum.
-
.i18n_key(underscore_class_path, underscore_label) ⇒ String
abstract
Override the rule of i18n keys.
-
.inherited(child) ⇒ Object
Initialize inherited class.
-
.labels ⇒ Array<Symbol>
get all labels of Enum.
-
.length ⇒ Integer
get Enum length.
-
.parse(object) ⇒ Inum::Base, Nil
Parse object to Enum.
-
.parse!(object) ⇒ Inum::Base
Parse object to Enum.
-
.to_a ⇒ Array<Inum>
return array of Enums.
-
.values ⇒ Array<Integer>
get all values of Enum.
Instance Method Summary collapse
-
#+(value) ⇒ Inum::Base
plus object.
-
#-(value) ⇒ Inum::Base
minus object.
-
#<=>(object) ⇒ Integer
Compare object.
-
#eql?(object) ⇒ Boolean
Compare object.
-
#initialize(label, value) ⇒ Base
constructor
initialize Inum::Base with value.
-
#label ⇒ Symbol
Label of Enum.
-
#to_s ⇒ String
Enum to String.
-
#translate ⇒ String
(also: #t)
Translate Enum to localized string.(use i18n).
-
#value ⇒ Integer
(also: #to_i)
Value of Enum.
Constructor Details
#initialize(label, value) ⇒ Base
The instance of Enum Member is singleton.
initialize Inum::Base with value.
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>
Type of usable with a Rails form helper.
Get collection.
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.
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
if change the rule of i18n keys.
Override the rule of i18n keys.
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 |
.labels ⇒ Array<Symbol>
get all labels of Enum.
139 140 141 |
# File 'lib/inum/base.rb', line 139 def self.labels @enums.map(&:label) end |
.length ⇒ Integer
get Enum length.
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.
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.
180 181 182 |
# File 'lib/inum/base.rb', line 180 def self.parse!(object) parse(object) || raise(Inum::NotDefined) end |
.to_a ⇒ Array<Inum>
return array of Enums.
187 188 189 |
# File 'lib/inum/base.rb', line 187 def self.to_a @enums.dup end |
.values ⇒ Array<Integer>
get 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.
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.
56 57 58 |
# File 'lib/inum/base.rb', line 56 def - (value) self.class.parse(@value - value.to_i) end |
#<=>(object) ⇒ Integer
Compare object.
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.
64 65 66 |
# File 'lib/inum/base.rb', line 64 def eql?(object) self.equal?(self.class.parse(object)) end |
#label ⇒ Symbol
Label of Enum.
71 72 73 |
# File 'lib/inum/base.rb', line 71 def label @label end |
#to_s ⇒ String
Enum to String.
78 79 80 |
# File 'lib/inum/base.rb', line 78 def to_s @label_string end |
#translate ⇒ String Also known as: t
find default ‘Namespace.Classname.EnumMember`
Translate Enum to localized string.(use i18n)
86 87 88 |
# File 'lib/inum/base.rb', line 86 def translate I18n.t(self.class.i18n_key(@underscore_class_path, @underscore_label)) end |
#value ⇒ Integer Also known as: to_i
Value of Enum.
94 95 96 |
# File 'lib/inum/base.rb', line 94 def value @value end |