Class: Inum::Base Abstract
- Inherits:
-
Object
- Object
- Inum::Base
- Extended by:
- Enumerable
- Includes:
- Comparable
- Defined in:
- lib/inum/base.rb
Overview
Inum::Base class should be a inheritance of Inum::Base.
Inum base class.
Class Method Summary collapse
-
.each {|enum| ... } ⇒ Object
Execute the yield(block) with each member of enum.
-
.form_items(option = {}) ⇒ Array<Array>
Get items for rails form.
-
.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.
-
#downcase ⇒ String
Downcase label.
-
#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).
-
#underscore ⇒ String
Underscore label.
-
#upcase ⇒ String
Upcase label.
-
#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.
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.
124 125 126 |
# File 'lib/inum/base.rb', line 124 def self.each(&block) @enums.each(&block) end |
.form_items(option = {}) ⇒ Array<Array>
Type of usable with a Rails form helper.
Get items for rails form.
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
if change the rule of i18n keys.
Override the rule of i18n keys.
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 |
.labels ⇒ Array<Symbol>
get all labels of Enum.
160 161 162 |
# File 'lib/inum/base.rb', line 160 def self.labels @enums.map(&:label) end |
.length ⇒ Integer
get Enum length.
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.
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.
200 201 202 |
# File 'lib/inum/base.rb', line 200 def self.parse!(object) parse(object) || raise(Inum::NotDefined) end |
.to_a ⇒ Array<Inum>
return array of Enums.
207 208 209 |
# File 'lib/inum/base.rb', line 207 def self.to_a @enums.dup end |
.values ⇒ Array<Integer>
get 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.
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 |
#downcase ⇒ String
Downcase label.
85 86 87 |
# File 'lib/inum/base.rb', line 85 def downcase @label_downcase 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.enum_class_name.enum_label`
Translate Enum to localized string.(use i18n)
107 108 109 |
# File 'lib/inum/base.rb', line 107 def translate I18n.t(self.class.i18n_key(@i18n_namespace, @label_underscore)) end |
#underscore ⇒ String
Underscore label.
99 100 101 |
# File 'lib/inum/base.rb', line 99 def underscore @label_underscore end |
#upcase ⇒ String
Upcase label.
92 93 94 |
# File 'lib/inum/base.rb', line 92 def upcase @label_upcase end |
#value ⇒ Integer Also known as: to_i
Value of Enum.
115 116 117 |
# File 'lib/inum/base.rb', line 115 def value @value end |