Class: NcsNavigator::Mdes::VariableType
- Inherits:
-
Object
- Object
- NcsNavigator::Mdes::VariableType
- Defined in:
- lib/ncs_navigator/mdes/variable_type.rb
Overview
Encapsulates restrictions on the content of a Variable.
Instance Attribute Summary collapse
-
#base_type ⇒ Symbol?
The XML Schema base type that this variable type is based on.
-
#code_list ⇒ CodeList<CodeListEntry>?
The fixed list of values that are valid for this type.
-
#max_length ⇒ Fixnum?
The maximum length of a valid value of this type.
-
#min_length ⇒ Fixnum?
The minimum length of a valid value of this type.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#pattern ⇒ Regexp?
A regular expression that valid values of this type must match.
-
#reference ⇒ Boolean
Whether this is a fully fleshed-out type or just a reference.
Class Method Summary collapse
-
.from_xsd_simple_type(st, options = {}) ⇒ VariableType
A new instance based on the provided simple type.
-
.reference(name) ⇒ VariableType
Creates an instance that represents a reference with the given name.
-
.xml_schema_type(type_name) ⇒ VariableType
Creates an instance corresponding to the given XML Schema simple base type.
Instance Method Summary collapse
- #diff(other_type, options = {}) ⇒ Object
-
#initialize(name = nil) ⇒ VariableType
constructor
A new instance of VariableType.
- #inspect ⇒ Object
-
#reference? ⇒ Boolean
Whether this is a fully fleshed-out type or just a reference.
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_type ⇒ Symbol?
Returns 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_list ⇒ CodeList<CodeListEntry>?
Returns the fixed list of values that are valid for this type.
32 33 34 |
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 32 def code_list @code_list end |
#max_length ⇒ Fixnum?
Returns 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_length ⇒ Fixnum?
Returns 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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 7 def name @name end |
#pattern ⇒ Regexp?
Returns 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 |
#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.
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.
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, ={}) log = [: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, ) 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.
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.
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, ={}) Differences::Entry.compute(self, other_type, diff_criteria(), ) end |
#inspect ⇒ Object
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.
39 40 41 |
# File 'lib/ncs_navigator/mdes/variable_type.rb', line 39 def reference @reference end |