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
data/lib/gobl/header.rb CHANGED
@@ -8,91 +8,45 @@
8
8
 
9
9
  module GOBL
10
10
  # Header defines the meta data of the body.
11
- class Header < GOBL::Struct
11
+ class Header < GOBL::Object
12
12
  # The Schema ID of the GOBL Header structure
13
13
  SCHEMA_ID = 'https://gobl.org/draft-0/envelope#/$defs/Header'
14
14
 
15
15
  # @!attribute [r] uuid
16
16
  # Unique UUIDv1 identifier for the envelope.
17
17
  # @return [GOBL::UUID::UUID]
18
- attribute :uuid, GOBL::UUID::UUID
18
+ property :uuid, GOBL::UUID::UUID
19
+ validates :uuid, presence: true
19
20
 
20
21
  # @!attribute [r] dig
21
22
  # Digest of the canonical JSON body.
22
23
  # @return [GOBL::DSig::Digest]
23
- attribute :dig, GOBL::DSig::Digest
24
+ property :dig, GOBL::DSig::Digest
25
+ validates :dig, presence: true
24
26
 
25
27
  # @!attribute [r] stamps
26
28
  # Seals of approval from other organisations.
27
29
  # @return [Array<Stamp>]
28
- attribute? :stamps, GOBL::Types::Array.of(Stamp).optional
30
+ property :stamps, [Stamp]
29
31
 
30
32
  # @!attribute [r] tags
31
33
  # Set of labels that describe but have no influence on the data.
32
34
  # @return [Array<String>]
33
- attribute? :tags, GOBL::Types::Array.of(GOBL::Types::String).optional
35
+ property :tags, [String]
34
36
 
35
37
  # @!attribute [r] meta
36
38
  # Additional semi-structured information about this envelope.
37
39
  # @return [GOBL::Org::Meta]
38
- attribute? :meta, GOBL::Org::Meta.optional
40
+ property :meta, GOBL::Org::Meta
39
41
 
40
42
  # @!attribute [r] notes
41
43
  # Any information that may be relevant to other humans about this envelope
42
44
  # @return [String]
43
- attribute? :notes, GOBL::Types::String.optional
45
+ property :notes, String
44
46
 
45
47
  # @!attribute [r] draft
46
48
  # When true, implies that this document should not be considered final. Digital signatures are optional.
47
49
  # @return [Boolean]
48
- attribute? :draft, GOBL::Types::Bool.optional
49
-
50
- # Creates a new object from a hash of GOBL data
51
- #
52
- # @param data [Hash] a hash of GOBL data
53
- #
54
- # @return [Header] the object created from the given data
55
- def self.from_gobl!(data)
56
- data = GOBL::Types::Hash[data]
57
-
58
- new(
59
- uuid: GOBL::UUID::UUID.from_gobl!(data['uuid']),
60
- dig: GOBL::DSig::Digest.from_gobl!(data['dig']),
61
- stamps: data['stamps']&.map { |item| Stamp.from_gobl!(item) },
62
- tags: data['tags']&.map { |item| item },
63
- meta: data['meta'] ? GOBL::Org::Meta.from_gobl!(data['meta']) : nil,
64
- notes: data['notes'],
65
- draft: data['draft']
66
- )
67
- end
68
-
69
- # Returns a hash of GOBL data representing the current object
70
- #
71
- # @return [Hash] the array of GOBL data that represents the current object
72
- def to_gobl
73
- {
74
- 'uuid' => attributes[:uuid]&.to_gobl,
75
- 'dig' => attributes[:dig]&.to_gobl,
76
- 'stamps' => attributes[:stamps]&.map { |item| item&.to_gobl },
77
- 'tags' => attributes[:tags]&.map { |item| item },
78
- 'meta' => attributes[:meta]&.to_gobl,
79
- 'notes' => attributes[:notes],
80
- 'draft' => attributes[:draft]
81
- }.compact
82
- end
83
-
84
- # @!method self.new(attrs)
85
- #
86
- # Returns a {Header} object from a given hash of attributes. Nested
87
- # attributes are supported: the constructor will instantiate the proper GOBL
88
- # objects when nested hashes or arrays are given as part of the `attrs`
89
- # parameter.
90
- #
91
- # The `new` method will only allow to create a new object if all attributes
92
- # marked as mandatory and not calculated in the JSON schema are provided.
93
- #
94
- # @param attrs [Hash] the hash of attributes
95
- #
96
- # @return [Header] the object corresponding to the given input
50
+ property :draft, Boolean
97
51
  end
