contmx 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/writerxml.rb +348 -0
  3. metadata +6 -5
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b6d9717daec1233d818ecf535095e81371fc53fc
4
- data.tar.gz: 1180a90e2e05a971b3737403e357eb2464bfaba6
3
+ metadata.gz: 206ce53c6b0568d6226728ab73b67d4987172f77
4
+ data.tar.gz: d8696f144166ec0352d3544bafed117a3d46f9b0
5
5
  SHA512:
6
- metadata.gz: 711039dfea1c3147dbbcf2d75301874bd6e6b20a8dda444b2e22364da27ed0a6f313192d92f132e000c176eae87c02b0c2f8e7c2caa08f287cdb831f57fd7619
7
- data.tar.gz: 7fb0872e35947062287dae413b4f07174ed6546853192c3a7f8652030d803dc6013206632abda01e66b47d8b8cc638548651ed7102f6e9a07afe09d523dd184b
6
+ metadata.gz: ea25e912003c25dce6c2f96ad3289127b83c24a6d758944a16affb022f136759cf1706b0f63f82efb063ab6b303b8b898007551d6391e1ba5c128b65f3a46eec
7
+ data.tar.gz: 3bf2d2df626891640dde7349064fc79203be89f2530e5a277024f7b574de5fc81cb3da845ed4ef634f63df077b50a9cb2155c422f513c2ad632169db70535f2f
data/lib/writerxml.rb ADDED
@@ -0,0 +1,348 @@
1
+ class WriterXML
2
+ attr_reader :attributes, :elements, :schemaLocation, :targetNamespace, :objects;
3
+
4
+ def initialize(para)
5
+ end
6
+
7
+
8
+ =begin
9
+ Metodo que inicializa la clase
10
+ =end
11
+ def init(para=nil)
12
+
13
+ #JSON.parse(s,:symbolize_names => true)
14
+ @attributes = Array.new if @attributes.nil?
15
+ @sequence = Array.new if @sequence.nil?
16
+
17
+ set_objects_to_acessor
18
+ set_attributes_to_reader
19
+ if para.kind_of? Hash
20
+ from_hash(para)
21
+ end
22
+ end
23
+
24
+ =begin
25
+ Metodo que actualiza los parametros despues de hacer el new
26
+ =end
27
+ def update(para)
28
+ if para.kind_of? Hash
29
+ from_hash(para)
30
+ end
31
+
32
+ end
33
+
34
+ =begin
35
+ Metodo que lee el hash o el arreglo y asigna cada uno de los valores a sus respetivas
36
+ variables
37
+ @param [Hash] [Array]
38
+ =end
39
+ def from_hash(para)
40
+ hash_objects = from_hash_to_array_objects(para)
41
+ set_atr_object_from_hash(para,hash_objects)
42
+ end
43
+
44
+ =begin
45
+ Metodo para colocar los objecto como attr_acessor
46
+ =end
47
+
48
+ def set_objects_to_acessor
49
+ if !@sequence.empty?
50
+ @sequence.each { |ele|
51
+ self.class.__send__(:attr_accessor, ele.downcase)
52
+ }
53
+ end
54
+
55
+ end
56
+
57
+
58
+ =begin
59
+ Metodo para colocar los atributos como attr_reader
60
+ =end
61
+
62
+ def set_attributes_to_reader
63
+ if !@attributes.empty?
64
+ @attributes.each { |ele|
65
+ self.class.__send__(:attr_reader, ele.downcase)
66
+ }
67
+ end
68
+
69
+ end
70
+
71
+
72
+ =begin
73
+ Metodo que leera cada uno de los atributos y hara un hash de toda la clase
74
+ @return [Hash] crear un hash leyendo las variables
75
+ =end
76
+ def to_hash
77
+ hash = {}
78
+ if !@attributes.nil?
79
+ return hash
80
+ end
81
+ @attributes.each { |attr|
82
+ downcaseAttr = attr.downcase
83
+ if instance_attribute_defined?(downcaseAttr)
84
+ hash[attr.to_sym] = get(downcaseAttr)
85
+ end
86
+ }
87
+ return hash;
88
+ end
89
+
90
+ =begin
91
+ Metodo que obtiene el objecto o el atributos de la clase
92
+ @return [Object] regresa un objecto dependiendo del atributos o nodos
93
+ =end
94
+
95
+ def get(key)
96
+ case key
97
+ when Symbol
98
+ return instance_variable_get("@#{key}")
99
+ when String
100
+ return instance_variable_get("@#{key}")
101
+ end
102
+
103
+ end
104
+
105
+ =begin
106
+ Asignara una instancia basandose en el symbolo que se indica en el parametro
107
+ @param [Symbol, String] , [Object]
108
+ =end
109
+ def set(key, value)
110
+ case key
111
+ when Symbol
112
+ return instance_variable_set("@#{key}", value)
113
+ when String
114
+ return instance_variable_set("@#{key}", value)
115
+ end
116
+ end
117
+
118
+ =begin
119
+ Metodo para obtener los atributos del objecto
120
+ =end
121
+
122
+ def [](key)
123
+ @attributes = Array.new if @attributes.nil?
124
+ if @attributes.include? key.downcase
125
+ return get(key)
126
+ else
127
+ raise 'El atributo no ha sido encontrado en el objecto'
128
+ end
129
+ end
130
+
131
+ =begin
132
+ Metodo para escribir atributos en el objecto
133
+ =end
134
+ def []= (key, value)
135
+ key.downcase!
136
+ attributes_downcase =@attributes.map(&:downcase)
137
+ if attributes_downcase.include? key
138
+ set(key, value)
139
+ else
140
+ raise 'El atributo '+ key + ' no ha sido encontrado'
141
+ end
142
+ end
143
+
144
+ =begin
145
+ Metodo que revise la existencia del atributos dentro de la clase
146
+ @param [Sysbol, String]
147
+ @return [Boolean]
148
+ =end
149
+ def instance_attribute_defined?(key)
150
+ case key
151
+ when Symbol
152
+ return instance_variable_defined?("@#{key}")
153
+ when String
154
+ return instance_variable_defined?("@#{key}")
155
+ end
156
+ end
157
+
158
+ =begin
159
+ Escribira en el objecto del parametro y revisara si el objecto es raiz o
160
+ es un nodo de una sequencia, si es la raiz agregara un prefijo si esta especificado
161
+ en las variables de instancia
162
+ @param [Nokogiri::XML::Builder ]
163
+ =end
164
+
165
+ def writeXML(xml)
166
+ if @targetNamespace.nil?
167
+ xml.send(self.class.to_s){
168
+ write_attributes_elements(xml)
169
+ }
170
+ else
171
+ prefix = "#{@targetNamespace[:prefix]}"
172
+ namespace = "#{@targetNamespace[:namespace]}"
173
+ xml.send(self.class.to_s){
174
+ ins = xml.parent.add_namespace_definition(prefix, namespace)
175
+ xml.parent.namespace = ins
176
+ write_attributes_elements(xml)
177
+ }
178
+ end
179
+ end
180
+
181
+ #@private
182
+ private
183
+ =begin
184
+ Este metodo coloca todos los atributos y ademas tambien coloca los namespace en
185
+ un hash, es usando para poder usar el metodo de writerXML
186
+ @return [Hash]
187
+ =end
188
+ def attribites_to_hash
189
+ hash = {}
190
+ if !@attributes.nil?
191
+ @attributes.each { |attr|
192
+ downcaseAttr = attr.downcase
193
+ if instance_attribute_defined?(downcaseAttr)
194
+ hash[attr.to_sym] = get(downcaseAttr)
195
+ end
196
+ }
197
+ end
198
+
199
+ if !@schemaLocation.nil?
200
+ hash["xmlns:xsi"] = "http://www.w3.org/2001/XMLSchema-instance"
201
+ hash["xsi:schemaLocation"] = "#{@schemaLocation}"
202
+ end
203
+ return hash;
204
+ end
205
+ =begin
206
+ Metodo que sirve para escribir los atributos y los nodos en el Nokogiri::XML::Builder
207
+ @param [Nokogiri::XML::Builder ] parametro que se escribira
208
+ =end
209
+
210
+ def write_attributes_elements(xml)
211
+ attributes = attribites_to_hash
212
+ if !attributes.nil?
213
+ attributes.each { |key, value|
214
+ xml.parent.set_attribute(key,value)
215
+ }
216
+ end
217
+ array_elements = sequence_to_array_objects
218
+ if !array_elements.empty?
219
+ array_elements.each { |object|
220
+ object.writeXML(xml)
221
+ }
222
+ end
223
+ end
224
+
225
+ =begin
226
+ Metodo para buscar los objecto de la secuencia y instanciarlos en un arreglo donde
227
+ seran utilizado para el metodo writeXML
228
+ @return [Array]
229
+ =end
230
+
231
+ def sequence_to_array_objects
232
+ @attributes = Array.new if @attributes.nil?
233
+ @sequence = Array.new if @sequence.nil?
234
+
235
+ array = []
236
+ if !@sequence.empty?
237
+ @sequence.each { |element|
238
+ downcaseElement = element.downcase
239
+ if instance_attribute_defined?(downcaseElement)
240
+ object = get(downcaseElement)
241
+ if object.kind_of?(Array)
242
+ return object
243
+ else
244
+ array << object
245
+ end
246
+ end
247
+ }
248
+ else
249
+ if !@objects.nil?
250
+ @objects.each { |o|
251
+ array << o
252
+ }
253
+ end
254
+ end
255
+ return array;
256
+ end
257
+ =begin
258
+ Revisa la secuencia, si la secuencia no esta en vacio reguesa un hash con el nombre
259
+ de las clases, en cambio si esta vacio, asigna variables de objecto a la clase raiz
260
+ @param [Hash] [Array]
261
+ @return [Hash] regresa el nombre de clases con key de la clase en minusculas
262
+ =end
263
+
264
+
265
+ def from_hash_to_array_objects(para)
266
+ hash_objects = {}
267
+ if !@sequence.empty?
268
+ @sequence.each { |o|
269
+ hash_objects[o.downcase.to_sym] = o
270
+ }
271
+ else
272
+ if para.kind_of?(Array)
273
+ @objects= Array.new
274
+ array_hashing = para
275
+ array_hashing.each { | l |
276
+ l.each { |k, v|
277
+ object = Object.const_get(k.to_s).new(v)
278
+ @objects << object
279
+ }
280
+ }
281
+ end
282
+ end
283
+ return hash_objects
284
+ end
285
+ =begin
286
+ Metodo que mapea todo el hash a un asignacion en la clase, se basa los atributos y sequence
287
+ inicializada en la para de arriba
288
+ @param [Hash] es hash es para mapear todos los atributos con sus asignaciones
289
+ @param [Array] es un arreglo con el nombre de los objectos, es como se llaman los objectos
290
+ ya que se inicializara y se asignaran
291
+ =end
292
+ def set_atr_object_from_hash(para, hash_objects)
293
+
294
+ attributes_downcase =@attributes.map(&:downcase)
295
+ elements_downcase =@sequence.map(&:downcase)
296
+ if para.kind_of? Hash
297
+ para.each { |key, value|
298
+ keyDowncase = key.downcase
299
+ if(attributes_downcase.include?( keyDowncase.to_s))
300
+ set( keyDowncase, value)
301
+ elsif (elements_downcase.include?(keyDowncase.to_s))
302
+ if(value.kind_of?(Hash))
303
+ object = Object.const_get(hash_objects[keyDowncase.to_sym]).new(value)
304
+ set( keyDowncase, object)
305
+ elsif (value.kind_of?(Array))
306
+ kind_of_value_inside_array(value, keyDowncase, hash_objects)
307
+ end
308
+ end
309
+ }
310
+ elsif para.kind_of? Array
311
+ array_objects = Array.new
312
+ para.each { |l|
313
+ l.each { |k, v|
314
+ object = Object.const_get(k.to_s).new(v)
315
+ array_objects << object
316
+ }
317
+ }
318
+ if !@sequence.empty?
319
+ set( @sequence[0].downcase.to_sym , array_objects)
320
+ else
321
+ @objects = array_objects
322
+ end
323
+ end
324
+ end
325
+ =begin
326
+ #Revisa que el los valores del hash no haya valores que son arreglos sino busca sus
327
+ objectos, si estan en la secuencia los crea a partir de alli, sino esta en la secuencia
328
+ los asigna a un objeto llamado objectos.
329
+ @param [Array] es el arreglo que se encontro en el from_hash
330
+ @param [Array] es el Symbol que se encontro en el from_hash y estan en minusculas
331
+ @param [Hash] son las clases disponibles en la sequence
332
+ =end
333
+ def kind_of_value_inside_array(array, key_downcase, hash_objects)
334
+
335
+ sequence_downcase =@sequence.map(&:downcase)
336
+ objects= Array.new
337
+
338
+ if sequence_downcase.include? key_downcase.to_s
339
+ array.each { |e|
340
+ object = Object.const_get(hash_objects[key_downcase.to_sym]).new(e)
341
+ objects << object
342
+ }
343
+ set( key_downcase, objects)
344
+ end
345
+ end
346
+
347
+
348
+ end
metadata CHANGED
@@ -1,21 +1,22 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contmx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cesar Castillo Moreno
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-25 00:00:00.000000000 Z
11
+ date: 2015-07-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: XML de contabilidad de Mexico
13
+ description: Contabilidad en medios electronicos
14
14
  email: cesar.cast.more@gmail.com
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
- files: []
18
+ files:
19
+ - lib/writerxml.rb
19
20
  homepage: https://github.com/cesarcastmore/contmx
20
21
  licenses:
21
22
  - MIT
@@ -39,6 +40,6 @@ rubyforge_project:
39
40
  rubygems_version: 2.4.7
40
41
  signing_key:
41
42
  specification_version: 4
42
- summary: XML de Contabilidad de Mexico
43
+ summary: XML de contabilidad de mexico
43
44
  test_files: []
44
45
  has_rdoc: