Class: Junoser::Xsd::Element

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/junoser/xsd/element.rb

Constant Summary

Constants included from Base

Base::OFFSET

Instance Attribute Summary

Attributes included from Base

#parent, #xml

Instance Method Summary collapse

Methods included from Base

#children, #inspect, #oneliner?, #root?

Constructor Details

#initialize(xml, options = {}) ⇒ Element

Returns a new instance of Element.



12
13
14
15
# File 'lib/junoser/xsd/element.rb', line 12

def initialize(xml, options = {})
  super
  @argument = find_name_element || find_type_attribute
end

Instance Method Details

#configObject



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/junoser/xsd/element.rb', line 17

def config
  @config ||= children.map do |child|
    case child.name
    when 'complexType'
      Junoser::Xsd::ComplexType.new(child, depth: @depth + 1, parent: self)
    when 'simpleType'
      Junoser::Xsd::SimpleType.new(child, depth: @depth + 1, parent: self)
    else
      raise "ERROR: unknown element: #{child.name}"
    end
  end
end

#contentObject



48
49
50
51
52
53
54
# File 'lib/junoser/xsd/element.rb', line 48

def content
  @content ||= if config.empty?
                 xml['type'] ? fmt(OFFSET + xml['type'].underscore) : ''
               else
                 config.map { |c| c.is_a?(String) ? fmt(OFFSET + c) : c.to_s }.join("\n")
               end
end

#to_sObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/junoser/xsd/element.rb', line 30

def to_s
  str = if nokeyword? && content.empty?
          fmt(+'arg')
        elsif nokeyword?
          content
        elsif content.empty?
          fmt(label)
        elsif content =~ /\A *arg\z/
          fmt("#{label} arg")
        elsif label
          fmt("#{label} (", content, ')')
        else
          fmt('(', content, ')')
        end

  oneliner? ? "#{str}.as(:oneline)" : str
end