98
52
  end
@@ -9,64 +9,7 @@
9
9
  module GOBL
10
10
  module I18n
11
11
  # Map of 2-Letter language codes to their translations.
12
- class String < GOBL::Struct
13
- extend Forwardable
14
- include Enumerable
15
-
16
- # The Schema ID of the GOBL String structure
17
- SCHEMA_ID = 'https://gobl.org/draft-0/i18n/string'
18
-
19
- attribute :_value, GOBL::Types::Hash
20
-
21
- # @!method [](key)
22
- # Returns the value associated with the given key
23
- # @param key [Object] the key
24
- # @return [Object] the value associated with the given key
25
- #
26
- # @!method each
27
- # Iterates over each key-value pair in the map
28
- # @overload each(&block)
29
- # Calls the given block with each key-value pair in the map
30
- # @yield [key, value] a key-value pair of the map
31
- # @return [self] the object itself
32
- # @overload each
33
- # Returns an Enumerator that iterates over the key-value pairs in the map
34
- # @return [Enumerator] the enumerator of the key-value pairs
35
- #
36
- # @!method empty?
37
- # Returns `true` if the number of entires in the map is zero, `false` otherwise.
38
- # @return [Boolean] whether the map is empty or not
39
- #
40
- # @!method length
41
- # Returns the number of entries in the map
42
- # @return [Integer] the number of entries in the map
43
- #
44
- # @!method each_key
45
- # Iterates over each key in the map
46
- # @overload each(&block)
47
- # Calls the given block with each key in the map
48
- # @yield [key] a key of the map
49
- # @return [self] the object itself
50
- # @overload each
51
- # Returns an Enumerator that iterates over the keys in the map
52
- # @return [Enumerator] the enumerator of the keys
53
- def_delegators :_value, :[], :each, :empty?, :length, :each_key
54
-
55
- # Creates a new object from a hash of GOBL data
56
- #
57
- # @param data [Hash] a hash of GOBL data
58
- #
59
- # @return [String] the object created from the given data
60
- def self.from_gobl!(data)
61
- new(_value: data)
62
- end
63
-
64
- # Returns a hash of GOBL data representing the current object
65
- #
66
- # @return [Hash] the hash of GOBL data that represents the current object
67
- def to_gobl
68
- _value
69
- end
12
+ class String < GOBL::Map
70
13
  end
71
14
  end
72
15
  end
@@ -9,98 +9,9 @@
9
9
  module GOBL
10
10
  module L10n
11
11
  # Code is used for short identifies like country or state codes.
12
- class Code < GOBL::Struct
12
+ class Code < GOBL::Value
13
13
  # The Schema ID of the GOBL Code structure
14
14
  SCHEMA_ID = 'https://gobl.org/draft-0/l10n/code'
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 [Code] 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 {Code} 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 [Code] the object corresponding to the given value.
41
- #
42
- # @example Instantiating from a symbol
43
- # code = GOBL::L10n::Code.new(:value)
44
- #
45
- # @example Instantiating from a string
46
- # code = GOBL::L10n::Code.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 {Code} 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 [Code, 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 {Code} object
81
- # code = GOBL::L10n::Code.new('value')
82
- # code == GOBL::L10n::Code.new('value') #=> true
83
- # code == GOBL::L10n::Code.new('other') #=> false
84
- #
85
- # @example Comparing to a string
86
- # code = GOBL::L10n::Code.new('value')
87
- # code == 'value' #=> true
88
- # code == 'other' #=> false
89
- #
90
- # @example Comparing to a symbol
91
- # code = GOBL::L10n::Code.new('value')
92
- # code == :value #=> true
93
- # code == :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
@@ -9,7 +9,7 @@
9
9
  module GOBL
10
10
  module L10n
11
11
  # Defines an ISO 3166-2 country code
12
- class CountryCode < GOBL::Struct
12
+ class CountryCode < GOBL::Enum
13
13
  # The Schema ID of the GOBL CountryCode structure
14
14
  SCHEMA_ID = 'https://gobl.org/draft-0/l10n/country-code'
15
15
 
@@ -266,139 +266,8 @@ module GOBL
266
266
  'ZW' => 'Zimbabwe'
267
267
  }.freeze
268
268
 
