gobl 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/lib/ext/hashme.rb +28 -0
  3. data/lib/gobl/bill/advances.rb +1 -77
  4. data/lib/gobl/bill/charge.rb +11 -65
  5. data/lib/gobl/bill/delivery.rb +5 -47
  6. data/lib/gobl/bill/discount.rb +11 -65
  7. data/lib/gobl/bill/exchange_rates.rb +1 -77
  8. data/lib/gobl/bill/invoice.rb +28 -104
  9. data/lib/gobl/bill/invoice_type.rb +3 -134
  10. data/lib/gobl/bill/line.rb +13 -65
  11. data/lib/gobl/bill/line_charge.rb +5 -47
  12. data/lib/gobl/bill/line_discount.rb +5 -47
  13. data/lib/gobl/bill/ordering.rb +2 -38
  14. data/lib/gobl/bill/outlay.rb +11 -59
  15. data/lib/gobl/bill/payment.rb +5 -47
  16. data/lib/gobl/bill/preceding.rb +12 -62
  17. data/lib/gobl/bill/scheme_keys.rb +1 -77
  18. data/lib/gobl/bill/tax.rb +4 -44
  19. data/lib/gobl/bill/totals.rb +17 -71
  20. data/lib/gobl/cal/date.rb +1 -90
  21. data/lib/gobl/cal/period.rb +5 -41
  22. data/lib/gobl/currency/code.rb +3 -134
  23. data/lib/gobl/currency/exchange_rate.rb +5 -41
  24. data/lib/gobl/document.rb +1 -58
  25. data/lib/gobl/dsig/digest.rb +5 -41
  26. data/lib/gobl/dsig/signature.rb +1 -90
  27. data/lib/gobl/enum.rb +81 -0
  28. data/lib/gobl/envelope.rb +9 -48
  29. data/lib/gobl/header.rb +10 -56
  30. data/lib/gobl/i18n/string.rb +1 -58
  31. data/lib/gobl/l10n/code.rb +1 -90
  32. data/lib/gobl/l10n/country_code.rb +3 -134
  33. data/lib/gobl/map.rb +62 -0
  34. data/lib/gobl/note/message.rb +5 -44
  35. data/lib/gobl/num/amount.rb +6 -35
  36. data/lib/gobl/object.rb +21 -0
  37. data/lib/gobl/operations.rb +3 -3
  38. data/lib/gobl/org/address.rb +18 -80
  39. data/lib/gobl/org/code.rb +1 -90
  40. data/lib/gobl/org/coordinates.rb +5 -47
  41. data/lib/gobl/org/email.rb +6 -47
  42. data/lib/gobl/org/inbox.rb +8 -50
  43. data/lib/gobl/org/item.rb +13 -65
  44. data/lib/gobl/org/item_code.rb +4 -41
  45. data/lib/gobl/org/key.rb +1 -90
  46. data/lib/gobl/org/meta.rb +1 -58
  47. data/lib/gobl/org/name.rb +12 -62
  48. data/lib/gobl/org/note.rb +6 -47
  49. data/lib/gobl/org/note_key.rb +3 -134
  50. data/lib/gobl/org/party.rb +14 -71
  51. data/lib/gobl/org/person.rb +9 -56
  52. data/lib/gobl/org/registration.rb +9 -59
  53. data/lib/gobl/org/source_key.rb +3 -134
  54. data/lib/gobl/org/tax_identity.rb +8 -53
  55. data/lib/gobl/org/telephone.rb +5 -44
  56. data/lib/gobl/org/unit.rb +1 -136
  57. data/lib/gobl/pay/advance.rb +11 -59
  58. data/lib/gobl/pay/card.rb +5 -41
  59. data/lib/gobl/pay/credit_transfer.rb +6 -50
  60. data/lib/gobl/pay/direct_debit.rb +4 -44
  61. data/lib/gobl/pay/due_date.rb +8 -50
  62. data/lib/gobl/pay/instructions.rb +11 -62
  63. data/lib/gobl/pay/method_key.rb +3 -134
  64. data/lib/gobl/pay/online.rb +4 -41
  65. data/lib/gobl/pay/term_key.rb +3 -134
  66. data/lib/gobl/pay/terms.rb +6 -47
  67. data/lib/gobl/stamp.rb +5 -41
  68. data/lib/gobl/struct.rb +15 -15
  69. data/lib/gobl/tax/category.rb +9 -50
  70. data/lib/gobl/tax/category_total.rb +11 -53
  71. data/lib/gobl/tax/combo.rb +6 -47
  72. data/lib/gobl/tax/localities.rb +1 -77
  73. data/lib/gobl/tax/locality.rb +6 -44
  74. data/lib/gobl/tax/rate.rb +8 -47
  75. data/lib/gobl/tax/rate_total.rb +9 -50
  76. data/lib/gobl/tax/rate_total_surcharge.rb +5 -41
  77. data/lib/gobl/tax/rate_value.rb +6 -47
  78. data/lib/gobl/tax/region.rb +12 -56
  79. data/lib/gobl/tax/scheme.rb +8 -50
  80. data/lib/gobl/tax/schemes.rb +1 -77
  81. data/lib/gobl/tax/set.rb +1 -77
  82. data/lib/gobl/tax/total.rb +4 -41
  83. data/lib/gobl/uuid/uuid.rb +1 -90
  84. data/lib/gobl/value.rb +58 -0
  85. data/lib/gobl/version.rb +1 -1
  86. data/lib/gobl.rb +3 -1
  87. data/lib/gobl_extensions/document_helper.rb +3 -3
  88. data/lib/gobl_extensions/envelope_helper.rb +5 -0
  89. data/lib/gobl_extensions/i18n/value_keys_helper.rb +3 -3
  90. data/lib/gobl_extensions/tax/region_helper.rb +3 -9
  91. metadata +11 -36
  92. data/home/cavalle/workspace/invopop/gobl.ruby/gobl.gemspec +0 -30
  93. data/lib/gobl/types.rb +0 -17
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
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.