Class: NcsNavigator::Mdes::VariableType

Inherits:
Object
  • Object
show all
Defined in:
lib/ncs_navigator/mdes/variable_type.rb

Overview

Encapsulates restrictions on the content of a Variable.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil) ⇒ VariableType

Returns a new instance of VariableType.



115
116
117
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 115

def initialize(name=nil)
  @name = name
end

Instance Attribute Details

#base_typeSymbol?

Returns the XML Schema base type that this variable type is based on.

Returns:

  • (Symbol, nil)

    the XML Schema base type that this variable type is based on.



12
13
14
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 12

def base_type
  @base_type
end

#code_listCodeList<CodeListEntry>?

Returns the fixed list of values that are valid for this type.

Returns:



32
33
34
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 32

def code_list
  @code_list
end

#max_lengthFixnum?

Returns the maximum length of a valid value of this type.

Returns:

  • (Fixnum, nil)

    the maximum length of a valid value of this type.



22
23
24
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 22

def max_length
  @max_length
end

#min_lengthFixnum?

Returns the minimum length of a valid value of this type.

Returns:

  • (Fixnum, nil)

    the minimum length of a valid value of this type.



27
28
29
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 27

def min_length
  @min_length
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 7

def name
  @name
end

#patternRegexp?

Returns a regular expression that valid values of this type must match.

Returns:

  • (Regexp, nil)

    a regular expression that valid values of this type must match.



17
18
19
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 17

def pattern
  @pattern
end

#referenceBoolean

Returns whether this is a fully fleshed-out type or just a reference. If it is a reference, all fields except for #name should be ignored.

Returns:

  • (Boolean)

    whether this is a fully fleshed-out type or just a reference. If it is a reference, all fields except for #name should be ignored.



38
39
40
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 38

def reference
  @reference
end

Class Method Details

.from_xsd_simple_type(st, options = {}) ⇒ VariableType

Returns a new instance based on the provided simple type.

Parameters:

  • st (Nokogiri::XML::Element)

    the xs:simpleType element from which to build the instance

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :log (#warn)

    the logger to which to direct warnings

Returns:

  • (VariableType)

    a new instance based on the provided simple type.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 50

def from_xsd_simple_type(st, options={})
  log = options[:log] || NcsNavigator::Mdes.default_logger

  restriction = st.xpath('xs:restriction[@base="xs:string"]', SourceDocuments.xmlns).first
  unless restriction
    log.warn "Unsupported restriction base in simpleType on line #{st.line}"
    return
  end

  new(st['name']).tap do |vt|
    vt.base_type = :string
    restriction.elements.each do |elt|
      case elt.name
      when 'pattern'
        p = elt['value']
        vt.pattern =
          begin
            Regexp.new("^#{p}$")
          rescue RegexpError
            log.warn("Uncompilable pattern #{p.inspect} in simpleType#{(' ' + vt.name.inspect) if vt.name} on line #{elt.line}")
            nil
          end
      when 'maxLength'
        vt.max_length = elt['value'].to_i
      when 'minLength'
        vt.min_length = elt['value'].to_i
      when 'enumeration'
        (vt.code_list ||= CodeList.new) << CodeListEntry.from_xsd_enumeration(elt, options)
        if elt['ncsdoc:desc'] =~ /\S/
          if vt.code_list.description.nil?
            vt.code_list.description = elt['ncsdoc:desc']
          elsif vt.code_list.description != elt['ncsdoc:desc']
            log.warn("Code list entry on line #{elt.line} unexpectedly has a different desc from the first entry")
          end
        end
      else
        log.warn "Unsupported restriction element #{elt.name.inspect} on line #{elt.line}"
      end
    end
  end
end

.reference(name) ⇒ VariableType

Creates an instance that represents a reference with the given name.

Returns:



97
98
99
100
101
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 97

def reference(name)
  new(name).tap do |vt|
    vt.reference = true
  end
end

.xml_schema_type(type_name) ⇒ VariableType

Creates an instance corresponding to the given XML Schema simple base type.

Returns:



108
109
110
111
112
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 108

def xml_schema_type(type_name)
  new.tap do |vt|
    vt.base_type = type_name.to_sym
  end
end

Instance Method Details

#diff(other_type, options = {}) ⇒ Object



157
158
159
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 157

def diff(other_type, options={})
  Differences::Entry.compute(self, other_type, diff_criteria(options), options)
end

#inspectObject



119
120
121
122
123
124
125
126
127
128
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 119

def inspect
  attrs = [
    [:name, name.inspect],
    [:base_type, base_type.inspect],
    [:reference, reference.inspect],
    [:code_list, code_list ? "<#{code_list.size} entries>" : nil]
  ].reject { |k, v| v.nil? }.
    collect { |k, v| "#{k}=#{v}" }
  "#<#{self.class} #{attrs.join(' ')}>"
end

#reference?Boolean

Returns whether this is a fully fleshed-out type or just a reference. If it is a reference, all fields except for #name should be ignored.

Returns:

  • (Boolean)

    whether this is a fully fleshed-out type or just a reference. If it is a reference, all fields except for #name should be ignored.



39
40
41
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 39

def reference
  @reference
end