gobl 0.1.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/data/tax/CO.json +420 -0
  3. data/data/tax/ES.json +61 -0
  4. data/lib/ext/hashme.rb +28 -0
  5. data/lib/gobl/bill/advances.rb +1 -77
  6. data/lib/gobl/bill/charge.rb +11 -65
  7. data/lib/gobl/bill/delivery.rb +5 -47
  8. data/lib/gobl/bill/discount.rb +11 -65
  9. data/lib/gobl/bill/exchange_rates.rb +1 -77
  10. data/lib/gobl/bill/invoice.rb +28 -104
  11. data/lib/gobl/bill/invoice_type.rb +3 -134
  12. data/lib/gobl/bill/line.rb +13 -65
  13. data/lib/gobl/bill/line_charge.rb +5 -47
  14. data/lib/gobl/bill/line_discount.rb +5 -47
  15. data/lib/gobl/bill/ordering.rb +2 -38
  16. data/lib/gobl/bill/outlay.rb +11 -59
  17. data/lib/gobl/bill/payment.rb +5 -47
  18. data/lib/gobl/bill/preceding.rb +12 -62
  19. data/lib/gobl/bill/scheme_keys.rb +1 -77
  20. data/lib/gobl/bill/tax.rb +4 -44
  21. data/lib/gobl/bill/totals.rb +17 -71
  22. data/lib/gobl/cal/date.rb +1 -90
  23. data/lib/gobl/cal/period.rb +5 -41
  24. data/lib/gobl/currency/code.rb +3 -134
  25. data/lib/gobl/currency/exchange_rate.rb +5 -41
  26. data/lib/gobl/document.rb +1 -58
  27. data/lib/gobl/dsig/digest.rb +5 -41
  28. data/lib/gobl/dsig/signature.rb +1 -90
  29. data/lib/gobl/enum.rb +81 -0
  30. data/lib/gobl/envelope.rb +9 -48
  31. data/lib/gobl/header.rb +10 -56
  32. data/lib/gobl/i18n/string.rb +1 -58
  33. data/lib/gobl/l10n/code.rb +1 -90
  34. data/lib/gobl/l10n/country_code.rb +3 -134
  35. data/lib/gobl/map.rb +62 -0
  36. data/lib/gobl/note/message.rb +5 -44
  37. data/lib/gobl/num/amount.rb +6 -35
  38. data/lib/gobl/object.rb +21 -0
  39. data/lib/gobl/operations.rb +3 -3
  40. data/lib/gobl/org/address.rb +18 -80
  41. data/lib/gobl/org/code.rb +1 -90
  42. data/lib/gobl/org/coordinates.rb +5 -47
  43. data/lib/gobl/org/email.rb +6 -47
  44. data/lib/gobl/org/inbox.rb +8 -50
  45. data/lib/gobl/org/item.rb +13 -65
  46. data/lib/gobl/org/item_code.rb +4 -41
  47. data/lib/gobl/org/key.rb +1 -90
  48. data/lib/gobl/org/meta.rb +1 -58
  49. data/lib/gobl/org/name.rb +12 -62
  50. data/lib/gobl/org/note.rb +6 -47
  51. data/lib/gobl/org/note_key.rb +3 -134
  52. data/lib/gobl/org/party.rb +14 -71
  53. data/lib/gobl/org/person.rb +9 -56
  54. data/lib/gobl/org/registration.rb +9 -59
  55. data/lib/gobl/org/source_key.rb +3 -134
  56. data/lib/gobl/org/tax_identity.rb +8 -53
  57. data/lib/gobl/org/telephone.rb +5 -44
  58. data/lib/gobl/org/unit.rb +1 -136
  59. data/lib/gobl/pay/advance.rb +11 -59
  60. data/lib/gobl/pay/card.rb +5 -41
  61. data/lib/gobl/pay/credit_transfer.rb +6 -50
  62. data/lib/gobl/pay/direct_debit.rb +4 -44
  63. data/lib/gobl/pay/due_date.rb +8 -50
  64. data/lib/gobl/pay/instructions.rb +11 -62
  65. data/lib/gobl/pay/method_key.rb +3 -134
  66. data/lib/gobl/pay/online.rb +4 -41
  67. data/lib/gobl/pay/term_key.rb +3 -134
  68. data/lib/gobl/pay/terms.rb +6 -47
  69. data/lib/gobl/stamp.rb +5 -41
  70. data/lib/gobl/struct.rb +15 -15
  71. data/lib/gobl/tax/category.rb +9 -50
  72. data/lib/gobl/tax/category_total.rb +11 -53
  73. data/lib/gobl/tax/combo.rb +6 -47
  74. data/lib/gobl/tax/localities.rb +1 -77
  75. data/lib/gobl/tax/locality.rb +6 -44
  76. data/lib/gobl/tax/rate.rb +8 -47
  77. data/lib/gobl/tax/rate_total.rb +9 -50
  78. data/lib/gobl/tax/rate_total_surcharge.rb +5 -41
  79. data/lib/gobl/tax/rate_value.rb +6 -47
  80. data/lib/gobl/tax/region.rb +12 -56
  81. data/lib/gobl/tax/scheme.rb +8 -50
  82. data/lib/gobl/tax/schemes.rb +1 -77
  83. data/lib/gobl/tax/set.rb +1 -77
  84. data/lib/gobl/tax/total.rb +4 -41
  85. data/lib/gobl/uuid/uuid.rb +1 -90
  86. data/lib/gobl/value.rb +58 -0
  87. data/lib/gobl/version.rb +1 -1
  88. data/lib/gobl.rb +3 -1
  89. data/lib/gobl_extensions/document_helper.rb +3 -3
  90. data/lib/gobl_extensions/envelope_helper.rb +5 -0
  91. data/lib/gobl_extensions/i18n/value_keys_helper.rb +3 -3
  92. data/lib/gobl_extensions/tax/region_helper.rb +3 -9
  93. metadata +12 -35
  94. data/lib/gobl/types.rb +0 -17
