Class: Wukong::Datatypes::Enum
Overview
A simple enumerated class
class MyEnum < Enum
enumerates :firefox, :safari, :ie, :chrome, :other
end
MyEnum[1].to_s # => "safari"
Direct Known Subclasses
Instance Attribute Summary collapse
-
#val ⇒ Object
Returns the value of attribute val.
Class Method Summary collapse
-
.[](*args) ⇒ Object
MyEnum is sugar for MyEnum.new(val).
-
.enumerates(*names) ⇒ Object
Use enumerates to set the class’ names.
- .to_pig ⇒ Object
- .to_sql_str ⇒ Object
Instance Method Summary collapse
-
#index(*args) ⇒ Object
returns the value corresponding to that string representation.
-
#initialize(val) ⇒ Enum
constructor
A new instance of Enum.
- #inspect ⇒ Object
- #to_flat ⇒ Object
-
#to_i ⇒ Object
Representations:.
- #to_s ⇒ Object
Constructor Details
#initialize(val) ⇒ Enum
Returns a new instance of Enum.
22 23 24 |
# File 'lib/wukong/datatypes/enum.rb', line 22 def initialize val self.val = val end |
Instance Attribute Details
#val ⇒ Object
Returns the value of attribute val.
20 21 22 |
# File 'lib/wukong/datatypes/enum.rb', line 20 def val @val end |
Class Method Details
.[](*args) ⇒ Object
MyEnum is sugar for MyEnum.new(val)
26 27 28 |
# File 'lib/wukong/datatypes/enum.rb', line 26 def self.[] *args new *args end |
.enumerates(*names) ⇒ Object
Use enumerates to set the class’ names
class MyEnum < Enum
enumerates :firefox, :safari, :ie, :chrome, :other
end
MyEnum[1].to_s # => "safari"
70 71 72 |
# File 'lib/wukong/datatypes/enum.rb', line 70 def self.enumerates *names self.names = names.map(&:to_s) end |
.to_pig ⇒ Object
57 58 59 |
# File 'lib/wukong/datatypes/enum.rb', line 57 def self.to_pig 'chararray' end |
.to_sql_str ⇒ Object
53 54 55 |
# File 'lib/wukong/datatypes/enum.rb', line 53 def self.to_sql_str "ENUM('#{names.join("', '")}')" end |
Instance Method Details
#index(*args) ⇒ Object
returns the value corresponding to that string representation
31 32 33 34 |
# File 'lib/wukong/datatypes/enum.rb', line 31 def index *args # delegate self.class.names.index *args end |
#inspect ⇒ Object
45 46 47 |
# File 'lib/wukong/datatypes/enum.rb', line 45 def inspect "<#{self.class.to_s} #{to_i} (#{to_s})>" end |
#to_flat ⇒ Object
49 50 51 |
# File 'lib/wukong/datatypes/enum.rb', line 49 def to_flat to_s #to_i end |
#to_i ⇒ Object
Representations:
37 38 39 |
# File 'lib/wukong/datatypes/enum.rb', line 37 def to_i val end |
#to_s ⇒ Object
40 41 42 43 |
# File 'lib/wukong/datatypes/enum.rb', line 40 def to_s return nil if val.nil? self.class.names[val] end |