Module: Enumeration::ClassMethods

Defined in:
lib/zkruby/enum.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



103
104
105
# File 'lib/zkruby/enum.rb', line 103

def method_missing(method,*args)
    @enums[method.downcase] || super
end

Instance Method Details

#enum(name, index, *args) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/zkruby/enum.rb', line 80

def enum(name,index,*args)
    @enums ||= {}
    
    instance = if (self < StandardError) then Class.new(self) else self.new(*args) end
    
    instance.extend(InstanceMethods) if (self < StandardError)

    const_name = if name.nil?
                     "ENUM" + index.to_s.sub("-","_")
                 else
                     name.to_s.upcase
                 end
                    
    self.const_set(const_name.to_s.upcase,instance)
    
    @enums[instance] = instance
    @enums[name] = instance if name
    @enums[index] = instance if index
    instance.instance_variable_set(:@name,name)
    instance.instance_variable_set(:@index,index)
    instance.freeze
end

#fetch(ref) ⇒ Enumeration

Parameters:

  • []

    ref Symbol, Fixnum

Returns:

Raises:

  • (KeyError)

    if ref not found



62
63
64
# File 'lib/zkruby/enum.rb', line 62

def fetch(ref)
    @enums.fetch(ref)
end

#get(ref) ⇒ Enumeration

Returns instance representing ref or nil if not found.

Parameters:

  • []

    ref Symbol, Fixnum or Enumeration

Returns:

  • (Enumeration)

    instance representing ref or nil if not found



55
56
57
# File 'lib/zkruby/enum.rb', line 55

def get(ref)
    @enums[ref]
end

#lookup(ref) ⇒ Object

Will dynamically create a new enum instance if ref is not found



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/zkruby/enum.rb', line 67

def lookup(ref)
    case ref
    when Enumeration,Class 
        ref
    when Symbol
        get(ref) || enum(ref,nil)
    when Fixnum
        get(ref) || enum(nil,ref)
    else
        nil
    end
end