@@ -9,7 +9,7 @@
9
9
  module GOBL
10
10
  module Currency
11
11
  # ISO Currency Code
12
- class Code < GOBL::Struct
12
+ class Code < GOBL::Enum
13
13
  # The Schema ID of the GOBL Code structure
14
14
  SCHEMA_ID = 'https://gobl.org/draft-0/currency/code'
15
15
 
@@ -183,139 +183,8 @@ module GOBL
183
183
  'ZWL' => 'Zimbabwe Dollar'
184
184
  }.freeze
185
185
 
186
- attribute :_value, GOBL::Types::String.enum(*ENUM.keys)
187
- private :_value
188
-
189
- # Creates a new object from a GOBL value
190
- #
191
- # @param data [String] the GOBL value
192
- #
193
- # @return [Code] the object created from the given data
194
- def self.from_gobl!(data)
195
- new(_value: data)
196
- end
197
-
198
- # Returns a GOBL value representing the current object
199
- #
200
- # @return [String] the GOBL value that represents the current object
201
- def to_gobl
202
- _value
203
- end
204
-
205
- # Returns a {Code} that corresponds to a given object. The object can be a
206
- # `Symbol`, a `String` or anything coercible into one (via `#to_s`).
207
- #
208
- # @param object [Symbol, String, #to_s] the value of the object.
209
- #
210
- # @return [Code] the object corresponding to the given value.
211
- #
212
- # @example Instantiating from a symbol
213
- # code = GOBL::Currency::Code.new(:aed)
214
- #
215
- # @example Instantiating from a string
216
- # code = GOBL::Currency::Code.new('AED')
217
- def self.new(object)
218
- case object
219
- when Hash, self # internal use, not to be used in public calls
220
- super
221
- when Symbol
222
- new find_by_sym(object)
223
- else
224
- super _value: object.to_s
225
- end
226
- end
227
-
228
- # Returns the string representation of the current object
229
- #
230
- # @return [String] the string representation of the current object
231
- def to_s
232
- _value.to_s
233
- end
234
-
235
- # Returns the symbol representation of the current object
236
- #
237
- # @return [Symbol] the symbol representation of the current object
238
- def to_sym
239
- to_s.parameterize.underscore.to_sym
240
- end
241
-
242
- # Returns whether the current object is equivalent to a given one. In addition
243
- # to {Code} objects, the current object can be compared to a `Symbol`, a
244
- # `String` or anything coercible into one (via `#to_s`)
245
- #
246
- # @param other [Code, Symbol, String, #to_s] the other object to compare to
247
- #
248
- # @return [Boolean] `true` if the objects are equivalent, `false` otherwise
249
- #
250
- # @example Comparing to another {Code} object
251
- # code = GOBL::Currency::Code.new('AED')
252
- # code == GOBL::Currency::Code.new('AED') #=> true
253
- # code == GOBL::Currency::Code.new('AFN') #=> false
254
- #
255
- # @example Comparing to a string
256
- # code = GOBL::Currency::Code.new('AED')
257
- # code == 'AED' #=> true
258
- # code == 'AFN' #=> false
259
- #
260
- # @example Comparing to a symbol
261
- # code = GOBL::Currency::Code.new('AED')
262
- # code == :aed #=> true
263
- # code == :afn #=> false
264
- def ==(other)
265
- case other
266
- when self.class
267
- super
268
- when Symbol
269
- to_sym == other
270
- else
271
- to_s == other.to_s
272
- end
273
- end
274
-
275
- # Returns an array with all the enumerated objects of this type
276
- #
277
- # @return [Array<Code>] the array of enumerated objects
278
- def self.all
279
- ENUM.keys.map { |key| new(key) }
280
- end
281
-
282
- # @api private
283
- def self.find_by_sym(sym)
284
- all.find { |object| object.to_sym == sym }
285
- end
286
-
287
- # @api private
288
- def self.find_by_inquirer(method_name)
289
- method_name =~ /(.+)\?$/ && find_by_sym(Regexp.last_match(1).to_sym)
290
- end
291
-
292
- # Returns the description of the current object
293
- #
294
- # @return [String] the description of the current object
295
- def description
296
- ENUM.fetch(_value, _value)
297
- end
298
-
299
- # @api private
300
- def respond_to_missing?(method_name, include_private = false)
301
- self.class.find_by_inquirer(method_name) || super
302
- end
303
-
304
- # Enables dynamic value inquirers like `Code#aed?` for
305
- # each of the declared enum values (see {ENUM}). Each inquirer returns a
306
- # `Boolean` denoting whether the value equals the enquired value (`true`) or
307
- # not (`false`)
308
- #
309
- # @example
310
- # code = GOBL::Currency::Code.new('AED')
311
- # code.aed? # => true
312
- # code.afn? # => false
313
- def method_missing(method_name, *args, &block)
314
- if value = self.class.find_by_inquirer(method_name)
315
- self == value
316
- else
317
- super
318
- end
186
+ def strict_enum?
187
+ true
319
188
  end
