class StixSchemaSpy::Type

Attributes

documentation[R]
inline[R]
name[R]
schema[R]

Public Class Methods

counter() click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 143
def self.counter
  @counter ||= 0
  @counter += 1
end
find(prefix, name, version) click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 90
def self.find(prefix, name, version)
  if name.nil?
    if prefix.split(':').length == 2
      prefix, name = prefix.split(':')
    else
      name = prefix
      prefix = nil
    end
  end
  
  if schema = Schema.find(prefix, version)
    schema.find_type(name)
  else
    ExternalType.new(prefix, name)
  end
end
inline(xml, schema, name) click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 77
def self.inline(xml, schema, name)
  type = if complex_type = xml.xpath('xs:complexType', {'xs' => 'http://www.w3.org/2001/XMLSchema'}).first
    ComplexType.new(complex_type, schema, name)
  elsif simple_type = xml.xpath('xs:simpleType', {'xs' => 'http://www.w3.org/2001/XMLSchema'}).first
    SimpleType.new(simple_type, schema, name)
  else
    $logger.warn "Unable to find type for #{xml.attributes['name'].value}" if defined?($logger)
    ExternalType.new("", "")
  end
  schema.types[type.name] = type
  type
end
new(xml, schema, inline = false) click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 8
def initialize(xml, schema, inline = false)
  @inline = !!inline
  @schema = schema
  @xml = xml
  @name = xml.attributes['name'] ? xml.attributes['name'].value : "#{inline}InlineType"
  @documentation = xml.xpath('./xs:annotation/xs:documentation', {'xs' => 'http://www.w3.org/2001/XMLSchema'}).to_a.map {|node| node.text}.join("\n")      
end

Public Instance Methods

abstract?() click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 35
def abstract?
  !!(@xml.attributes['abstract'] && @xml.attributes['abstract'].value == "true")
end
ancestors() click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 39
def ancestors
  if parent_type
    [parent_type] + (parent_type.respond_to?(:ancestors) ? parent_type.ancestors : [])
  else
    []
  end
end
child_types() click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 131
def child_types
  (@child_types || []).uniq
end
doc_path() click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 68
def doc_path
  @schema.doc_path ? @schema.doc_path + "##{name}" : nil
end
example(configuration) click to toggle source

This may be overriden by classes that inherit this. Otherwise it just renders the template

# File lib/stix_schema_spy/models/type.rb, line 64
def example(configuration)
  ERB.new(File.read("views/examples/#{prefix}/#{name}.erb")).result(configuration.get_binding)
end
full_name() click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 16
def full_name
  @inline ? name : "#{prefix}:#{name}"
end
get_extension(type) click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 20
def get_extension(type)
  if node = type.xpath('xs:complexContent/xs:extension | xs:simpleContent/xs:extension | xs:complexContent/xs:restriction | xs:simpleContent/xs:restriction', {'xs' => 'http://www.w3.org/2001/XMLSchema'}).first
    base = node.attributes['base'].value
    parent = schema.find_type(base) || Type.find(base, nil, stix_version)
    if parent.nil?
      puts "Unable to find base type #{base} for extended type #{full_name}"
      return false
    else
      return parent.use_parent(self)
    end
  else
    false
  end
end
has_example?() click to toggle source

This may be overriden by classes that inherit this. Otherwise it just checks to see if a template exists

# File lib/stix_schema_spy/models/type.rb, line 59
def has_example?
  File.exists?("views/examples/#{prefix}/#{name}.erb")
end
has_own_fields?() click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 135
def has_own_fields?
  fields.length > 0 && (parent_type.nil? || parent_type.fields.length != fields.length)
end
inspect() click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 152
def inspect
  "#<#{self.class.to_s}:#{object_id} @name=\"#{full_name}\">"
end
own_usages() click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 113
def own_usages
  @usages || []
end
parent_type() click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 47
def parent_type
  @extension = get_extension(@xml) if @extension.nil?
  return @extension
end
prefix() click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 139
def prefix
  @schema.prefix
end
stix_version() click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 148
def stix_version
  schema.stix_version
end
url() click to toggle source

URL is just the prefix/type TODO: This should really go in a helper.…

# File lib/stix_schema_spy/models/type.rb, line 54
def url
  "#{prefix}/#{name}"
end
usages() click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 117
def usages
  if parent_type
    (own_usages + parent_type.usages).flatten.uniq
  else
    own_usages.uniq
  end
end
use(by) click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 107
def use(by)
  @usages ||= []
  @usages.push(by)
  return self
end
use_parent(child) click to toggle source
# File lib/stix_schema_spy/models/type.rb, line 125
def use_parent(child)
  @child_types ||= []
  @child_types << child
  self
end
vocab?() click to toggle source

Returns whether or not this type is a vocabulary

# File lib/stix_schema_spy/models/type.rb, line 73
def vocab?
  @is_vocab ||= full_name =~ /Vocab-\d\.\d$/
end