Module: XsdModel::Elements::BaseElement

Constant Summary collapse

XSD_URI =
'http://www.w3.org/2001/XMLSchema'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AttributeMethods

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/xsd_model/elements/base_element.rb', line 89

def method_missing(name, *args)
  super if name.to_s.end_with? '?'

  if XsdModel::Elements.const_defined? name.camelize # TotalDigits.. :]
    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

#attributesObject

Returns the value of attribute attributes.



12
13
14
# File 'lib/xsd_model/elements/base_element.rb', line 12

def attributes
  @attributes
end

#childrenObject

Returns the value of attribute children.



12
13
14
# File 'lib/xsd_model/elements/base_element.rb', line 12

def children
  @children
end

#namespacesObject

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



75
76
77
78
# File 'lib/xsd_model/elements/base_element.rb', line 75

def ==(other)
  (attributes == other.attributes) &&
    (children == other.children)
end

#basic_xsd_type?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/xsd_model/elements/base_element.rb', line 67

def basic_xsd_type?
  has_type? && type.start_with?("#{xsd_prefix}:")
end

#element_nameObject



59
60
61
# File 'lib/xsd_model/elements/base_element.rb', line 59

def element_name
  self.class.name.demodulize.underscore
end

#empty?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/xsd_model/elements/base_element.rb', line 71

def empty?
  children.empty?
end

#has_custom_type?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/xsd_model/elements/base_element.rb', line 63

def has_custom_type?
  has_type? && !type.start_with?("#{xsd_prefix}:")
end

#initialize(*args) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/xsd_model/elements/base_element.rb', line 15

def initialize(*args)
  hashes, rest = args.partition { |arg| arg.is_a? Hash }

  @children = rest.flatten
  @attributes = hashes[0] || {}
  @namespaces = hashes[1] || {}
end

#name_with_prefixObject



43
44
45
# File 'lib/xsd_model/elements/base_element.rb', line 43

def name_with_prefix
  [xmlns_prefix, name].compact.join(':')
end

#reverse_traverse {|_self, children_result| ... } ⇒ Object

Yields:

  • (_self, children_result)

Yield Parameters:



80
81
82
83
84
85
86
# File 'lib/xsd_model/elements/base_element.rb', line 80

def reverse_traverse(&block)
  children_result = children.map do |child|
    child.reverse_traverse(&block)
  end

  yield self, children_result
end

#type_with_prefixObject



47
48
49
50
51
52
53
# File 'lib/xsd_model/elements/base_element.rb', line 47

def type_with_prefix
  if type&.include? ':'
    type
  else
    [xmlns_prefix, type].compact.join(':')
  end
end

#xmlns_prefixObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/xsd_model/elements/base_element.rb', line 23

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_uriObject



39
40
41
# File 'lib/xsd_model/elements/base_element.rb', line 39

def xmlns_uri
  namespaces['xmlns']
end

#xsd_prefixObject



55
56
57
# File 'lib/xsd_model/elements/base_element.rb', line 55

def xsd_prefix
  namespaces.invert[XSD_URI].gsub('xmlns:', '')
end