320
189
  end
321
190
  end
@@ -9,57 +9,21 @@
9
9
  module GOBL
10
10
  module Currency
11
11
  # ExchangeRate contains data on the rate to be used when converting amounts from the document's base currency to whatever is defined.
12
- class ExchangeRate < GOBL::Struct
12
+ class ExchangeRate < GOBL::Object
13
13
  # The Schema ID of the GOBL ExchangeRate structure
14
14
  SCHEMA_ID = 'https://gobl.org/draft-0/currency/exchange-rate'
15
15
 
16
16
  # @!attribute [r] currency
17
17
  # ISO currency code this rate represents.
18
18
  # @return [GOBL::Currency::Code]
19
- attribute :currency, GOBL::Currency::Code
19
+ property :currency, GOBL::Currency::Code
20
+ validates :currency, presence: true
20
21
 
21
22
  # @!attribute [r] amount
22
23
  # How much is 1.00 of this currency worth in the documents currency.
23
24
  # @return [GOBL::Num::Amount]
24
- attribute :amount, GOBL::Types.Constructor(GOBL::Num::Amount)
25
-
26
- # Creates a new object from a hash of GOBL data
27
- #
28
- # @param data [Hash] a hash of GOBL data
29
- #
30
- # @return [ExchangeRate] the object created from the given data
31
- def self.from_gobl!(data)
32
- data = GOBL::Types::Hash[data]
33
-
34
- new(
35
- currency: GOBL::Currency::Code.from_gobl!(data['currency']),
36
- amount: data['amount']
37
- )
38
- end
39
-
40
- # Returns a hash of GOBL data representing the current object
41
- #
42
- # @return [Hash] the array of GOBL data that represents the current object
43
- def to_gobl
44
- {
45
- 'currency' => attributes[:currency]&.to_gobl,
46
- 'amount' => attributes[:amount]&.to_gobl
47
- }.compact
48
- end
49
-
50
- # @!method self.new(attrs)
51
- #
52
- # Returns a {ExchangeRate} object from a given hash of attributes. Nested
53
- # attributes are supported: the constructor will instantiate the proper GOBL
54
- # objects when nested hashes or arrays are given as part of the `attrs`
55
- # parameter.
56
- #
57
- # The `new` method will only allow to create a new object if all attributes
58
- # marked as mandatory and not calculated in the JSON schema are provided.
59
- #
60
- # @param attrs [Hash] the hash of attributes
61
- #
62
- # @return [ExchangeRate] the object corresponding to the given input
25
+ property :amount, GOBL::Num::Amount
26
+ validates :amount, presence: true
63
27
  end
