Module: Icss::Meta::TypeFactory

Defined in:
lib/icss/type/type_factory.rb

Class Method Summary collapse

Class Method Details

.classify_schema_declaration(schema) ⇒ Object

A Schema is represented by one of:

  • A symbol or string, naming a defined type.

  • A class that responds to .receive, returned as itself

  • A hash (respond_to?(:each_pair), of the form:

    {"type": "typeName" ...attributes...}
    

    where typeName is either a simple or derived type name, as defined in the Icss::Type class

  • An array, representing a union of embedded types.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/icss/type/type_factory.rb', line 77

def self.classify_schema_declaration(schema)
  if schema.respond_to?(:each_pair)
    schema.symbolize_keys!
    type = schema[:type]
  else type = schema
  end
  type = type.to_sym if type.respond_to?(:to_sym)
  # p ['clfy', __FILE__, schema, type]

  # FIXME -- make this match the preamble comment

  if    type.is_a?(Module) && type < NamedType          then return [:is_type,           type]
  elsif ::Icss::SIMPLE_TYPES.include?(type)             then return [:simple,            SIMPLE_TYPES[type]]
  elsif (type == Array) && schema[:items].blank?        then return [:factory,           IdenticalArrayFactory]
  elsif (type == Hash)  && schema[:values].blank?       then return [:factory,           IdenticalHashFactory]
  elsif ::Icss::FACTORY_TYPES.include?(type)            then return [:factory,           FACTORY_TYPES[type]]
  elsif ::Icss::STRUCTURED_SCHEMAS.include?(type)       then return [:structured_schema, STRUCTURED_SCHEMAS[type]]
  elsif (type == :base)                                 then return [:is_type,           schema[:name].camelize.constantize]
  elsif (type == :union) || type.is_a?(Array)           then return [:union_schema,      Icss::Meta::UnionSchema]
  elsif type.is_a?(Symbol) && type.to_s =~ /^[\w\.\:]+/ then return [:named_type,        type]
  elsif type.is_a?(Class) || type.is_a?(Module)         then return [:is_type,           type]
  elsif type.respond_to?(:each_pair)                    then return [:is_type,           receive(type)]
  else  raise ArgumentError, %Q{Can not classify #{schema.inspect}: should be the handle for a named type; one of #{SIMPLE_TYPES.keys.join(',')}; a schema of the form {"type": "typename" ...attributes....}; or an array (representing a union type).}
  end
end

.namespaced_name(nm) ⇒ Object



111
112
113
114
115
# File 'lib/icss/type/type_factory.rb', line 111

def self.namespaced_name(nm)
  nm = nm.to_s
  return nm if (nm == 'thing') || (nm =~ /[\.\/]/)
  [@default_namespace, nm].compact.join('.')
end

.receive(schema) ⇒ Object

A Schema is represented by one of:

  • A symbol or string, naming a defined type.

  • A class that responds to .receive, returned as itself

  • A hash (respond_to?(:each_pair), of the form:

    {"type": "typename" ...attributes...}
    

    where typename is either a simple or derived type name, as defined in the Icss::Type class

  • An array, representing a union of embedded types.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/icss/type/type_factory.rb', line 51

def self.receive schema
  flavor, klass = classify_schema_declaration(schema)
  # p ['tfr', __FILE__, flavor, klass, schema]
  case flavor
  when :simple            then return klass
  when :factory           then return klass
  when :is_type           then return klass
  when :structured_schema then return receive_structured_schema(klass, schema)
  when :union_schema      then return receive_union_schema(klass, schema)
  when :named_type        then return receive_named_type(klass, schema)
  else
  end
end

.with_namespace(def_ns) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/icss/type/type_factory.rb', line 103

def self.with_namespace(def_ns)
  old_def_ns = @default_namespace
  @default_namespace = def_ns
  ret = yield
  @default_namespace = old_def_ns
  ret
end