Class: Icss::NamedType

Inherits:
Type
  • Object
show all
Includes:
Validations
Defined in:
lib/icss/type.rb

Overview

Record, enums and fixed are named types. Each has a fullname that is composed of two parts; a name and a namespace. Equality of names is defined on the fullname.

The name portion of a fullname, and record field names must:

  • start with [A-Za-z_]

  • subsequently contain only [A-Za-z0-9_]

  • A namespace is a dot-separated sequence of such names.

References to previously defined names are as in the latter two cases above: if they contain a dot they are a fullname, if they do not contain a dot, the namespace is the namespace of the enclosing definition.

Primitive type names have no namespace and their names may not be defined in any namespace. A schema may only contain multiple definitions of a fullname if the definitions are equivalent.

Direct Known Subclasses

EnumType, FixedType, RecordType

Constant Summary

Constants inherited from Type

Type::PRIMITIVE_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Validations

#validate_name, #validate_namespace

Methods inherited from Type

find, pig_name, primitive?, #primitive?, #title, #to_json

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



129
130
131
# File 'lib/icss/type.rb', line 129

def parent
  @parent
end

Instance Method Details

#fullnameObject

If no explicit namespace is specified, the namespace is taken from the most tightly enclosing schema or protocol. For example, if “name”: “X” is specified, and this occurs within a field of the record definition of org.foo.Y, then the fullname is org.foo.X.



179
180
181
# File 'lib/icss/type.rb', line 179

def fullname
  [namespace, name].reject(&:blank?).join('.')
end

#name=(nm) ⇒ Object

In named types, the namespace and name are determined in one of the following ways:

  • A name and namespace are both specified. For example, one might use “name”: “X”, “namespace”: “org.foo” to indicate the fullname org.foo.X.

  • A fullname is specified. If the name specified contains a dot, then it is assumed to be a fullname, and any namespace also specified is ignored. For example, use “name”: “org.foo.X” to indicate the fullname org.foo.X.

  • A name only is specified, i.e., a name that contains no dots. In this case the namespace is taken from the most tightly enclosing schema or protocol. For example, if “name”: “X” is specified, and this occurs within a field of the record definition of org.foo.Y, then the fullname is org.foo.X.



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/icss/type.rb', line 149

def name= nm
  if nm.include?('.')
    split_name = nm.split('.')
    @use_fullname = true
    @name      = split_name.pop
    @namespace = split_name.join('.')
  else
    @name = nm
  end
  ::Icss::Type::DERIVED_TYPES[@name.to_sym] = self
  @name
end

#namespaceObject



169
170
171
# File 'lib/icss/type.rb', line 169

def namespace
  @namespace || (parent ? parent.namespace : "")
end

#receive_namespace(nmsp) ⇒ Object



162
163
164
165
166
167
# File 'lib/icss/type.rb', line 162

def receive_namespace nmsp
  # If the namespace is given in the name (using a dotted name string) then
  # any namespace also specified is ignored.
  if @namespace then warn "Warning: namespace already set, ignoring" and return ; end
  @namespace = nmsp
end

#to_hashObject



183
184
185
186
187
188
# File 'lib/icss/type.rb', line 183

def to_hash
  hsh = super
  if    @use_fullname then hsh[:name] = fullname
  elsif @namespace    then hsh.merge!( :namespace => @namespace ) ; end
  hsh.merge( :type => self.class.type )
end