64
28
  end
65
29
  end
data/lib/gobl/document.rb CHANGED
@@ -8,63 +8,6 @@
8
8
 
9
9
  module GOBL
10
10
  # Contents of the envelope that must contain a $schema.
11
- class Document < GOBL::Struct
12
- extend Forwardable
13
- include Enumerable
14
-
15
- # The Schema ID of the GOBL Document structure
16
- SCHEMA_ID = 'https://gobl.org/draft-0/envelope#/$defs/Document'
17
-
18
- attribute :_value, GOBL::Types::Hash
19
-
20
- # @!method [](key)
21
- # Returns the value associated with the given key
22
- # @param key [Object] the key
23
- # @return [Object] the value associated with the given key
24
- #
25
- # @!method each
26
- # Iterates over each key-value pair in the map
27
- # @overload each(&block)
28
- # Calls the given block with each key-value pair in the map
29
- # @yield [key, value] a key-value pair of the map
30
- # @return [self] the object itself
31
- # @overload each
32
- # Returns an Enumerator that iterates over the key-value pairs in the map
33
- # @return [Enumerator] the enumerator of the key-value pairs
34
- #
35
- # @!method empty?
36
- # Returns `true` if the number of entires in the map is zero, `false` otherwise.
37
- # @return [Boolean] whether the map is empty or not
38
- #
39
- # @!method length
40
- # Returns the number of entries in the map
41
- # @return [Integer] the number of entries in the map
42
- #
43
- # @!method each_key
44
- # Iterates over each key in the map
45
- # @overload each(&block)
46
- # Calls the given block with each key in the map
47
- # @yield [key] a key of the map
48
- # @return [self] the object itself
49
- # @overload each
50
- # Returns an Enumerator that iterates over the keys in the map
51
- # @return [Enumerator] the enumerator of the keys
52
- def_delegators :_value, :[], :each, :empty?, :length, :each_key
53
-
54
- # Creates a new object from a hash of GOBL data
55
- #
56
- # @param data [Hash] a hash of GOBL data
57
- #
58
- # @return [Document] the object created from the given data
59
- def self.from_gobl!(data)
60
- new(_value: data)
61
- end
62
-
63
- # Returns a hash of GOBL data representing the current object
64
- #
65
- # @return [Hash] the hash of GOBL data that represents the current object
66
- def to_gobl
67
- _value
68
- end
11
+ class Document < GOBL::Map
69
12
  end
