Module: Frenchy::Enum

Defined in:
lib/frenchy/enum.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject

Returns the value of attribute name.



12
13
14
# File 'lib/frenchy/enum.rb', line 12

def name
  @name
end

#tagObject

Returns the value of attribute tag.



12
13
14
# File 'lib/frenchy/enum.rb', line 12

def tag
  @tag
end

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/frenchy/enum.rb', line 3

def self.included(base)
  base.extend(ClassMethods)

  base.class_eval do
    @enums = {}
    @default = nil
  end
end

Instance Method Details

#==(other) ⇒ Object



40
41
42
43
44
45
# File 'lib/frenchy/enum.rb', line 40

def ==(other)
  (other.is_a?(Symbol) && (name == other)) ||
  (other.respond_to?(:to_i) && (other.to_i == tag)) ||
  (other.respond_to?(:to_s) && (other.to_s == to_s || other.to_s == name.to_s)) ||
  super
end

#initialize(attrs = {}) ⇒ Object



14
15
16
17
18
# File 'lib/frenchy/enum.rb', line 14

def initialize(attrs={})
  attrs.each do |k, v|
    send("#{k}=", v) if respond_to?("#{k}=")
  end
end

#inspectObject



20
21
22
# File 'lib/frenchy/enum.rb', line 20

def inspect
  "\#<#{self.class.name}::#{name}=#{tag}>"
end

#to_iObject



24
25
26
# File 'lib/frenchy/enum.rb', line 24

def to_i
  tag
end

#to_sObject



36
37
38
# File 'lib/frenchy/enum.rb', line 36

def to_s
  name.to_s.underscore
end

#to_strObject



28
29
30
# File 'lib/frenchy/enum.rb', line 28

def to_str
  to_s
end

#to_symObject



32
33
34
# File 'lib/frenchy/enum.rb', line 32

def to_sym
  name
end