Class: Cfdi40::Node

Inherits:
Object
  • 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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeNode

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_nodesObject

Nokigiri XML Document for the xml_node



10
11
12
# File 'lib/cfdi40/node.rb', line 10

def children_nodes
  @children_nodes
end

#element_nameObject

Returns setted @element_name or use class_name



227
228
229
230
231
# File 'lib/cfdi40/node.rb', line 227

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_nodeObject

Nokigiri XML Document for the xml_node



10
11
12
# File 'lib/cfdi40/node.rb', line 10

def parent_node
  @parent_node
end

#readonlyObject (readonly)

Returns the value of attribute readonly.



12
13
14
# File 'lib/cfdi40/node.rb', line 12

def readonly
  @readonly
end

#xml_documentObject

Nokigiri XML Document for the xml_node



10
11
12
# File 'lib/cfdi40/node.rb', line 10

def xml_document
  @xml_document
end

#xml_parentObject

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

.attributesObject



132
133
134
# File 'lib/cfdi40/node.rb', line 132

def self.attributes
  @@attributes[name]
end

.default_valuesObject



136
137
138
# File 'lib/cfdi40/node.rb', line 136

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



123
124
125
126
# File 'lib/cfdi40/node.rb', line 123

def self.define_element_name(value)
  verify_class_variables
  @@element_names[name] = value.to_s
end

.define_namespace(namespace, value) ⇒ Object



118
119
120
121
# File 'lib/cfdi40/node.rb', line 118

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
77
78
79
80
81
# File 'lib/cfdi40/node.rb', line 49

def self.define_reader(accessor, format)
  case format.to_s
  when 'integer'
    define_method("#{accessor}".to_sym) do
      value = instance_variable_defined?("@#{accessor}".to_sym) ? instance_variable_get("@#{accessor}".to_sym) : 0
      value.to_i
    end
  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



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
112
113
114
115
116
# File 'lib/cfdi40/node.rb', line 83

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_nameObject



144
145
146
147
# File 'lib/cfdi40/node.rb', line 144

def self.element_name
  verify_class_variables
  @@element_names[name]
end

.formatsObject



140
141
142
# File 'lib/cfdi40/node.rb', line 140

def self.formats
  @@formats[name]
end

.namespacesObject



128
129
130
# File 'lib/cfdi40/node.rb', line 128

def self.namespaces
  @@namespaces[name]
end

.verify_class_variablesObject

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



246
247
248
249
250
251
252
253
254
# File 'lib/cfdi40/node.rb', line 246

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

Raises:



165
166
167
168
169
170
# File 'lib/cfdi40/node.rb', line 165

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



256
257
258
259
260
261
262
# File 'lib/cfdi40/node.rb', line 256

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



239
240
241
242
243
# File 'lib/cfdi40/node.rb', line 239

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

Returns:

  • (Boolean)


159
160
161
162
163
# File 'lib/cfdi40/node.rb', line 159

def attibute_is_null?(accessor)
  return true unless instance_variable_defined?("@#{accessor}".to_sym)

  instance_variable_get("@#{accessor}".to_sym).nil?
end

#clean_cached_xmlObject

Cleans a Nokogiri object and/or loaded_xml string if exists



279
280
281
282
283
284
# File 'lib/cfdi40/node.rb', line 279

def clean_cached_xml
  @docxml = nil
  @loaded_xml = nil

  parent_node&.clean_cached_xml
end

#create_xml_nodeObject



217
218
219
220
221
222
223
224
# File 'lib/cfdi40/node.rb', line 217

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_namespaceObject



186
187
188
189
190
191
192
# File 'lib/cfdi40/node.rb', line 186

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

Raises:



172
173
174
175
176
# File 'lib/cfdi40/node.rb', line 172

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_nameObject



233
234
235
236
237
# File 'lib/cfdi40/node.rb', line 233

def expanded_element_name
  return element_name unless current_namespace

  "#{current_namespace}:#{element_name}"
end

#formatted_value(accessor) ⇒ Object



264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/cfdi40/node.rb', line 264

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



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/cfdi40/node.rb', line 196

def load_from_ng_node(ng_node)
  # TODO: Se puede cargar el certificado
  # x509_cert = OpenSSL::X509::Certificate.new(Base64.decode64(<valor del atributo>))
  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

#lockObject

Locks for readonly this node and children



179
180
181
182
183
184
# File 'lib/cfdi40/node.rb', line 179

def lock
  @readonly = true
  @children_nodes.each(&:lock)

  true
end

#set_defaultsObject



149
150
151
152
153
154
155
156
157
# File 'lib/cfdi40/node.rb', line 149

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