70
13
  end
@@ -9,57 +9,21 @@
9
9
  module GOBL
10
10
  module DSig
11
11
  # Digest defines a structure to hold a digest value including the algorithm used to generate it.
12
- class Digest < GOBL::Struct
12
+ class Digest < GOBL::Object
13
13
  # The Schema ID of the GOBL Digest structure
14
14
  SCHEMA_ID = 'https://gobl.org/draft-0/dsig/digest'
15
15
 
16
16
  # @!attribute [r] alg
17
17
  # Algorithm stores the algorithm key that was used to generate the value.
18
18
  # @return [String]
19
- attribute :alg, GOBL::Types::String
19
+ property :alg, String
20
+ validates :alg, presence: true
20
21
 
21
22
  # @!attribute [r] val
22
23
  # Value contains the Hexadecimal representation of the resulting hash generated by the algorithm.
23
24
  # @return [String]
24
- attribute :val, GOBL::Types::String
25
-
26
- # Creates a new object from a hash of GOBL data
27
- #
28
- # @param data [Hash] a hash of GOBL data
29
- #
30
- # @return [Digest] the object created from the given data
31
- def self.from_gobl!(data)
32
- data = GOBL::Types::Hash[data]
33
-
34
- new(
35
- alg: data['alg'],
36
- val: data['val']
37
- )
38
- end
39
-
40
- # Returns a hash of GOBL data representing the current object
41
- #
42
- # @return [Hash] the array of GOBL data that represents the current object
43
- def to_gobl
44
- {
45
- 'alg' => attributes[:alg],
46
- 'val' => attributes[:val]
47
- }.compact
48
- end
49
-
50
- # @!method self.new(attrs)
51
- #
52
- # Returns a {Digest} object from a given hash of attributes. Nested
53
- # attributes are supported: the constructor will instantiate the proper GOBL
54
- # objects when nested hashes or arrays are given as part of the `attrs`
55
- # parameter.
56
- #
57
- # The `new` method will only allow to create a new object if all attributes
58
- # marked as mandatory and not calculated in the JSON schema are provided.
59
- #
60
- # @param attrs [Hash] the hash of attributes
61
- #
62
- # @return [Digest] the object corresponding to the given input
25
+ property :val, String
26
+ validates :val, presence: true
63
27
  end
64
28
  end
65
29
  end
@@ -9,98 +9,9 @@
9
9
  module GOBL
10
10
  module DSig
11
11
  # JSON Web Signature in compact form.
12
- class Signature < GOBL::Struct
12
+ class Signature < GOBL::Value
13
13
  # The Schema ID of the GOBL Signature structure
14
14
  SCHEMA_ID = 'https://gobl.org/draft-0/dsig/signature'