269
- attribute :_value, GOBL::Types::String.enum(*ENUM.keys)
270
- private :_value
271
-
272
- # Creates a new object from a GOBL value
273
- #
274
- # @param data [String] the GOBL value
275
- #
276
- # @return [CountryCode] the object created from the given data
277
- def self.from_gobl!(data)
278
- new(_value: data)
279
- end
280
-
281
- # Returns a GOBL value representing the current object
282
- #
283
- # @return [String] the GOBL value that represents the current object
284
- def to_gobl
285
- _value
286
- end
287
-
288
- # Returns a {CountryCode} that corresponds to a given object. The object can be a
289
- # `Symbol`, a `String` or anything coercible into one (via `#to_s`).
290
- #
291
- # @param object [Symbol, String, #to_s] the value of the object.
292
- #
293
- # @return [CountryCode] the object corresponding to the given value.
294
- #
295
- # @example Instantiating from a symbol
296
- # country_code = GOBL::L10n::CountryCode.new(:af)
297
- #
298
- # @example Instantiating from a string
299
- # country_code = GOBL::L10n::CountryCode.new('AF')
300
- def self.new(object)
301
- case object
302
- when Hash, self # internal use, not to be used in public calls
303
- super
304
- when Symbol
305
- new find_by_sym(object)
306
- else
307
- super _value: object.to_s
308
- end
309
- end
310
-
311
- # Returns the string representation of the current object
312
- #
313
- # @return [String] the string representation of the current object
314
- def to_s
315
- _value.to_s
316
- end
317
-
318
- # Returns the symbol representation of the current object
319
- #
320
- # @return [Symbol] the symbol representation of the current object
321
- def to_sym
322
- to_s.parameterize.underscore.to_sym
323
- end
324
-
325
- # Returns whether the current object is equivalent to a given one. In addition
326
- # to {CountryCode} objects, the current object can be compared to a `Symbol`, a
327
- # `String` or anything coercible into one (via `#to_s`)
328
- #
329
- # @param other [CountryCode, Symbol, String, #to_s] the other object to compare to
330
- #
331
- # @return [Boolean] `true` if the objects are equivalent, `false` otherwise
332
- #
333
- # @example Comparing to another {CountryCode} object
334
- # country_code = GOBL::L10n::CountryCode.new('AF')
335
- # country_code == GOBL::L10n::CountryCode.new('AF') #=> true
336
- # country_code == GOBL::L10n::CountryCode.new('AX') #=> false
337
- #
338
- # @example Comparing to a string
339
- # country_code = GOBL::L10n::CountryCode.new('AF')
340
- # country_code == 'AF' #=> true
341
- # country_code == 'AX' #=> false
342
- #
343
- # @example Comparing to a symbol
344
- # country_code = GOBL::L10n::CountryCode.new('AF')
345
- # country_code == :af #=> true
346
- # country_code == :ax #=> false
347
- def ==(other)
348
- case other
349
- when self.class
350
- super
351
- when Symbol
352
- to_sym == other
353
- else
354
- to_s == other.to_s
355
- end
356
- end
357
-
358
- # Returns an array with all the enumerated objects of this type
359
- #
360
- # @return [Array<CountryCode>] the array of enumerated objects
361
- def self.all
362
- ENUM.keys.map { |key| new(key) }
363
- end
364
-
365
- # @api private
366
- def self.find_by_sym(sym)
367
- all.find { |object| object.to_sym == sym }
368
- end
369
-
370
- # @api private
371
- def self.find_by_inquirer(method_name)
372
- method_name =~ /(.+)\?$/ && find_by_sym(Regexp.last_match(1).to_sym)
373
- end
374
-
375
- # Returns the description of the current object
376
- #
377
- # @return [String] the description of the current object
378
- def description
379
- ENUM.fetch(_value, _value)
380
- end
381
-
382
- # @api private
383
- def respond_to_missing?(method_name, include_private = false)
384
- self.class.find_by_inquirer(method_name) || super
385
- end
386
-
387
- # Enables dynamic value inquirers like `CountryCode#af?` for
388
- # each of the declared enum values (see {ENUM}). Each inquirer returns a
389
- # `Boolean` denoting whether the value equals the enquired value (`true`) or
390
- # not (`false`)
391
- #
392
- # @example
393
- # country_code = GOBL::L10n::CountryCode.new('AF')
394
- # country_code.af? # => true
395
- # country_code.ax? # => false
396
- def method_missing(method_name, *args, &block)
397
- if value = self.class.find_by_inquirer(method_name)
398
- self == value
399
- else
400
- super
401
- end
269
+ def strict_enum?
270
+ true
402
271
  end
