Module: Mantra::Helpers::ObjectWithType::ClassMethods

Defined in:
lib/mantra/helpers/object_with_type.rb

Instance Method Summary collapse

Instance Method Details

#alias_type(alias_type = nil) ⇒ Object



18
19
20
# File 'lib/mantra/helpers/object_with_type.rb', line 18

def alias_type(alias_type=nil)
  alias_type.nil? ? @alias_type : (@alias_type = alias_type.to_sym)
end

#create(options) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
30
31
# File 'lib/mantra/helpers/object_with_type.rb', line 22

def create(options)
  type = options[:type] || options["type"]
  raise UnspecifiedType.new("options hash should contain type") if type.nil?
  subclass = self.find_by_type(type.to_sym)
  if subclass.nil?
    raise UnknownType.new("unknown type #{type}")
  else
    subclass.new(options)
  end
end

#find_by_type(type) ⇒ Object



33
34
35
# File 'lib/mantra/helpers/object_with_type.rb', line 33

def find_by_type(type)
  self.subclasses.find { |s| s.type == type || s.alias_type == type }
end

#inherited(subclass) ⇒ Object



37
38
39
40
# File 'lib/mantra/helpers/object_with_type.rb', line 37

def inherited(subclass)
  subclasses << subclass
  super
end

#subclassesObject



42
43
44
# File 'lib/mantra/helpers/object_with_type.rb', line 42

def subclasses
  @subclasses ||= []
end

#type(type = nil) ⇒ Object



14
15
16
# File 'lib/mantra/helpers/object_with_type.rb', line 14

def type(type=nil)
  type.nil? ? @type : (@type = type.to_sym)
end