15
-
16
- attribute :_value, GOBL::Types::String
17
- private :_value
18
-
19
- # Creates a new object from a GOBL value
20
- #
21
- # @param data [String] the GOBL value
22
- #
23
- # @return [Signature] the object created from the given data
24
- def self.from_gobl!(data)
25
- new(_value: data)
26
- end
27
-
28
- # Returns a GOBL value representing the current object
29
- #
30
- # @return [String] the GOBL value that represents the current object
31
- def to_gobl
32
- _value
33
- end
34
-
35
- # Returns a {Signature} that corresponds to a given object. The object can be a
36
- # `Symbol`, a `String` or anything coercible into one (via `#to_s`).
37
- #
38
- # @param object [Symbol, String, #to_s] the value of the object.
39
- #
40
- # @return [Signature] the object corresponding to the given value.
41
- #
42
- # @example Instantiating from a symbol
43
- # signature = GOBL::DSig::Signature.new(:value)
44
- #
45
- # @example Instantiating from a string
46
- # signature = GOBL::DSig::Signature.new('value')
47
- def self.new(object)
48
- case object
49
- when Hash, self # internal use, not to be used in public calls
50
- super
51
- when Symbol
52
- new object.to_s
53
- else
54
- super _value: object.to_s
55
- end
56
- end
57
-
58
- # Returns the string representation of the current object
59
- #
60
- # @return [String] the string representation of the current object
61
- def to_s
62
- _value.to_s
63
- end
64
-
65
- # Returns the symbol representation of the current object
66
- #
67
- # @return [Symbol] the symbol representation of the current object
68
- def to_sym
69
- to_s.parameterize.underscore.to_sym
70
- end
71
-
72
- # Returns whether the current object is equivalent to a given one. In addition
73
- # to {Signature} objects, the current object can be compared to a `Symbol`, a
74
- # `String` or anything coercible into one (via `#to_s`)
75
- #
76
- # @param other [Signature, Symbol, String, #to_s] the other object to compare to
77
- #
78
- # @return [Boolean] `true` if the objects are equivalent, `false` otherwise
79
- #
80
- # @example Comparing to another {Signature} object
81
- # signature = GOBL::DSig::Signature.new('value')
82
- # signature == GOBL::DSig::Signature.new('value') #=> true
83
- # signature == GOBL::DSig::Signature.new('other') #=> false
84
- #
85
- # @example Comparing to a string
86
- # signature = GOBL::DSig::Signature.new('value')
87
- # signature == 'value' #=> true
88
- # signature == 'other' #=> false
89
- #
90
- # @example Comparing to a symbol
91
- # signature = GOBL::DSig::Signature.new('value')
92
- # signature == :value #=> true
93
- # signature == :other #=> false
94
- def ==(other)
95
- case other
96
- when self.class
97
- super
98
- when Symbol
99
- to_sym == other
100
- else
101
- to_s == other.to_s
102
- end
103
- end
104
15
  end
105
16
  end
106
17
  end
data/lib/gobl/enum.rb ADDED
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GOBL
4
+ # Base class for single value structures in the GOBL schema that have a controlled set
5
+ # of values associated to them (i.e., an enumeration) that may constrain them
6
+ class Enum < Value
7
+ # Returns an enum value object that corresponds to a given value. The value can be a
8
+ # `Symbol`, a `String` or anything coercible into one (via `#to_s`)
9
+ #
10
+ # @param value [Symbol, String, #to_s] the value of the object
11
+ #
12
+ # @return [Enum] the enum value instance corresponding to the given value
13
+ def initialize(value)
14
+ if value.is_a? Symbol
15
+ self._value = value_from_sym(value) || value.to_s
16
+ else
17
+ super
18
+ end
19
+ end
20
+
21
+ # Returns the description of the current enum value
22
+ #
23
+ # @return [String] the description of the current enum value
24
+ def description
25
+ self.class::ENUM.fetch(_value, _value)
26
+ end
27
+
28
+ # Enables dynamic value inquirers for each of the declared enum values. Each inquirer
29
+ # returns a `Boolean` denoting whether the value equals the enquired value (`true`) or
30
+ # not (`false`)
31
+ def method_missing(method_name, *args, &block)
32
+ if (value = self.class.find_by_inquirer(method_name))
33
+ self == value
34
+ else
35
+ super
36
+ end
37
+ end
38
+
39
+ # @api private
40
+ def respond_to_missing?(method_name, include_private = false)
41
+ self.class.find_by_inquirer(method_name) || super
42
+ end
43
+
44
+ class << self
45
+ # Returns an array with all the enumerated objects of this type
46
+ #
47
+ # @return [Array<#{name}>] the array of enumerated objects
48
+ def all
49
+ self::ENUM.keys.map { |key| new(key) }
50
+ end
51
+
52
+ # @api private
53
+ def find_by_sym(sym)
54
+ all.find { |object| object.to_sym == sym }
55
+ end
56
+
57
+ # @api private
58
+ def find_by_inquirer(method_name)
59
+ method_name =~ /(.+)\?$/ && find_by_sym(Regexp.last_match(1).to_sym)
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ def _value=(value)
66
+ if strict_enum? && !self.class::ENUM.key?(value)
67
+ raise TypeError, "\"#{value}\" is not within the valid values of the enumeration"
68
+ end
69
+
70
+ super
71
+ end
72
+
73
+ def strict_enum?
74
+ false
75
+ end
76
+
77
+ def value_from_sym(sym)
78
+ self.class.find_by_sym(sym)&.to_s
79
+ end
80
+ end
81
+ end
data/lib/gobl/envelope.rb CHANGED
@@ -8,70 +8,31 @@
8
8
 
