Module: EnumeratedType::ClassMethods

Defined in:
lib/enumerated_type.rb

Instance Method Summary collapse

Instance Method Details

#[](name) ⇒ Object



115
116
117
# File 'lib/enumerated_type.rb', line 115

def [](name)
  by(:name, name)
end

#by(property, value, &miss) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/enumerated_type.rb', line 105

def by(property, value, &miss)
  miss ||= Proc.new { |v| raise(ArgumentError, "Could not find #{self.name} with ##{property} == #{v.inspect}'") }

  if @by_cache.has_property?(property)
    @by_cache.get(property, value, miss)
  else
    find { |e| e.send(property) == value } || miss.call(value)
  end
end

#coerce(coercable) ⇒ Object



123
124
125
126
127
128
129
130
131
# File 'lib/enumerated_type.rb', line 123

def coerce(coercable)
  case
  when coercable.class == self then coercable
  when coercable.respond_to?(:to_sym) then self[coercable.to_sym]
  when coercable.respond_to?(:to_str) then self[coercable.to_str.to_sym]
  else
    raise TypeError, "#{coercable.inspect} cannot be coerced into a #{self.name}"
  end
end

#each(&block) ⇒ Object



101
102
103
# File 'lib/enumerated_type.rb', line 101

def each(&block)
  @all.each(&block)
end

#recognized?(name) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/enumerated_type.rb', line 119

def recognized?(name)
  map(&:name).include?(name)
end