Class: StixSchemaSpy::Node

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

Direct Known Subclasses

Attribute, Element

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml, schema, containing_type = nil) ⇒ Node

Returns a new instance of Node.



7
8
9
10
11
12
# File 'lib/stix_schema_spy/models/node.rb', line 7

def initialize(xml, schema, containing_type = nil)
  @xml = xml
  @schema = schema
  @containing_type = containing_type
  @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

#containing_typeObject (readonly)

Returns the value of attribute containing_type.



5
6
7
# File 'lib/stix_schema_spy/models/node.rb', line 5

def containing_type
  @containing_type
end

#documentationObject (readonly)

Returns the value of attribute documentation.



5
6
7
# File 'lib/stix_schema_spy/models/node.rb', line 5

def documentation
  @documentation
end

#schemaObject (readonly)

Returns the value of attribute schema.



5
6
7
# File 'lib/stix_schema_spy/models/node.rb', line 5

def schema
  @schema
end

Instance Method Details

#element?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/stix_schema_spy/models/node.rb', line 22

def element?
  !attribute?
end

#inspectObject



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

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


14
15
16
# File 'lib/stix_schema_spy/models/node.rb', line 14

def link
  type.url
end

#nameObject



45
46
47
# File 'lib/stix_schema_spy/models/node.rb', line 45

def name
  @name ||= name!
end

#name!Object



49
50
51
52
53
54
55
# File 'lib/stix_schema_spy/models/node.rb', line 49

def name!
  if reference?
    @xml.attributes['ref'].value.split(':').last
  else
    @xml.attributes['name'].value
  end
end

#reference?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/stix_schema_spy/models/node.rb', line 18

def reference?
  @xml.attributes['ref'] != nil
end

#referenced_elementObject

Only valid if this is a reference. Also works for attributes, this was a crappy name



58
59
60
61
62
63
64
65
66
# File 'lib/stix_schema_spy/models/node.rb', line 58

def referenced_element
  ref = @xml.attributes['ref'].value
  @referenced_element ||= if ref =~ /:/
    prefix, element = ref.split(':')
    schema.find_element(element) || schema.find_attribute("@#{element}") if schema = Schema.find(prefix)
  else
    self.schema.find_element(ref) || self.schema.find_attribute("@#{ref}")
  end
end

#stix_versionObject



72
73
74
# File 'lib/stix_schema_spy/models/node.rb', line 72

def stix_version
  schema.stix_version
end

#typeObject



26
27
28
# File 'lib/stix_schema_spy/models/node.rb', line 26

def type
  @type ||= type!
end

#type!Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/stix_schema_spy/models/node.rb', line 30

def type!
  if reference?
    if referenced_element
      referenced_element.type.use(self)
    else        
      ExternalType.new(*@xml.attributes['ref'].value.split(':')).use(self)
    end
  elsif named_type = @xml.attributes['type']
    type = schema.find_type(named_type.value) || Type.find(named_type.value, nil, stix_version)
    type.use(self)
  else
    Type.inline(@xml, self.schema, self.name).use(self)
  end
end