contmx 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/lib/contmx.rb +5 -0
  3. data/lib/writerxml.rb +256 -0
  4. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fb8cd83929fee3966866b73fbc86897a4c5c7e9d
4
+ data.tar.gz: 0d8d6cad41c2103e7d2cd65541b45215db5da404
5
+ SHA512:
6
+ metadata.gz: 09f1fa7c7b03bac1cd2a4f9b271886cf87bb9118d15963b633cf4267302a69255e775859c325ee4725505c32294e330bf0ca51eb572ab907d3daca4d7bd5e660
7
+ data.tar.gz: 024ba58520fe70fdbc134251173213cea41eecfd847845eb32576ecfcd76380354590cffc376717d662ab6f1ecc34e36e75f066151e1bf49fecd44d348f0db9a
data/lib/contmx.rb ADDED
@@ -0,0 +1,5 @@
1
+ class Contmx
2
+ def self.hi
3
+ puts "Hello world!"
4
+ end
5
+ end
data/lib/writerxml.rb ADDED
@@ -0,0 +1,256 @@
1
+ class WriterXML
2
+ attr_reader :attributes, :elements, :schemaLocation, :targetNamespace, :objects;
3
+
4
+ def initialize(hash)
5
+
6
+ end
7
+
8
+ =begin
9
+ Metodo que lee el hash o el arreglo y asigna cada uno de los valores a sus respetivas
10
+ variables
11
+ @param [Hash] [Array]
12
+ =end
13
+ def from_hash(para)
14
+
15
+ if !para.nil?
16
+ @attributes = [] if @attributes.nil?
17
+ @sequence = [] if @sequence.nil?
18
+
19
+ hash_objects = from_hash_to_array_objects(para)
20
+ set_atr_object_from_hash(para,hash_objects)
21
+
22
+ end
23
+
24
+ end
25
+
26
+ =begin
27
+ Metodo que leera cada uno de los atributos y hara un hash de toda la clase
28
+ @return [Hash] crear un hash leyendo las variables
29
+ =end
30
+ def to_hash
31
+ hash = {}
32
+ if !@attributes.nil?
33
+ return hash
34
+ end
35
+ @attributes.each { |attr|
36
+ downcaseAttr = attr.downcase
37
+ if instance_attribute_defined?(downcaseAttr)
38
+ hash[attr.to_sym] = get(downcaseAttr)
39
+ end
40
+ }
41
+ return hash;
42
+ end
43
+
44
+ =begin
45
+ Metodo que obtiene el objecto o el atributos de la clase
46
+ @return [Object] regresa un objecto dependiendo del atributos o nodos
47
+ =end
48
+
49
+ def get(key)
50
+ case key
51
+ when Symbol
52
+ return instance_variable_get("@#{key}")
53
+ when String
54
+ return instance_variable_get("@#{key}")
55
+ end
56
+
57
+ end
58
+
59
+ =begin
60
+ Asignara una instancia basandose en el symbolo que se indica en el parametro
61
+ @param [Symbol, String] , [Object]
62
+ =end
63
+ def set(key, value)
64
+ case key
65
+ when Symbol
66
+ return instance_variable_set("@#{key}", value)
67
+ when String
68
+ return instance_variable_set("@#{key}", value)
69
+ end
70
+ end
71
+
72
+ def [](key)
73
+ return get(key)
74
+ end
75
+
76
+ =begin
77
+ Metodo que revise la existencia del atributos dentro de la clase
78
+ @param [Sysbol, String]
79
+ @return [Boolean]
80
+ =end
81
+ def instance_attribute_defined?(key)
82
+ case key
83
+ when Symbol
84
+ return instance_variable_defined?("@#{key}")
85
+ when String
86
+ return instance_variable_defined?("@#{key}")
87
+ end
88
+ end
89
+
90
+ =begin
91
+ Escribira en el objecto del parametro y revisara si el objecto es raiz o
92
+ es un nodo de una sequencia, si es la raiz agregara un prefijo si esta especificado
93
+ en las variables de instancia
94
+ @param [Nokogiri::XML::Builder ]
95
+ =end
96
+
97
+ def writeXML(xml)
98
+ if @targetNamespace.nil?
99
+ xml.send(self.class.to_s){
100
+ write_attributes_elements(xml)
101
+ }
102
+ else
103
+ prefix = "#{@targetNamespace[:prefix]}"
104
+ namespace = "#{@targetNamespace[:namespace]}"
105
+ xml.send(self.class.to_s){
106
+ ins = xml.parent.add_namespace_definition(prefix, namespace)
107
+ xml.parent.namespace = ins
108
+ write_attributes_elements(xml)
109
+ }
110
+ end
111
+ end
112
+
113
+ #@private
114
+ private
115
+ =begin
116
+ Este metodo coloca todos los atributos y ademas tambien coloca los namespace en
117
+ un hash, es usando para poder usar el metodo de writerXML
118
+ @return [Hash]
119
+ =end
120
+ def attribites_to_hash
121
+ hash = {}
122
+
123
+ if !@targetNamespace.nil?
124
+ prefix = "#{@targetNamespace[:prefix]}"
125
+ namespace = "#{@targetNamespace[:namespace]}"
126
+ hash[prefix] = namespace;
127
+ end
128
+
129
+ if !@attributes.nil?
130
+ @attributes.each { |attr|
131
+ downcaseAttr = attr.downcase
132
+ if instance_attribute_defined?(downcaseAttr)
133
+ hash[attr.to_sym] = get(downcaseAttr)
134
+ end
135
+ }
136
+ end
137
+
138
+ if !@schemaLocation.nil?
139
+ hash["xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance"
140
+ hash["xsi:schemaLocation"] = "#{@schemaLocation}"
141
+ end
142
+ return hash;
143
+ end
144
+ =begin
145
+ Metodo que sirve para escribir los atributos y los nodos en el Nokogiri::XML::Builder
146
+ @param [Nokogiri::XML::Builder ] parametro que se escribira
147
+ =end
148
+
149
+ def write_attributes_elements(xml)
150
+ attributes = attribites_to_hash
151
+ if !attributes.nil?
152
+ attributes.each { |key, value|
153
+ xml.parent.set_attribute(key,value)
154
+ }
155
+ end
156
+ array_elements = elements_to_array_objects
157
+ if !array_elements.empty?
158
+ array_elements.each { |object|
159
+ object.writeXML(xml)
160
+ }
161
+ end
162
+ end
163
+
164
+ =begin
165
+ Metodo para buscar los objecto de la secuencia y instanciarlos en un arreglo donde
166
+ seran utilizado para el metodo writeXML
167
+ @return [Array]
168
+ =end
169
+
170
+ def sequence_to_array_objects
171
+ array = []
172
+ if !@sequence.empty?
173
+ @sequence.each { |element|
174
+ downcaseElement = element.downcase
175
+ if instance_attribute_defined?(downcaseElement)
176
+ object = get(downcaseElement)
177
+ if object.kind_of?(Array)
178
+ return object
179
+ else
180
+ array << object
181
+ end
182
+ end
183
+ }
184
+ else
185
+ if !@objects.nil?
186
+ @objects.each { |o|
187
+ array << o
188
+ }
189
+ end
190
+ end
191
+ return array;
192
+ end
193
+ =begin
194
+ Revisa la secuencia, si la secuencia no esta en vacio reguesa un hash con el nombre
195
+ de las clases, en cambio si esta vacio, asigna variables de objecto a la clase raiz
196
+ @param [Hash] [Array]
197
+ @return [Hash] regresa el nombre de clases con key de la clase en minusculas
198
+ =end
199
+
200
+
201
+ def from_hash_to_array_objects(para)
202
+ hash_objects = {}
203
+ if !@sequence.empty?
204
+ @sequence.each { |o|
205
+ hash_objects[o.downcase.to_sym] = o
206
+ }
207
+ else
208
+ if para.kind_of?(Array)
209
+ @objects= Array.new
210
+ array_hashing = para
211
+ array_hashing.each { | l |
212
+ l.each { |k, v|
213
+ object = Object.const_get(k.to_s).new(v)
214
+ @objects << object
215
+ }
216
+ }
217
+ end
218
+ end
219
+ return hash_objects
220
+ end
221
+ =begin
222
+ Metodo que mapea todo el hash a un asignacion en la clase, se basa los atributos y sequence
223
+ inicializada en la para de arriba
224
+ @param [Hash] es hash es para mapear todos los atributos con sus asignaciones
225
+ @param [Array] es un arreglo con el nombre de los objectos, es como se llaman los objectos
226
+ ya que se inicializara y se asignaran
227
+ =end
228
+ def set_atr_object_from_hash(para, hash_objects)
229
+
230
+ attributes_downcase =@attributes.map(&:downcase)
231
+ elements_downcase =@sequence.map(&:downcase)
232
+
233
+ if para.kind_of? Hash
234
+ para.each { |key, value|
235
+ keyDowncase = key.downcase
236
+ if(attributes_downcase.include?( keyDowncase.to_s))
237
+ set( keyDowncase, value)
238
+ elsif (elements_downcase.include?(keyDowncase.to_s))
239
+ if(value.kind_of?(Hash))
240
+ object = Object.const_get(hash_objects[keyDowncase.to_sym]).new(value)
241
+ set( keyDowncase, object)
242
+ elsif (value.kind_of?(Array))
243
+ objects= Array.new
244
+ value.each { |e|
245
+ object = Object.const_get(hash_objects[keyDowncase.to_sym]).new(value)
246
+ @objects << object
247
+ }
248
+ set( keyDowncase, objects)
249
+ end
250
+ end
251
+ }
252
+
253
+ end
254
+ end
255
+
256
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: contmx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Cesar Castillo Moreno
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-07-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: XML de contabilidad de Mexico
14
+ email: cesar.cast.more@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/contmx.rb
20
+ - lib/writerxml.rb
21
+ homepage: https://github.com/cesarcastmore/contmx
22
+ licenses:
23
+ - MIT
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.4.7
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: XML de Contabilidad de Mexico
45
+ test_files: []
46
+ has_rdoc: