module XsdModel::Elements::BaseElement

Constants

XSD_URI

Attributes

attributes[RW]
children[RW]
css_path[RW]
namespaces[RW]

Public Class Methods

new(*args, attributes: {}, namespaces: {}, css_path: '') click to toggle source
# 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

Public Instance Methods

==(other) click to toggle source
# File lib/xsd_model/elements/base_element.rb, line 74
def ==(other)
  (attributes == other.attributes) &&
    (children == other.children)
end
basic_xsd_type?() click to toggle source
# File lib/xsd_model/elements/base_element.rb, line 66
def basic_xsd_type?
  has_type? && type.start_with?("#{xsd_prefix}:")
end
element_name() click to toggle source
# File lib/xsd_model/elements/base_element.rb, line 58
def element_name
  self.class.name.demodulize.underscore
end
empty?() click to toggle source
# File lib/xsd_model/elements/base_element.rb, line 70
def empty?
  children.empty?
end
has_custom_type?() click to toggle source
# File lib/xsd_model/elements/base_element.rb, line 62
def has_custom_type?
  has_type? && !type.start_with?("#{xsd_prefix}:")
end
method_missing(name, *args) click to toggle source

TODO: add similar respond_to? method

Calls superclass method
# 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
name_with_prefix() click to toggle source
# 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| ... } click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/xsd_model/elements/base_element.rb, line 38
def xmlns_uri
  namespaces['xmlns']
end
xsd_prefix() click to toggle source
# File lib/xsd_model/elements/base_element.rb, line 54
def xsd_prefix
  namespaces.invert[XSD_URI].gsub('xmlns:', '')
end