Module: Enumerative::Enumeration

Defined in:
lib/enumerative/enumeration.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(other_module) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/enumerative/enumeration.rb', line 5

def self.included( other_module )
  other_module.module_eval do
    valid_keys.each do |k|
      const_set k.gsub( /\s/, '_' ).upcase, other_module.new( k )
    end

    extend ClassMethods

    attr_reader :key
  end
end

Instance Method Details

#==(other) ⇒ Object



40
41
42
# File 'lib/enumerative/enumeration.rb', line 40

def ==( other )
  other.is_a?( self.class ) && (other.key == key)
end

#blank?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/enumerative/enumeration.rb', line 56

def blank?
  key.blank?
end

#initialize(key_or_hash) ⇒ Object



33
34
35
36
37
38
# File 'lib/enumerative/enumeration.rb', line 33

def initialize( key_or_hash )
  key = key_or_hash.is_a?( Hash ) ?
          (key_or_hash['key'] || key_or_hash[:key]) :
          key_or_hash
  @key = key.to_s.try( :dup ).try( :freeze )
end

#marked_for_destruction?Boolean

Method is required for Rails > 3.2.x due to ActiveRecord’s autosave functionality.

Returns:

  • (Boolean)


63
64
65
# File 'lib/enumerative/enumeration.rb', line 63

def marked_for_destruction?
  false
end

#to_sObject



44
45
46
# File 'lib/enumerative/enumeration.rb', line 44

def to_s
  key
end

#valid?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/enumerative/enumeration.rb', line 48

def valid?
  key.blank? || self.class.valid_keys.include?( key )
end

#valueObject



52
53
54
# File 'lib/enumerative/enumeration.rb', line 52

def value
  key.blank? ? nil : self.class.translate( key )
end