403
272
  end
404
273
  end
data/lib/gobl/map.rb ADDED
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GOBL
4
+ # Base class for map structures in the GOBL Schema. A map is an unconstrained list of
5
+ # keys and values
6
+ class Map < Struct
7
+ extend Forwardable
8
+ include Enumerable
9
+
10
+ # @api private
11
+ attr_accessor :_map
12
+
13
+ # Initializes a new map struct that corresponds to a given Hash
14
+ #
15
+ # @param map [Hash] the hash with the map data
16
+ #
17
+ # @return [Map] a new map object
18
+ def initialize(map)
19
+ super()
20
+ self._map = map
21
+ end
22
+
23
+ # @api private
24
+ def as_json(...)
25
+ _map.as_json(...)
26
+ end
27
+
28
+ # @!method [](key)
29
+ # Returns the value associated with the given key
30
+ # @param key [Object] the key
31
+ # @return [Object] the value associated with the given key
32
+ #
33
+ # @!method each
34
+ # Iterates over each key-value pair in the map
35
+ # @overload each(&block)
36
+ # Calls the given block with each key-value pair in the map
37
+ # @yield [key, value] a key-value pair of the map
38
+ # @return [self] the object itself
39
+ # @overload each
40
+ # Returns an Enumerator that iterates over the key-value pairs in the map
41
+ # @return [Enumerator] the enumerator of the key-value pairs
42
+ #
43
+ # @!method empty?
44
+ # Returns `true` if the number of entires in the map is zero, `false` otherwise.
45
+ # @return [Boolean] whether the map is empty or not
46
+ #
47
+ # @!method length
48
+ # Returns the number of entries in the map
49
+ # @return [Integer] the number of entries in the map
50
+ #
51
+ # @!method each_key
52
+ # Iterates over each key in the map
53
+ # @overload each(&block)
54
+ # Calls the given block with each key in the map
55
+ # @yield [key] a key of the map
56
+ # @return [self] the object itself
57
+ # @overload each
58
+ # Returns an Enumerator that iterates over the keys in the map
59
+ # @return [Enumerator] the enumerator of the keys
60
+ def_delegators :_map, :[], :each, :empty?, :length, :each_key
61
+ end
62
+ end
@@ -9,64 +9,25 @@
9
9
  module GOBL
10
10
  module Note
11
11
  # Message represents the minimum possible contents for a GoBL document type.
12
- class Message < GOBL::Struct
12
+ class Message < GOBL::Object
13
13
  # The Schema ID of the GOBL Message structure
14
14
  SCHEMA_ID = 'https://gobl.org/draft-0/note/message'
15
15
 
16
16
  # @!attribute [r] title
17
17
  # Summary of the message content
18
18
  # @return [String]
19
- attribute? :title, GOBL::Types::String.optional
19
+ property :title, String
20
20
 
21
21
  # @!attribute [r] content
22
22
  # Details of what exactly this message wants to communicate
23
23
  # @return [String]
24
- attribute :content, GOBL::Types::String
24
+ property :content, String
25
+ validates :content, presence: true
25
26
 
26
27
  # @!attribute [r] meta
27
28
  # Any additional semi-structured data that might be useful.
28
29
  # @return [GOBL::Org::Meta]
29
- attribute? :meta, GOBL::Org::Meta.optional
30
-
31
- # Creates a new object from a hash of GOBL data
32
- #
33
- # @param data [Hash] a hash of GOBL data
34
- #
35
- # @return [Message] the object created from the given data
36
- def self.from_gobl!(data)
37
- data = GOBL::Types::Hash[data]
38
-
39
- new(
40
- title: data['title'],
41
- content: data['content'],
42
- meta: data['meta'] ? GOBL::Org::Meta.from_gobl!(data['meta']) : nil
43
- )
44
- end
45
-
46
- # Returns a hash of GOBL data representing the current object
47
- #
48
- # @return [Hash] the array of GOBL data that represents the current object
49
- def to_gobl
50
- {
51
- 'title' => attributes[:title],
52
- 'content' => attributes[:content],
53
- 'meta' => attributes[:meta]&.to_gobl
54
- }.compact
55
- end
56
-
57
- # @!method self.new(attrs)
58
- #
59
- # Returns a {Message} object from a given hash of attributes. Nested
60
- # attributes are supported: the constructor will instantiate the proper GOBL
61
- # objects when nested hashes or arrays are given as part of the `attrs`
62
- # parameter.
63
- #
64
- # The `new` method will only allow to create a new object if all attributes
65
- # marked as mandatory and not calculated in the JSON schema are provided.
66
- #
67
- # @param attrs [Hash] the hash of attributes
68
- #
69
- # @return [Message] the object corresponding to the given input
30
+ property :meta, GOBL::Org::Meta
70
31
  end
