Class: XmlSchemaMapper::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/xml_schema_mapper/element.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xsd_element) ⇒ Element

Returns a new instance of Element.



11
12
13
# File 'lib/xml_schema_mapper/element.rb', line 11

def initialize(xsd_element)
  @xsd = xsd_element
end

Instance Attribute Details

#xsdObject (readonly)

Returns the value of attribute xsd.



7
8
9
# File 'lib/xml_schema_mapper/element.rb', line 7

def xsd
  @xsd
end

Instance Method Details

#accept(visitor, data, *args) ⇒ Object



30
31
32
33
34
35
36
37
# File 'lib/xml_schema_mapper/element.rb', line 30

def accept(visitor, data, *args)
  case data
  when Array
    visitor.visit_array(self, data, *args)
  else
    visitor.visit(self, data, *args)
  end
end

#base_klass_nameObject



80
81
82
83
84
# File 'lib/xml_schema_mapper/element.rb', line 80

def base_klass_name
  base.name.camelize
rescue NoMethodError
  nil
end

#complex?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/xml_schema_mapper/element.rb', line 22

def complex?
  !simple?
end

#content_from(object) ⇒ Object



39
40
41
42
# File 'lib/xml_schema_mapper/element.rb', line 39

def content_from(object)
  data = object.send(reader)
  data.respond_to?(:to_xml) ? data.to_xml : data
end

#converter_classObject



56
57
58
# File 'lib/xml_schema_mapper/element.rb', line 56

def converter_class
  (klass_name + "Converter").safe_constantize
end

#elementsObject



26
27
28
# File 'lib/xml_schema_mapper/element.rb', line 26

def elements
  xsd.elements.values.map { |e| XmlSchemaMapper::Element.new(e) }
end

#klassObject



68
69
70
# File 'lib/xml_schema_mapper/element.rb', line 68

def klass
  klass_name.safe_constantize
end

#klass_nameObject



72
73
74
75
76
77
78
# File 'lib/xml_schema_mapper/element.rb', line 72

def klass_name
  if type.name.nil?
    name.underscore.camelize
  else
    type.name.underscore.camelize
  end
end

#mapper_class(safe = false) ⇒ Object



60
61
62
# File 'lib/xml_schema_mapper/element.rb', line 60

def mapper_class(safe=false)
  safe ? mapper_class_name.safe_constantize : mapper_class_name.constantize
end

#mapper_class_nameObject



64
65
66
# File 'lib/xml_schema_mapper/element.rb', line 64

def mapper_class_name
  klass_name + "Mapper"
end

#method_nameObject



44
45
46
# File 'lib/xml_schema_mapper/element.rb', line 44

def method_name
  name.underscore.to_sym
end

#readerObject



52
53
54
# File 'lib/xml_schema_mapper/element.rb', line 52

def reader
  method_name
end

#simple?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
# File 'lib/xml_schema_mapper/element.rb', line 15

def simple?
  [
      LibXML::XML::Schema::Types::XML_SCHEMA_TYPE_SIMPLE,
      LibXML::XML::Schema::Types::XML_SCHEMA_TYPE_BASIC
  ].include? type.kind
end

#writerObject



48
49
50
# File 'lib/xml_schema_mapper/element.rb', line 48

def writer
  :"#{method_name}="
end