Class: ZATCA::UBL::BaseComponent

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Defined in:
lib/zatca/ubl/base_component.rb

Constant Summary collapse

ArrayOfBaseComponentOrNil =

def self.guard_dig(obj)

unless obj.respond_to?(:dig)
  raise TypeError, "#{obj.class.name} does not have #dig method"
end

end

ZATCA::Types::Array.of(ZATCA::Types.Instance(ZATCA::UBL::BaseComponent))
.default { [] }
.constructor { |value| value.blank? ? [] : value.compact }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/zatca/ubl/base_component.rb', line 6

def attributes
  @attributes
end

#elementsObject

Returns the value of attribute elements.



6
7
8
# File 'lib/zatca/ubl/base_component.rb', line 6

def elements
  @elements
end

#indexObject

Returns the value of attribute index.



6
7
8
# File 'lib/zatca/ubl/base_component.rb', line 6

def index
  @index
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/zatca/ubl/base_component.rb', line 6

def name
  @name
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/zatca/ubl/base_component.rb', line 6

def value
  @value
end

Class Method Details

.build(elements: [], attributes: {}, value: "", name: "", index: nil) ⇒ Object

There are cases where we end up constructing elements with no content and we don’t want to include them in the final XML.

This method helps us to return nil if the element has no attributes, elements or value.

which is then caught in the ‘build_xml` method (using `elements.compact`) and ignored.



47
48
49
50
51
# File 'lib/zatca/ubl/base_component.rb', line 47

def self.build(elements: [], attributes: {}, value: "", name: "", index: nil)
  return nil if elements.blank? && attributes.blank? && value.blank?

  new(elements: elements, attributes: attributes, value: value, name: name, index: index)
end

Instance Method Details

#[](name) ⇒ Object



53
54
55
# File 'lib/zatca/ubl/base_component.rb', line 53

def [](name)
  elements.find { |element| element.name == name }
end

#build_xml(xml) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/zatca/ubl/base_component.rb', line 119

def build_xml(xml)
  xml.send(name, attributes) do
    if elements.length > 0
      elements.compact.each { |element| element.build_xml(xml) }
    elsif value.present?
      xml.text(value)
    end
  end
end

#dig(key, *args) ⇒ Object



57
58
59
60
61
62
# File 'lib/zatca/ubl/base_component.rb', line 57

def dig(key, *args)
  value = self[key]
  return value if args.length == 0 || value.nil?
  # DigRb.guard_dig(value)
  value.dig(*args)
end

#find_nested_element_by_path(path) ⇒ Object

TODO: Under Active Development



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/zatca/ubl/base_component.rb', line 65

def find_nested_element_by_path(path)
  path_parts = path.split("/")
  nested_element = self

  path_parts.each_with_index do |path_part, index|
    # byebug
    # next_path_part = path_parts[index + 1]
    # found_element = nested_element[path_part]
    # found_next_path_part = found_element[next_path_part]

    # element_with_next_path_part = found_element.find do |child_element|
    #   child_element.name == next_path_part
    # end
    # byebug
    # nested_element.elements.each do |element|
    #   byebug
    #   next_element = element[path_part]

    #   if next_element && next_element[next_path_part]
    #     nested_element = next_element[next_path_part]
    #     break
    #   elsif next_element.present?
    #     nested_element = next_element
    #   end
    # end

    # nested_element = found_element if found_element.present?
  end

  nested_element
end

#generate_xml(canonicalized: false, spaces: 2, apply_invoice_hacks: false, remove_root_xml_tag: false) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/zatca/ubl/base_component.rb', line 129

def generate_xml(
  canonicalized: false,
  spaces: 2,
  apply_invoice_hacks: false,
  remove_root_xml_tag: false
)
  ZATCA::UBL::Builder.new(element: self).build(
    canonicalized: canonicalized,
    spaces: spaces,
    apply_invoice_hacks: apply_invoice_hacks,
    remove_root_xml_tag: remove_root_xml_tag
  )
end

#schemaObject



97
98
99
# File 'lib/zatca/ubl/base_component.rb', line 97

def schema
  self.class.schema
end

#to_hObject



101
102
103
104
105
106
107
108
109
# File 'lib/zatca/ubl/base_component.rb', line 101

def to_h
  {
    name => {
      attributes: attributes,
      **elements.map(&:to_h),
      value: value
    }
  }
end

#to_xmlObject



111
112
113
114
115
116
117
# File 'lib/zatca/ubl/base_component.rb', line 111

def to_xml
  builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
    xml.root { build_xml(xml) }
  end

  builder.to_xml
end