Skip to content

Commit 292f23b

Browse files
authored
Merge pull request #32 from bigsolom/introduce_blank_present
introduce blank? and present? methods to make checking for blank values easier
2 parents 43516a3 + 94f9e59 commit 292f23b

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

lib/secretariat.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
=end
1616

1717
require_relative 'secretariat/version'
18+
require_relative 'secretariat/object_extensions'
1819
require_relative 'secretariat/constants'
1920
require_relative 'secretariat/helpers'
2021
require_relative 'secretariat/versioner'

lib/secretariat/invoice.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
require 'bigdecimal'
1818

1919
module Secretariat
20+
using ObjectExtensions
21+
2022
Invoice = Struct.new("Invoice",
2123
:id,
2224
:issue_date,
@@ -256,7 +258,7 @@ def to_xml(version: 1, validate: true)
256258
end
257259
trade_settlement = by_version(version, 'ApplicableSupplyChainTradeSettlement', 'ApplicableHeaderTradeSettlement')
258260
xml['ram'].send(trade_settlement) do
259-
if payment_reference && payment_reference != ''
261+
if payment_reference.present?
260262
xml['ram'].PaymentReference payment_reference
261263
end
262264
xml['ram'].InvoiceCurrencyCode currency_code
@@ -273,7 +275,7 @@ def to_xml(version: 1, validate: true)
273275
xml['ram'].ApplicableTradeTax do
274276
Helpers.currency_element(xml, 'ram', 'CalculatedAmount', tax.tax_amount, currency_code, add_currency: version == 1)
275277
xml['ram'].TypeCode 'VAT'
276-
if tax_reason_text && tax_reason_text != ''
278+
if tax_reason_text.present?
277279
xml['ram'].ExemptionReason tax_reason_text
278280
end
279281
Helpers.currency_element(xml, 'ram', 'BasisAmount', tax.base_amount, currency_code, add_currency: version == 1)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module Secretariat
2+
module ObjectExtensions
3+
refine Object do
4+
# Copied from activesupport/lib/active_support/core_ext/object/blank.rb, line 18
5+
def blank?
6+
respond_to?(:empty?) ? !!empty? : !self
7+
end
8+
9+
def present?
10+
!blank?
11+
end
12+
end
13+
end
14+
end

lib/secretariat/trade_party.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
=end
1616

1717
module Secretariat
18+
using ObjectExtensions
19+
1820
TradeParty = Struct.new('TradeParty',
1921
:name, :street1, :street2, :city, :postal_code, :country_id, :vat_id, :global_id, :global_id_scheme_id, :tax_id,
2022
keyword_init: true,
2123
) do
2224
def to_xml(xml, exclude_tax: false, version: 2)
23-
if global_id && global_id != '' && global_id_scheme_id && global_id_scheme_id != ''
25+
if global_id.present? && global_id_scheme_id.present?
2426
xml['ram'].GlobalID(schemeID: global_id_scheme_id) do
2527
xml.text(global_id)
2628
end
@@ -29,19 +31,19 @@ def to_xml(xml, exclude_tax: false, version: 2)
2931
xml['ram'].PostalTradeAddress do
3032
xml['ram'].PostcodeCode postal_code
3133
xml['ram'].LineOne street1
32-
if street2 && street2 != ''
34+
if street2.present?
3335
xml['ram'].LineTwo street2
3436
end
3537
xml['ram'].CityName city
3638
xml['ram'].CountryID country_id
3739
end
38-
if !exclude_tax && vat_id && vat_id != ''
40+
if !exclude_tax && vat_id.present?
3941
xml['ram'].SpecifiedTaxRegistration do
4042
xml['ram'].ID(schemeID: 'VA') do
4143
xml.text(vat_id)
4244
end
4345
end
44-
elsif tax_id && tax_id != ''
46+
elsif tax_id.present?
4547
xml['ram'].SpecifiedTaxRegistration do
4648
xml['ram'].ID(schemeID: 'FC') do
4749
xml.text(tax_id)

test/invoice_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,5 +588,12 @@ def test_negative_de_invoice_against_schematron_2
588588
end
589589
assert_equal [], errors
590590
end
591+
592+
def test_invoice_object_extensions
593+
invoice = make_de_invoice
594+
xml = invoice.to_xml(version: 2)
595+
596+
assert_match(/<ram:PaymentReference>#{invoice.payment_reference}<\/ram:PaymentReference>/, xml)
597+
end
591598
end
592599
end

0 commit comments

Comments
 (0)