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



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 # 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

#css_pathObject

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

#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



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

Returns:

  • (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_nameObject



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  children.empty?
end

#has_custom_type?Boolean

Returns:

  • (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_prefixObject



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

Yields:

  • (_self, children_result)

Yield Parameters:



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_prefixObject



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_prefixObject



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_uriObject



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

def xmlns_uri
  namespaces['xmlns']
end

#xsd_prefixObject



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

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