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) ⇒ Object



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

def alias_type(alias_type)
  aliases(alias_type)
end

#aliases(*args) ⇒ Object



22
23
24
25
# File 'lib/mantra/helpers/object_with_type.rb', line 22

def aliases(*args)
  @aliases = [] if @aliases.nil?
  args.empty? ? @aliases : (@aliases.concat(args.map {|a| a.to_sym }))
end

#create(options) ⇒ Object

Raises:



27
28
29
30
31
32
33
34
35
36
# File 'lib/mantra/helpers/object_with_type.rb', line 27

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



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

def find_by_type(type)
  self.subclasses.find { |s| s.type == type || s.aliases.include?(type) }
end

#inherited(subclass) ⇒ Object



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

def inherited(subclass)
  subclasses << subclass
  super
end

#subclassesObject



47
48
49
# File 'lib/mantra/helpers/object_with_type.rb', line 47

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