Module: TypeCollection::Base::BaseClassMethods

Defined in:
lib/typecollection/base.rb

Instance Method Summary collapse

Instance Method Details

#all_type_namesObject

Returns all of the known subclass names for this collection



8
# File 'lib/typecollection/base.rb', line 8

def all_type_names(); return __tc_members().keys(); end

#all_typesObject

Returns all of the known subclasses for this collection



10
# File 'lib/typecollection/base.rb', line 10

def all_types(); return __tc_members().values(); end

#generate_type(type) ⇒ Object

Creates the type associated with the provided value (Class or Otherwise)



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/typecollection/base.rb', line 19

def generate_type(type)
  type = type.inferred_type() if (type.kind_of?(Class))
  root = __tc_collection_root()

  name = TypeCollection::NAME_DECOMPOSER.match(root.name)

  new_type = Class.new(root)
  new_type_name = type + name[:constant]

  # Back away.  Just BACK. AWAY.
  #
  # We need to take this new class we've just created here
  # and assign it to a constant in the same namespace as its
  # collection root.  That may be the global namespace.
  # This mess accomplishes that.
  ::Kernel.instance_exec(new_type, &eval("->(klass){ #{name[:namespace]}::#{new_type_name} = klass }"))

  root.__tc_register_member(new_type)
  new_type
end

#get_associated_type(associate, create_if_unknown = TypeCollection.create_unknown_types) ⇒ Object

Get similar type based on the object passed in which can be a String, Object (using the inferred type), or Class



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/typecollection/base.rb', line 41

def get_associated_type(associate, create_if_unknown=TypeCollection.create_unknown_types)
  if (!associate.kind_of?(String))
    if (!associate.kind_of?(Class))
      associate = associate.class
    end
    associate = associate.inferred_type()
  end
  begin
    self.get_type(associate)
  rescue TypeCollection::UnknownChildType => uct
    if create_if_unknown
      self.generate_type(associate)
    else
      raise
    end
  end
end

#get_type(type) ⇒ Object

Gets the type associated with the provided value (Class or Otherwise)



12
13
14
15
16
17
# File 'lib/typecollection/base.rb', line 12

def get_type(type)
  type = type.inferred_type() if (type.kind_of?(Class))
  mems = __tc_members()
  raise TypeCollection::UnknownChildType.new("Unregistered type: #{type}") unless (mems.has_key?(type))
  return mems[type]
end