Module: XsdModel::Elements::BaseElement
- Extended by:
- AttributeMethods
- Included in:
- All, Annotation, Any, AnyAttribute, Appinfo, Attribute, AttributeGroup, Choice, Collection, Comment, ComplexContent, ComplexType, Content, Doctype, Document, Documentation, Element, Enumeration, Extension, FractionDigits, Group, Import, Include, Length, Logical, MaxInclusive, MaxLength, MinExclusive, MinInclusive, MinLength, Pattern, Restriction, Schema, SchemaInfo, Sequence, SimpleContent, SimpleType, Text, TotalDigits, Union, WhiteSpace
- Defined in:
- lib/xsd_model/elements/base_element.rb
Constant Summary
collapse
- XSD_URI =
'http://www.w3.org/2001/XMLSchema'
Instance Attribute Summary collapse
Instance Method Summary
collapse
attribute_method
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
TODO: add similar #respond_to? method
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/xsd_model/elements/base_element.rb', line 88
def method_missing(name, *args)
super if name.to_s.end_with? '?'
if XsdModel::Elements.const_defined? name.camelize
const = XsdModel::Elements.const_get name.camelize
children.select { |child| child.is_a? const }
elsif XsdModel::Elements.const_defined? name.camelize.singularize
const = XsdModel::Elements.const_get name.camelize.singularize
children.select { |child| child.is_a? const }
else
super
end
end
|
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
12
13
14
|
# File 'lib/xsd_model/elements/base_element.rb', line 12
def attributes
@attributes
end
|
#children ⇒ Object
Returns the value of attribute children.
12
13
14
|
# File 'lib/xsd_model/elements/base_element.rb', line 12
def children
@children
end
|
#css_path ⇒ Object
Returns the value of attribute css_path.
12
13
14
|
# File 'lib/xsd_model/elements/base_element.rb', line 12
def css_path
@css_path
end
|
#namespaces ⇒ Object
Returns the value of attribute namespaces.
12
13
14
|
# File 'lib/xsd_model/elements/base_element.rb', line 12
def namespaces
@namespaces
end
|
Instance Method Details
#==(other) ⇒ Object
74
75
76
77
|
# File 'lib/xsd_model/elements/base_element.rb', line 74
def ==(other)
(attributes == other.attributes) &&
(children == other.children)
end
|
#basic_xsd_type? ⇒ Boolean
66
67
68
|
# File 'lib/xsd_model/elements/base_element.rb', line 66
def basic_xsd_type?
has_type? && type.start_with?("#{xsd_prefix}:")
end
|
#element_name ⇒ Object
58
59
60
|
# File 'lib/xsd_model/elements/base_element.rb', line 58
def element_name
self.class.name.demodulize.underscore
end
|
#empty? ⇒ Boolean
70
71
72
|
# File 'lib/xsd_model/elements/base_element.rb', line 70
def empty?
children.empty?
end
|
#has_custom_type? ⇒ Boolean
62
63
64
|
# File 'lib/xsd_model/elements/base_element.rb', line 62
def has_custom_type?
has_type? && !type.start_with?("#{xsd_prefix}:")
end
|
#initialize(*args, attributes: {}, namespaces: {}, css_path: '') ⇒ Object
15
16
17
18
19
20
|
# File 'lib/xsd_model/elements/base_element.rb', line 15
def initialize(*args, attributes: {}, namespaces: {}, css_path: '')
@children = args.flatten
@attributes = attributes
@namespaces = namespaces
@css_path = css_path
end
|
#name_with_prefix ⇒ Object
42
43
44
|
# File 'lib/xsd_model/elements/base_element.rb', line 42
def name_with_prefix
[xmlns_prefix, name].compact.join(':')
end
|
#reverse_traverse {|_self, children_result| ... } ⇒ Object
79
80
81
82
83
84
85
|
# File 'lib/xsd_model/elements/base_element.rb', line 79
def reverse_traverse(&block)
children_result = children.map do |child|
child.reverse_traverse(&block)
end
yield self, children_result
end
|
#type_with_prefix ⇒ Object
46
47
48
49
50
51
52
|
# File 'lib/xsd_model/elements/base_element.rb', line 46
def type_with_prefix
if type&.include? ':'
type
else
[xmlns_prefix, type].compact.join(':')
end
end
|
#xmlns_prefix ⇒ Object
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/xsd_model/elements/base_element.rb', line 22
def xmlns_prefix
nil if xmlns_uri.nil?
ary = namespaces.to_a
candidates = ary.select do |n|
n[1] == xmlns_uri
end.map(&:first)
full_prefix = candidates.find do |c|
c.start_with? 'xmlns:'
end
full_prefix&.gsub('xmlns:', '')
end
|
#xmlns_uri ⇒ Object
38
39
40
|
# File 'lib/xsd_model/elements/base_element.rb', line 38
def xmlns_uri
namespaces['xmlns']
end
|
#xsd_prefix ⇒ Object
54
55
56
|
# File 'lib/xsd_model/elements/base_element.rb', line 54
def xsd_prefix
namespaces.invert[XSD_URI].gsub('xmlns:', '')
end
|