9
9
  module GOBL
10
10
  # Envelope wraps around a gobl document and provides support for digest creation and digital signatures.
11
- class Envelope < GOBL::Struct
11
+ class Envelope < GOBL::Object
12
12
  # The Schema ID of the GOBL Envelope structure
13
13
  SCHEMA_ID = 'https://gobl.org/draft-0/envelope'
14
14
 
15
- # @!attribute [r] schema
15
+ # @!attribute [r] $schema
16
16
  # Schema identifies the schema that should be used to understand this document
17
17
  # @return [String]
18
- attribute :schema, GOBL::Types::String
18
+ property :$schema, String
19
+ validates :$schema, presence: true
19
20
 
20
21
  # @!attribute [r] head
21
22
  # Details on what the contents are
22
23
  # @return [Header]
23
- attribute :head, Header
24
+ property :head, Header
25
+ validates :head, presence: true
24
26
 
25
27
  # @!attribute [r] doc
26
28
  # The data inside the envelope
27
29
  # @return [Document]
28
- attribute :doc, Document
30
+ property :doc, Document
31
+ validates :doc, presence: true
29
32
 
30
33
  # @!attribute [r] sigs
31
34
  # JSON Web Signatures of the header
32
35
  # @return [Array<GOBL::DSig::Signature>]
33
- attribute? :sigs, GOBL::Types::Array.of(GOBL::DSig::Signature).optional
34
-
35
- # Creates a new object from a hash of GOBL data
36
- #
37
- # @param data [Hash] a hash of GOBL data
38
- #
39
- # @return [Envelope] the object created from the given data
40
- def self.from_gobl!(data)
41
- data = GOBL::Types::Hash[data]
42
-
43
- new(
44
- schema: data['$schema'],
45
- head: Header.from_gobl!(data['head']),
46
- doc: Document.from_gobl!(data['doc']),
47
- sigs: data['sigs']&.map { |item| GOBL::DSig::Signature.from_gobl!(item) }
48
- )
49
- end
50
-
51
- # Returns a hash of GOBL data representing the current object
52
- #
53
- # @return [Hash] the array of GOBL data that represents the current object
54
- def to_gobl
55
- {
56
- '$schema' => attributes[:schema],
57
- 'head' => attributes[:head]&.to_gobl,
58
- 'doc' => attributes[:doc]&.to_gobl,
59
- 'sigs' => attributes[:sigs]&.map { |item| item&.to_gobl }
60
- }.compact
61
- end
62
-
63
- # @!method self.new(attrs)
64
- #
65
- # Returns a {Envelope} object from a given hash of attributes. Nested
66
- # attributes are supported: the constructor will instantiate the proper GOBL
67
- # objects when nested hashes or arrays are given as part of the `attrs`
68
- # parameter.
69
- #
70
- # The `new` method will only allow to create a new object if all attributes
71
- # marked as mandatory and not calculated in the JSON schema are provided.
72
- #
73
- # @param attrs [Hash] the hash of attributes
74
- #
75
- # @return [Envelope] the object corresponding to the given input
36
+ property :sigs, [GOBL::DSig::Signature]
76
37
  end
77
38
  end