71
32
  end
72
33
  end
@@ -3,7 +3,7 @@
3
3
  module GOBL
4
4
  module Num
5
5
  # Represents a numeric quantity with optional decimal places that determine accuracy
6
- class Amount
6
+ class Amount < GOBL::Struct
7
7
  # The integer value of the amount
8
8
  attr_reader :value
9
9
 
@@ -33,40 +33,6 @@ module GOBL
33
33
  end
34
34
  end
35
35
 
36
- # Creates a new {Amount} from a GOBL value
37
- #
38
- # @param data [Hash] a GOBL value
39
- #
40
- # @return [Amount] the object created from the given data
41
- def self.from_gobl!(data)
42
- new(data)
43
- end
44
-
45
- # Deserializes an {Amount} from a JSON string
46
- #
47
- # @param json [String] a JSON string
48
- #
49
- # @return [Amount] the deserialized {Amount}
50
- def self.from_json!(json)
51
- from_gobl!(JSON.parse(json))
52
- end
53
-
54
- # Returns a GOBL value representing the current amount
55
- #
56
- # @return [String] the GOBL value that represents the current amount
57
- def to_gobl
58
- to_s
59
- end
60
-
61
- # Serializes a GOBL struct into a JSON string
62
- #
63
- # @param options [#to_h] a Hash-like object to pass to `JSON.generate`
64
- #
65
- # @return [GOBL::Struct] the JSON string representing the GOBL struct
66
- def to_json(options = nil)
67
- JSON.generate(to_gobl, options)
68
- end
69
-
70
36
  # Returns the string representation of the current amount
71
37
  #
72
38
  # @return [String] the string representation of the current amount
@@ -222,6 +188,11 @@ module GOBL
222
188
  self.class.new(value: -value, exp: exp)
223
189
  end
224
190
 
191
+ #  @api private
192
+ def as_json(*)
193
+ to_s
194
+ end
195
+
225
196
  protected
226
197
 
227
198
  # Extract the value and exponent from the string.
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GOBL
4
+ # Base class for object structs with properties in the GOBL Schema
5
+ class Object < Struct
6
+ include Hashme
7
+
8
+ # @!method self.new(attrs)
9
+ #
10
+ # Returns a GOBL object from a given hash of attributes. Nested attributes are
11
+ # supported: the constructor will instantiate the proper GOBL objects when nested
12
+ # hashes or arrays are given as part of the `attrs` parameter.
13
+ #
14
+ # The `new` method will only allow to create a new object if all attributes marked
15
+ # as mandatory and not calculated in the JSON schema are provided.
16
+ #
17
+ # @param attrs [Hash] the hash of attributes
18
+ #
19
+ # @return [Object] the object corresponding to the given input
20
+ end
21
+ end
@@ -10,7 +10,7 @@ module GOBL
10
10
  # GOBL.config.service_host = 'localhost'
11
11
  # GOBL.config.service_port = 8080
12
12
  #
13
- # doc = GOBL::Document.from_gobl!(
13
+ # doc = GOBL::Document.new(
14
14
  # '$schema' => 'https://gobl.org/draft-0/bill/invoice',
15
15
  # 'code' => 'SAMPLE-001',
16
16
  # 'currency' => 'EUR',
@@ -70,7 +70,7 @@ module GOBL
70
70
 
71
71
  raise ServiceError, response['error'] if response['error'].present?
72
72
 
73
- GOBL::Struct.from_gobl! response['payload']
73
+ GOBL::Struct.from_data response['payload']
74
74
  end
75
75
 
76
76
  # Checks whether or not a document or envelope is valid according to the GOBL schema
@@ -125,7 +125,7 @@ module GOBL
125
125
 
126
126
  raise ServiceError, response['error'] if response['error'].present?
127
127
 
128
- GOBL::Struct.from_gobl! response['payload']
128
+ GOBL::Struct.from_data response['payload']
129
129
  end
130
130
 
131
131
  private