gobl 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +7 -0
  2. data/data/tax/ES.json +841 -0
  3. data/data/tax/FR.json +47 -0
  4. data/data/tax/GB.json +56 -0
  5. data/data/tax/NL.json +60 -0
  6. data/home/cavalle/workspace/invopop/gobl.ruby/gobl.gemspec +30 -0
  7. data/lib/extensions.rb +12 -0
  8. data/lib/gobl/bill/advances.rb +90 -0
  9. data/lib/gobl/bill/charge.rb +121 -0
  10. data/lib/gobl/bill/delivery.rb +79 -0
  11. data/lib/gobl/bill/discount.rb +121 -0
  12. data/lib/gobl/bill/exchange_rates.rb +90 -0
  13. data/lib/gobl/bill/invoice.rb +209 -0
  14. data/lib/gobl/bill/invoice_type.rb +163 -0
  15. data/lib/gobl/bill/line.rb +121 -0
  16. data/lib/gobl/bill/line_charge.rb +79 -0
  17. data/lib/gobl/bill/line_discount.rb +79 -0
  18. data/lib/gobl/bill/ordering.rb +58 -0
  19. data/lib/gobl/bill/outlay.rb +107 -0
  20. data/lib/gobl/bill/payment.rb +79 -0
  21. data/lib/gobl/bill/preceding.rb +114 -0
  22. data/lib/gobl/bill/scheme_keys.rb +90 -0
  23. data/lib/gobl/bill/tax.rb +72 -0
  24. data/lib/gobl/bill/totals.rb +135 -0
  25. data/lib/gobl/cal/date.rb +106 -0
  26. data/lib/gobl/cal/period.rb +63 -0
  27. data/lib/gobl/config.rb +14 -0
  28. data/lib/gobl/currency/code.rb +322 -0
  29. data/lib/gobl/currency/exchange_rate.rb +65 -0
  30. data/lib/gobl/document.rb +70 -0
  31. data/lib/gobl/dsig/digest.rb +65 -0
  32. data/lib/gobl/dsig/signature.rb +106 -0
  33. data/lib/gobl/envelope.rb +77 -0
  34. data/lib/gobl/header.rb +98 -0
  35. data/lib/gobl/i18n/string.rb +72 -0
  36. data/lib/gobl/id.rb +80 -0
  37. data/lib/gobl/l10n/code.rb +106 -0
  38. data/lib/gobl/l10n/country_code.rb +405 -0
  39. data/lib/gobl/note/message.rb +72 -0
  40. data/lib/gobl/num/amount.rb +248 -0
  41. data/lib/gobl/num/percentage.rb +84 -0
  42. data/lib/gobl/operations/service_error.rb +10 -0
  43. data/lib/gobl/operations/validation_result.rb +46 -0
  44. data/lib/gobl/operations.rb +170 -0
  45. data/lib/gobl/org/address.rb +156 -0
  46. data/lib/gobl/org/code.rb +106 -0
  47. data/lib/gobl/org/coordinates.rb +79 -0
  48. data/lib/gobl/org/email.rb +79 -0
  49. data/lib/gobl/org/inbox.rb +86 -0
  50. data/lib/gobl/org/item.rb +121 -0
  51. data/lib/gobl/org/item_code.rb +65 -0
  52. data/lib/gobl/org/key.rb +106 -0
  53. data/lib/gobl/org/meta.rb +72 -0
  54. data/lib/gobl/org/name.rb +114 -0
  55. data/lib/gobl/org/note.rb +79 -0
  56. data/lib/gobl/org/note_key.rb +181 -0
  57. data/lib/gobl/org/party.rb +135 -0
  58. data/lib/gobl/org/person.rb +100 -0
  59. data/lib/gobl/org/registration.rb +99 -0
  60. data/lib/gobl/org/source_key.rb +161 -0
  61. data/lib/gobl/org/tax_identity.rb +93 -0
  62. data/lib/gobl/org/telephone.rb +72 -0
  63. data/lib/gobl/org/unit.rb +204 -0
  64. data/lib/gobl/pay/advance.rb +107 -0
  65. data/lib/gobl/pay/card.rb +65 -0
  66. data/lib/gobl/pay/credit_transfer.rb +86 -0
  67. data/lib/gobl/pay/direct_debit.rb +72 -0
  68. data/lib/gobl/pay/due_date.rb +86 -0
  69. data/lib/gobl/pay/instructions.rb +114 -0
  70. data/lib/gobl/pay/method_key.rb +163 -0
  71. data/lib/gobl/pay/online.rb +65 -0
  72. data/lib/gobl/pay/term_key.rb +166 -0
  73. data/lib/gobl/pay/terms.rb +79 -0
  74. data/lib/gobl/stamp.rb +63 -0
  75. data/lib/gobl/struct.rb +48 -0
  76. data/lib/gobl/tax/category.rb +83 -0
  77. data/lib/gobl/tax/category_total.rb +87 -0
  78. data/lib/gobl/tax/combo.rb +79 -0
  79. data/lib/gobl/tax/localities.rb +90 -0
  80. data/lib/gobl/tax/locality.rb +72 -0
  81. data/lib/gobl/tax/rate.rb +77 -0
  82. data/lib/gobl/tax/rate_total.rb +82 -0
  83. data/lib/gobl/tax/rate_total_surcharge.rb +63 -0
  84. data/lib/gobl/tax/rate_value.rb +79 -0
  85. data/lib/gobl/tax/region.rb +100 -0
  86. data/lib/gobl/tax/scheme.rb +86 -0
  87. data/lib/gobl/tax/schemes.rb +90 -0
  88. data/lib/gobl/tax/set.rb +90 -0
  89. data/lib/gobl/tax/total.rb +65 -0
  90. data/lib/gobl/types.rb +17 -0
  91. data/lib/gobl/uuid/uuid.rb +106 -0
  92. data/lib/gobl/version.rb +5 -0
  93. data/lib/gobl.rb +41 -0
  94. data/lib/gobl_extensions/document_helper.rb +45 -0
  95. data/lib/gobl_extensions/envelope_helper.rb +15 -0
  96. data/lib/gobl_extensions/i18n/value_keys_helper.rb +27 -0
  97. data/lib/gobl_extensions/tax/region_helper.rb +41 -0
  98. metadata +225 -0
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ ## DO NOT EDIT - This file was generated automatically.
5
+ ##
6
+ ## Generated with GOBL v0.30.4
7
+ ##
8
+
9
+ module GOBL
10
+ module Currency
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
13
+ # The Schema ID of the GOBL ExchangeRate structure
14
+ SCHEMA_ID = 'https://gobl.org/draft-0/currency/exchange-rate'
15
+
16
+ # @!attribute [r] currency
17
+ # ISO currency code this rate represents.
18
+ # @return [GOBL::Currency::Code]
19
+ attribute :currency, GOBL::Currency::Code
20
+
21
+ # @!attribute [r] amount
22
+ # How much is 1.00 of this currency worth in the documents currency.
23
+ # @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
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ ## DO NOT EDIT - This file was generated automatically.
5
+ ##
6
+ ## Generated with GOBL v0.30.4
7
+ ##
8
+
9
+ module GOBL
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
69
+ end
70
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ ## DO NOT EDIT - This file was generated automatically.
5
+ ##
6
+ ## Generated with GOBL v0.30.4
7
+ ##
8
+
9
+ module GOBL
10
+ module DSig
11
+ # Digest defines a structure to hold a digest value including the algorithm used to generate it.
12
+ class Digest < GOBL::Struct
13
+ # The Schema ID of the GOBL Digest structure
14
+ SCHEMA_ID = 'https://gobl.org/draft-0/dsig/digest'
15
+
16
+ # @!attribute [r] alg
17
+ # Algorithm stores the algorithm key that was used to generate the value.
18
+ # @return [String]
19
+ attribute :alg, GOBL::Types::String
20
+
21
+ # @!attribute [r] val
22
+ # Value contains the Hexadecimal representation of the resulting hash generated by the algorithm.
23
+ # @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
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ ## DO NOT EDIT - This file was generated automatically.
5
+ ##
6
+ ## Generated with GOBL v0.30.4
7
+ ##
8
+
9
+ module GOBL
10
+ module DSig
11
+ # JSON Web Signature in compact form.
12
+ class Signature < GOBL::Struct
13
+ # The Schema ID of the GOBL Signature structure
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
+ end
105
+ end
106
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ ## DO NOT EDIT - This file was generated automatically.
5
+ ##
6
+ ## Generated with GOBL v0.30.4
7
+ ##
8
+
9
+ module GOBL
10
+ # Envelope wraps around a gobl document and provides support for digest creation and digital signatures.
11
+ class Envelope < GOBL::Struct
12
+ # The Schema ID of the GOBL Envelope structure
13
+ SCHEMA_ID = 'https://gobl.org/draft-0/envelope'
14
+
15
+ # @!attribute [r] schema
16
+ # Schema identifies the schema that should be used to understand this document
17
+ # @return [String]
18
+ attribute :schema, GOBL::Types::String
19
+
20
+ # @!attribute [r] head
21
+ # Details on what the contents are
22
+ # @return [Header]
23
+ attribute :head, Header
24
+
25
+ # @!attribute [r] doc
26
+ # The data inside the envelope
27
+ # @return [Document]
28
+ attribute :doc, Document
29
+
30
+ # @!attribute [r] sigs
31
+ # JSON Web Signatures of the header
32
+ # @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
76
+ end
77
+ end
@@ -0,0 +1,98 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ ## DO NOT EDIT - This file was generated automatically.
5
+ ##
6
+ ## Generated with GOBL v0.30.4
7
+ ##
8
+
9
+ module GOBL
10
+ # Header defines the meta data of the body.
11
+ class Header < GOBL::Struct
12
+ # The Schema ID of the GOBL Header structure
13
+ SCHEMA_ID = 'https://gobl.org/draft-0/envelope#/$defs/Header'
14
+
15
+ # @!attribute [r] uuid
16
+ # Unique UUIDv1 identifier for the envelope.
17
+ # @return [GOBL::UUID::UUID]
18
+ attribute :uuid, GOBL::UUID::UUID
19
+
20
+ # @!attribute [r] dig
21
+ # Digest of the canonical JSON body.
22
+ # @return [GOBL::DSig::Digest]
23
+ attribute :dig, GOBL::DSig::Digest
24
+
25
+ # @!attribute [r] stamps
26
+ # Seals of approval from other organisations.
27
+ # @return [Array<Stamp>]
28
+ attribute? :stamps, GOBL::Types::Array.of(Stamp).optional
29
+
30
+ # @!attribute [r] tags
31
+ # Set of labels that describe but have no influence on the data.
32
+ # @return [Array<String>]
33
+ attribute? :tags, GOBL::Types::Array.of(GOBL::Types::String).optional
34
+
35
+ # @!attribute [r] meta
36
+ # Additional semi-structured information about this envelope.
37
+ # @return [GOBL::Org::Meta]
38
+ attribute? :meta, GOBL::Org::Meta.optional
39
+
40
+ # @!attribute [r] notes
41
+ # Any information that may be relevant to other humans about this envelope
42
+ # @return [String]
43
+ attribute? :notes, GOBL::Types::String.optional
44
+
45
+ # @!attribute [r] draft
46
+ # When true, implies that this document should not be considered final. Digital signatures are optional.
47
+ # @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
97
+ end
98
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ ##
4
+ ## DO NOT EDIT - This file was generated automatically.
5
+ ##
6
+ ## Generated with GOBL v0.30.4
7
+ ##
8
+
9
+ module GOBL
10
+ module I18n
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
70
+ end
71
+ end
72
+ end
data/lib/gobl/id.rb ADDED
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+
5
+ module GOBL
6
+ # Provide a wrapper around Schema IDs (URLs) so that it's easy to extract the type of
7
+ # document we're dealing with
8
+ class ID
9
+ # Initializes a new {GOBL::ID} from a string URI
10
+ #
11
+ # @return [GOBL::ID]
12
+ def initialize(val)
13
+ @id = URI.parse(val)
14
+ end
15
+
16
+ # Returns whether the current schema ID represents a GOBL struct or not
17
+ #
18
+ # @return [Boolean] `true` if the current Schema ID represents a GOBL struct, `false`
19
+ # otherwise
20
+ def gobl?
21
+ @id.host == 'gobl.org'
22
+ end
23
+
24
+ # Returns the fragment part of the URI, without the '#'. This is usually used with
25
+ # schema references
26
+ #
27
+ # @return [String] the fragment part of the schema URI
28
+ def fragment
29
+ @id.fragment
30
+ end
31
+
32
+ # Returns the GOBL schema version
33
+ #
34
+ # @return [String] the schema version
35
+ def version
36
+ @id.path.split('/')[1]
37
+ end
38
+
39
+ # Returns the name of the schema (by convention, the last element of a path)
40
+ #
41
+ # @return [String] the name of the schema
42
+ def name
43
+ @id.path.split('/').last
44
+ end
45
+
46
+ # Returns the module components of the path, which may be empty. The
47
+ # last element of the path is always considered the name of the current schema,
48
+ # and not the module
49
+ #
50
+ # @return [Array<String>] the module components of the schema
51
+ def modules
52
+ @id.path.split('/')[2..-2]
53
+ end
54
+
55
+ # Returns the module components of the path as a single string.
56
+ #
57
+ # @return [String] the module components string
58
+ def module
59
+ modules.join('/')
60
+ end
61
+
62
+ # Converts the schema into a `String`
63
+ #
64
+ # @return [String] the schema as a string
65
+ def to_s
66
+ @id.to_s
67
+ end
68
+
69
+ # Returns whether the current schema is equivalent to a given object. Any object that
70
+ # can be coerced into a string via `#to_s` can be provided.
71
+ #
72
+ # @param other [#to_s] another object to compare the current one with
73
+ #
74
+ # @return [Boolean] `true` if the given other can be considered equivalent to the
75
+ # current schema, `false` otherwise
76
+ def ==(other)
77
+ to_s == other.to_s
78
+ end
79
+ end
80
+ end