Class: Wukong::Datatypes::Enum

Inherits:
Object
  • Object
show all
Defined in:
lib/wukong/datatypes/enum.rb

Overview

A simple enumerated class

class MyEnum < Enum
  enumerates :firefox, :safari, :ie, :chrome, :other
end
MyEnum[1].to_s # => "safari"

Direct Known Subclasses

Binned

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#valObject

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_pigObject



57
58
59
# File 'lib/wukong/datatypes/enum.rb', line 57

def self.to_pig
  'chararray'
end

.to_sql_strObject



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

#inspectObject



45
46
47
# File 'lib/wukong/datatypes/enum.rb', line 45

def inspect
  "<#{self.class.to_s} #{to_i} (#{to_s})>"
end

#to_flatObject



49
50
51
# File 'lib/wukong/datatypes/enum.rb', line 49

def to_flat
  to_s #to_i
end

#to_iObject

Representations:



37
38
39
# File 'lib/wukong/datatypes/enum.rb', line 37

def to_i
  val
end

#to_sObject



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