Class: WriterXML

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ WriterXML

Returns a new instance of WriterXML.



4
5
6
# File 'lib/writerxml.rb', line 4

def initialize(hash)

end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



2
3
4
# File 'lib/writerxml.rb', line 2

def attributes
  @attributes
end

#elementsObject (readonly)

Returns the value of attribute elements.



2
3
4
# File 'lib/writerxml.rb', line 2

def elements
  @elements
end

#objectsObject (readonly)

Returns the value of attribute objects.



2
3
4
# File 'lib/writerxml.rb', line 2

def objects
  @objects
end

#schemaLocationObject (readonly)

Returns the value of attribute schemaLocation.



2
3
4
# File 'lib/writerxml.rb', line 2

def schemaLocation
  @schemaLocation
end

#targetNamespaceObject (readonly)

Returns the value of attribute targetNamespace.



2
3
4
# File 'lib/writerxml.rb', line 2

def targetNamespace
  @targetNamespace
end

Instance Method Details

#[](key) ⇒ Object



72
73
74
# File 'lib/writerxml.rb', line 72

def [](key)
  return get(key)
end

#from_hash(para) ⇒ Object

Metodo que lee el hash o el arreglo y asigna cada uno de los valores a sus respetivas variables

Parameters:

  • [Array] (Hash)


13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/writerxml.rb', line 13

def from_hash(para)

  if !para.nil?
    @attributes = [] if @attributes.nil?
    @sequence = [] if @sequence.nil?

    hash_objects = from_hash_to_array_objects(para)
    set_atr_object_from_hash(para,hash_objects)

  end

end

#get(key) ⇒ Object

Metodo que obtiene el objecto o el atributos de la clase

Returns:

  • (Object)

    regresa un objecto dependiendo del atributos o nodos



49
50
51
52
53
54
55
56
57
# File 'lib/writerxml.rb', line 49

def get(key)
  case key
  when Symbol
    return instance_variable_get("@#{key}")
  when String
    return instance_variable_get("@#{key}")
  end

end

#instance_attribute_defined?(key) ⇒ Boolean

Metodo que revise la existencia del atributos dentro de la clase

Parameters:

  • (Sysbol, String)

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
# File 'lib/writerxml.rb', line 81

def instance_attribute_defined?(key)
  case key
  when Symbol
    return instance_variable_defined?("@#{key}")
  when String
    return instance_variable_defined?("@#{key}")
  end
end

#set(key, value) ⇒ Object

Asignara una instancia basandose en el symbolo que se indica en el parametro

Parameters:

  • , (Symbol, String)
    Object


63
64
65
66
67
68
69
70
# File 'lib/writerxml.rb', line 63

def set(key, value)
  case key
  when Symbol
    return instance_variable_set("@#{key}", value)
  when String
    return instance_variable_set("@#{key}", value)
  end
end

#to_hashHash

Metodo que leera cada uno de los atributos y hara un hash de toda la clase

Returns:

  • (Hash)

    crear un hash leyendo las variables



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/writerxml.rb', line 30

def to_hash
  hash = {}
  if !@attributes.nil?
    return hash
  end
  @attributes.each { |attr|
    downcaseAttr =  attr.downcase
    if instance_attribute_defined?(downcaseAttr)
      hash[attr.to_sym] = get(downcaseAttr)
    end
  }
  return hash;
end

#writeXML(xml) ⇒ Object

Escribira en el objecto del parametro y revisara si el objecto es raiz o es un nodo de una sequencia, si es la raiz agregara un prefijo si esta especificado en las variables de instancia

Parameters:

  • (Nokogiri::XML::Builder)


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/writerxml.rb', line 97

def writeXML(xml)
  if @targetNamespace.nil?
    xml.send(self.class.to_s){
      write_attributes_elements(xml)
    }
  else
    prefix = "#{@targetNamespace[:prefix]}"
    namespace = "#{@targetNamespace[:namespace]}"
    xml.send(self.class.to_s){
      ins = xml.parent.add_namespace_definition(prefix, namespace)
      xml.parent.namespace = ins
      write_attributes_elements(xml)
    }
  end
end