Module: Reaction::HasTypes::ClassMethods

Defined in:
lib/reaction/has_types.rb

Instance Method Summary collapse

Instance Method Details

#class_for_type(type) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/reaction/has_types.rb', line 22

def class_for_type(type)
  return type if type.is_a?(Class)
  name = type.to_s.split('_').map(&:capitalize).join
  const_get("#{name}Type")
rescue NameError
  nil
end

#class_for_type!(type) ⇒ Object



30
31
32
# File 'lib/reaction/has_types.rb', line 30

def class_for_type!(type)
  class_for_type(type) or raise ArgumentError.new("Unknown type: #{type}")
end

#cleanup_typesObject



34
35
36
37
38
# File 'lib/reaction/has_types.rb', line 34

def cleanup_types
  types.each do |name, type|
    type.cleanup
  end
end

#set_type(name, type) ⇒ Object



16
17
18
19
20
# File 'lib/reaction/has_types.rb', line 16

def set_type(name, type)
  type ||= Type
  klass = class_for_type!(type)
  types[name.to_sym] = klass.new(name)
end

#type(name) ⇒ Object



8
9
10
# File 'lib/reaction/has_types.rb', line 8

def type(name)
  types[name.to_sym]
end

#typesObject



12
13
14
# File 'lib/reaction/has_types.rb', line 12

def types
  @types ||= {}
end