Class: StixSchemaSpy::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/stix_schema_spy/models/type.rb

Direct Known Subclasses

ComplexType, SimpleType

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml, schema, inline = false) ⇒ Type

Returns a new instance of Type.



8
9
10
11
12
13
14
# 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

Instance Attribute Details

#documentationObject (readonly)

Returns the value of attribute documentation.



6
7
8
# File 'lib/stix_schema_spy/models/type.rb', line 6

def documentation
  @documentation
end

#inlineObject (readonly)

Returns the value of attribute inline.



6
7
8
# File 'lib/stix_schema_spy/models/type.rb', line 6

def inline
  @inline
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/stix_schema_spy/models/type.rb', line 6

def name
  @name
end

#schemaObject (readonly)

Returns the value of attribute schema.



6
7
8
# File 'lib/stix_schema_spy/models/type.rb', line 6

def schema
  @schema
end

Class Method Details

.counterObject



143
144
145
146
# File 'lib/stix_schema_spy/models/type.rb', line 143

def self.counter
  @counter ||= 0
  @counter += 1
end

.find(prefix, name, version) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# 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

Instance Method Details

#abstract?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/stix_schema_spy/models/type.rb', line 35

def abstract?
  !!(@xml.attributes['abstract'] && @xml.attributes['abstract'].value == "true")
end

#ancestorsObject



39
40
41
42
43
44
45
# 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_typesObject



131
132
133
# File 'lib/stix_schema_spy/models/type.rb', line 131

def child_types
  (@child_types || []).uniq
end

#doc_pathObject



68
69
70
# File 'lib/stix_schema_spy/models/type.rb', line 68

def doc_path
  @schema.doc_path ? @schema.doc_path + "##{name}" : nil
end

#example(configuration) ⇒ Object

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



64
65
66
# 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_nameObject



16
17
18
# File 'lib/stix_schema_spy/models/type.rb', line 16

def full_name
  @inline ? name : "#{prefix}:#{name}"
end

#get_extension(type) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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?Boolean

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

Returns:

  • (Boolean)


59
60
61
# File 'lib/stix_schema_spy/models/type.rb', line 59

def has_example?
  File.exists?("views/examples/#{prefix}/#{name}.erb")
end

#has_own_fields?Boolean

Returns:

  • (Boolean)


135
136
137
# 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

#inspectObject



152
153
154
# File 'lib/stix_schema_spy/models/type.rb', line 152

def inspect
  "#<#{self.class.to_s}:#{object_id} @name=\"#{full_name}\">"
end

#own_usagesObject



113
114
115
# File 'lib/stix_schema_spy/models/type.rb', line 113

def own_usages
  @usages || []
end

#parent_typeObject



47
48
49
50
# File 'lib/stix_schema_spy/models/type.rb', line 47

def parent_type
  @extension = get_extension(@xml) if @extension.nil?
  return @extension
end

#prefixObject



139
140
141
# File 'lib/stix_schema_spy/models/type.rb', line 139

def prefix
  @schema.prefix
end

#stix_versionObject



148
149
150
# File 'lib/stix_schema_spy/models/type.rb', line 148

def stix_version
  schema.stix_version
end

#urlObject

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



54
55
56
# File 'lib/stix_schema_spy/models/type.rb', line 54

def url
  "#{prefix}/#{name}"
end

#usagesObject



117
118
119
120
121
122
123
# 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) ⇒ Object



107
108
109
110
111
# File 'lib/stix_schema_spy/models/type.rb', line 107

def use(by)
  @usages ||= []
  @usages.push(by)
  return self
end

#use_parent(child) ⇒ Object



125
126
127
128
129
# File 'lib/stix_schema_spy/models/type.rb', line 125

def use_parent(child)
  @child_types ||= []
  @child_types << child
  self
end

#vocab?Boolean

Returns whether or not this type is a vocabulary

Returns:

  • (Boolean)


73
74
75
# File 'lib/stix_schema_spy/models/type.rb', line 73

def vocab?
  @is_vocab ||= full_name =~ /Vocab-\d\.\d$/
end