diff --git a/lib/secretariat.rb b/lib/secretariat.rb index d2cf46e..aec4498 100644 --- a/lib/secretariat.rb +++ b/lib/secretariat.rb @@ -15,6 +15,7 @@ =end require_relative 'secretariat/version' +require_relative 'secretariat/object_extensions' require_relative 'secretariat/constants' require_relative 'secretariat/helpers' require_relative 'secretariat/versioner' diff --git a/lib/secretariat/invoice.rb b/lib/secretariat/invoice.rb index 73c97ed..67cdb86 100644 --- a/lib/secretariat/invoice.rb +++ b/lib/secretariat/invoice.rb @@ -17,6 +17,8 @@ require 'bigdecimal' module Secretariat + using ObjectExtensions + Invoice = Struct.new("Invoice", :id, :issue_date, @@ -256,7 +258,7 @@ def to_xml(version: 1, validate: true) end trade_settlement = by_version(version, 'ApplicableSupplyChainTradeSettlement', 'ApplicableHeaderTradeSettlement') xml['ram'].send(trade_settlement) do - if payment_reference && payment_reference != '' + if payment_reference.present? xml['ram'].PaymentReference payment_reference end xml['ram'].InvoiceCurrencyCode currency_code @@ -273,7 +275,7 @@ def to_xml(version: 1, validate: true) xml['ram'].ApplicableTradeTax do Helpers.currency_element(xml, 'ram', 'CalculatedAmount', tax.tax_amount, currency_code, add_currency: version == 1) xml['ram'].TypeCode 'VAT' - if tax_reason_text && tax_reason_text != '' + if tax_reason_text.present? xml['ram'].ExemptionReason tax_reason_text end Helpers.currency_element(xml, 'ram', 'BasisAmount', tax.base_amount, currency_code, add_currency: version == 1) diff --git a/lib/secretariat/object_extensions.rb b/lib/secretariat/object_extensions.rb new file mode 100644 index 0000000..d81e5d4 --- /dev/null +++ b/lib/secretariat/object_extensions.rb @@ -0,0 +1,14 @@ +module Secretariat + module ObjectExtensions + refine Object do + # Copied from activesupport/lib/active_support/core_ext/object/blank.rb, line 18 + def blank? + respond_to?(:empty?) ? !!empty? : !self + end + + def present? + !blank? + end + end + end +end \ No newline at end of file diff --git a/lib/secretariat/trade_party.rb b/lib/secretariat/trade_party.rb index ae6df67..ebd1afb 100644 --- a/lib/secretariat/trade_party.rb +++ b/lib/secretariat/trade_party.rb @@ -15,12 +15,14 @@ =end module Secretariat + using ObjectExtensions + TradeParty = Struct.new('TradeParty', :name, :street1, :street2, :city, :postal_code, :country_id, :vat_id, :global_id, :global_id_scheme_id, :tax_id, keyword_init: true, ) do def to_xml(xml, exclude_tax: false, version: 2) - if global_id && global_id != '' && global_id_scheme_id && global_id_scheme_id != '' + if global_id.present? && global_id_scheme_id.present? xml['ram'].GlobalID(schemeID: global_id_scheme_id) do xml.text(global_id) end @@ -29,19 +31,19 @@ def to_xml(xml, exclude_tax: false, version: 2) xml['ram'].PostalTradeAddress do xml['ram'].PostcodeCode postal_code xml['ram'].LineOne street1 - if street2 && street2 != '' + if street2.present? xml['ram'].LineTwo street2 end xml['ram'].CityName city xml['ram'].CountryID country_id end - if !exclude_tax && vat_id && vat_id != '' + if !exclude_tax && vat_id.present? xml['ram'].SpecifiedTaxRegistration do xml['ram'].ID(schemeID: 'VA') do xml.text(vat_id) end end - elsif tax_id && tax_id != '' + elsif tax_id.present? xml['ram'].SpecifiedTaxRegistration do xml['ram'].ID(schemeID: 'FC') do xml.text(tax_id) diff --git a/test/invoice_test.rb b/test/invoice_test.rb index 91ce0eb..059eec3 100644 --- a/test/invoice_test.rb +++ b/test/invoice_test.rb @@ -588,5 +588,12 @@ def test_negative_de_invoice_against_schematron_2 end assert_equal [], errors end + + def test_invoice_object_extensions + invoice = make_de_invoice + xml = invoice.to_xml(version: 2) + + assert_match(/#{invoice.payment_reference}<\/ram:PaymentReference>/, xml) + end end end