Class: Cfdi40::Node
- Inherits:
-
Object
show all
- Defined in:
- lib/cfdi40/node.rb
Overview
Main class for build CFDi.
Keeps definitions (names, accessors and formats) and relations (parent & children) between nodes
Direct Known Subclasses
Complemento, ComplementoConcepto, Comprobante, Concepto, ConceptoImpuestos, Conceptos, CpTotales, DoctoRelacionado, Emisor, Impuestos, ImpuestosDR, ImpuestosP, InstEducativas, Pago, Pagos, Receptor, Timbre, Traslado, TrasladoDR, TrasladoP, Traslados, TrasladosDR, TrasladosP
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.attributes ⇒ Object
-
.default_values ⇒ Object
-
.define_attribute(accessor, xml_attribute:, default: nil, format: nil, readonly: false, no_writer: nil) ⇒ Object
no_writer option implies that the writer methos must be defined in the child class.
-
.define_element_name(value) ⇒ Object
-
.define_namespace(namespace, value) ⇒ Object
-
.define_reader(accessor, format) ⇒ Object
-
.define_writer(accessor, readonly_attribute, format) ⇒ Object
-
.element_name ⇒ Object
-
.formats ⇒ Object
-
.namespaces ⇒ Object
-
.verify_class_variables ⇒ Object
Use class variables to define attributes used to create nodes Class variables are the same for children classes, so are organized by the name of the class.
Instance Method Summary
collapse
Constructor Details
#initialize ⇒ Node
Returns a new instance of Node.
14
15
16
17
18
19
|
# File 'lib/cfdi40/node.rb', line 14
def initialize
self.class.verify_class_variables
@readonly = readonly
@children_nodes = []
set_defaults
end
|
Instance Attribute Details
#children_nodes ⇒ Object
Nokigiri XML Document for the xml_node
10
11
12
|
# File 'lib/cfdi40/node.rb', line 10
def children_nodes
@children_nodes
end
|
#element_name ⇒ Object
Returns setted @element_name or use class_name
222
223
224
225
226
|
# File 'lib/cfdi40/node.rb', line 222
def element_name
return self.class.element_name unless self.class.element_name.nil? || self.class.element_name == ""
self.class.name.split("::").last
end
|
#parent_node ⇒ Object
Nokigiri XML Document for the xml_node
10
11
12
|
# File 'lib/cfdi40/node.rb', line 10
def parent_node
@parent_node
end
|
#readonly ⇒ Object
Returns the value of attribute readonly.
12
13
14
|
# File 'lib/cfdi40/node.rb', line 12
def readonly
@readonly
end
|
#xml_document ⇒ Object
Nokigiri XML Document for the xml_node
10
11
12
|
# File 'lib/cfdi40/node.rb', line 10
def xml_document
@xml_document
end
|
#xml_parent ⇒ Object
Nokigiri XML Document for the xml_node
10
11
12
|
# File 'lib/cfdi40/node.rb', line 10
def xml_parent
@xml_parent
end
|
Class Method Details
.attributes ⇒ Object
127
128
129
|
# File 'lib/cfdi40/node.rb', line 127
def self.attributes
@@attributes[name]
end
|
.default_values ⇒ Object
131
132
133
|
# File 'lib/cfdi40/node.rb', line 131
def self.default_values
@@default_values[name]
end
|
.define_attribute(accessor, xml_attribute:, default: nil, format: nil, readonly: false, no_writer: nil) ⇒ Object
no_writer option implies that the writer methos must be defined in the child class.
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/cfdi40/node.rb', line 37
def self.define_attribute(accessor, xml_attribute:, default: nil, format: nil, readonly: false, no_writer: nil)
verify_class_variables
define_reader(accessor, format)
define_writer(accessor, readonly, format) unless no_writer
@@attributes[name][accessor.to_sym] = xml_attribute
@@default_values[name][accessor.to_sym] = default if default
return unless format
@@formats[name][accessor.to_sym] = format.to_sym
end
|
.define_element_name(value) ⇒ Object
118
119
120
121
|
# File 'lib/cfdi40/node.rb', line 118
def self.define_element_name(value)
verify_class_variables
@@element_names[name] = value.to_s
end
|
.define_namespace(namespace, value) ⇒ Object
113
114
115
116
|
# File 'lib/cfdi40/node.rb', line 113
def self.define_namespace(namespace, value)
verify_class_variables
@@namespaces[name][namespace] = value
end
|
.define_reader(accessor, format) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/cfdi40/node.rb', line 49
def self.define_reader(accessor, format)
case format.to_s
when 't_Importe', 'decimal'
define_method("#{accessor}".to_sym) do
value = instance_variable_defined?("@#{accessor}".to_sym) ? instance_variable_get("@#{accessor}".to_sym) : 0
value.to_f.round(6)
end
when 't_ImporteMXN'
define_method("#{accessor}".to_sym) do
value = instance_variable_defined?("@#{accessor}".to_sym) ? instance_variable_get("@#{accessor}".to_sym) : 0
value.to_f.round(2)
end
when 't_FechaH', 't_FechaHora'
define_method("#{accessor}".to_sym) do
value = instance_variable_defined?("@#{accessor}".to_sym) ? instance_variable_get("@#{accessor}".to_sym) : nil
return nil unless value.is_a?(Time)
value
end
else
define_method("#{accessor}".to_sym) do
value = instance_variable_defined?("@#{accessor}".to_sym) ? instance_variable_get("@#{accessor}".to_sym) : nil
return nil if value.nil?
value.to_s
end
end
end
|
.define_writer(accessor, readonly_attribute, format) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/cfdi40/node.rb', line 78
def self.define_writer(accessor, readonly_attribute, format)
if readonly_attribute
define_method "#{accessor}=".to_sym do |value|
raise Cfdi40::Error, "attribute '#{accessor}' can not be modified"
end
else
case format.to_s
when 't_FechaH', 't_FechaHora'
define_method "#{accessor}=".to_sym do |value|
raise Cfdi40::Error, "CFDI is read only" if self.readonly
clean_cached_xml
if value.nil? || value.is_a?(Time)
instance_variable_set("@#{accessor}".to_sym, value)
return
end
begin
parsed_time = Time.strptime(value.to_s, "%Y-%m-%dT%H:%M:%S")
instance_variable_set("@#{accessor}".to_sym, parsed_time)
rescue
raise Cfdi40::Error, "#{value} must have format 'yyyy-mm-ddTHH:MM:SS'"
end
end
else
define_method "#{accessor}=".to_sym do |value|
raise Cfdi40::Error, "CFDI loaded in read only mode" if self.readonly
clean_cached_xml
instance_variable_set("@#{accessor}".to_sym, value)
end
end
end
end
|
.element_name ⇒ Object
139
140
141
142
|
# File 'lib/cfdi40/node.rb', line 139
def self.element_name
verify_class_variables
@@element_names[name]
end
|
135
136
137
|
# File 'lib/cfdi40/node.rb', line 135
def self.formats
@@formats[name]
end
|
.namespaces ⇒ Object
123
124
125
|
# File 'lib/cfdi40/node.rb', line 123
def self.namespaces
@@namespaces[name]
end
|
.verify_class_variables ⇒ Object
Use class variables to define attributes used to create nodes Class variables are the same for children classes, so are organized by the name of the class.
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/cfdi40/node.rb', line 24
def self.verify_class_variables
@@attributes ||= {}
@@attributes[name] ||= {}
@@namespaces ||= {}
@@namespaces[name] ||= {}
@@default_values ||= {}
@@default_values[name] ||= {}
@@formats ||= {}
@@formats[name] ||= {}
@@element_names ||= {}
end
|
Instance Method Details
#add_attributes_to(node) ⇒ Object
Add defined attributes. Skip unused attributes
241
242
243
244
245
246
247
248
249
|
# File 'lib/cfdi40/node.rb', line 241
def add_attributes_to(node)
self.class.attributes.each do |object_accessor, xml_attribute|
next unless respond_to?(object_accessor)
next unless instance_variable_defined?("@#{object_accessor}".to_sym)
next if instance_variable_get("@#{object_accessor}".to_sym).nil?
node[xml_attribute] = formatted_value(object_accessor)
end
end
|
#add_child_node(child_node) ⇒ Object
160
161
162
163
164
165
|
# File 'lib/cfdi40/node.rb', line 160
def add_child_node(child_node)
raise Error, "child_node must be a Node object" unless child_node.is_a?(Node)
child_node.parent_node = self
@children_nodes << child_node
end
|
#add_children_to(xml_node) ⇒ Object
251
252
253
254
255
256
257
|
# File 'lib/cfdi40/node.rb', line 251
def add_children_to(xml_node)
children_nodes.each do |node|
node.xml_document = xml_document
node.xml_parent = xml_node
node.create_xml_node
end
end
|
#add_namespaces_to(xml_node) ⇒ Object
234
235
236
237
238
|
# File 'lib/cfdi40/node.rb', line 234
def add_namespaces_to(xml_node)
self.class.namespaces.each do |namespace, value|
xml_node.add_namespace namespace, value
end
end
|
#attibute_is_null?(accessor) ⇒ Boolean
154
155
156
157
158
|
# File 'lib/cfdi40/node.rb', line 154
def attibute_is_null?(accessor)
return true unless instance_variable_defined?("@#{accessor}".to_sym)
instance_variable_get("@#{accessor}".to_sym).nil?
end
|
#clean_cached_xml ⇒ Object
Cleans a Nokogiri object and/or loaded_xml string if exists
274
275
276
277
278
279
|
# File 'lib/cfdi40/node.rb', line 274
def clean_cached_xml
@docxml = nil
@loaded_xml = nil
parent_node&.clean_cached_xml
end
|
#create_xml_node ⇒ Object
212
213
214
215
216
217
218
219
|
# File 'lib/cfdi40/node.rb', line 212
def create_xml_node
before_add if respond_to?(:before_add, true)
xml_node = xml_document.create_element(expanded_element_name)
add_namespaces_to(xml_node)
add_attributes_to(xml_node)
add_children_to(xml_node)
xml_parent.add_child xml_node
end
|
#current_namespace ⇒ Object
181
182
183
184
185
186
187
|
# File 'lib/cfdi40/node.rb', line 181
def current_namespace
return unless self.class.respond_to?(:namespaces)
return parent_node.current_namespace if self.class.namespaces.empty? && !parent_node.nil?
self.class.namespaces.keys.last
end
|
#delete_child(child_node) ⇒ Object
167
168
169
170
171
|
# File 'lib/cfdi40/node.rb', line 167
def delete_child(child_node)
raise Error, "child_node must be a Node object" unless child_node.is_a?(Node)
@children_nodes.delete(child_node)
end
|
#expanded_element_name ⇒ Object
228
229
230
231
232
|
# File 'lib/cfdi40/node.rb', line 228
def expanded_element_name
return element_name unless current_namespace
"#{current_namespace}:#{element_name}"
end
|
259
260
261
262
263
264
265
266
267
268
269
270
271
|
# File 'lib/cfdi40/node.rb', line 259
def formatted_value(accessor)
case self.class.formats[accessor]
when :t_Importe, :decimal
public_send(accessor).to_f == 0.0 ? "0" : format("%0.6f", public_send(accessor).to_f)
when :t_ImporteMXN
public_send(accessor).to_f == 0.0 ? "0" : format("%0.2f", public_send(accessor).to_f)
when :t_FechaH, :t_FechaHora
value = public_send(accessor)
value.is_a?(Time) ? value.strftime("%Y-%m-%dT%H:%M:%S") : ''
else
public_send(accessor)
end
end
|
#load_from_ng_node(ng_node) ⇒ Object
Load attributes from a Nokogiri::XML::Element. Attributes are loaded directly to the instance variable
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
# File 'lib/cfdi40/node.rb', line 191
def load_from_ng_node(ng_node)
self.class.attributes.each do |variable_name, attr_name|
next if ng_node.attributes[attr_name].nil?
case self.class.formats[variable_name]
when :t_FechaH, :t_FechaHora
begin
parsed_time = Time.strptime(ng_node.attributes[attr_name].value, "%Y-%m-%dT%H:%M:%S")
instance_variable_set("@#{variable_name}".to_sym, parsed_time)
rescue
puts "Warning: cannot parse '#{value}' as Time"
next
end
else
instance_variable_set("@#{variable_name}".to_sym, ng_node.attributes[attr_name].value)
end
end
end
|
#lock ⇒ Object
Locks for readonly this node and children
174
175
176
177
178
179
|
# File 'lib/cfdi40/node.rb', line 174
def lock
@readonly = true
@children_nodes.each(&:lock)
true
end
|
#set_defaults ⇒ Object
144
145
146
147
148
149
150
151
152
|
# File 'lib/cfdi40/node.rb', line 144
def set_defaults
return if self.class.default_values.nil?
self.class.default_values.each do |accessor, value|
next unless attibute_is_null?(accessor)
instance_variable_set "@#{accessor}".to_sym, value
end
end
|