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.
- checksums.yaml +4 -4
- data/data/tax/CO.json +420 -0
- data/data/tax/ES.json +61 -0
- data/lib/ext/hashme.rb +28 -0
- data/lib/gobl/bill/advances.rb +1 -77
- data/lib/gobl/bill/charge.rb +11 -65
- data/lib/gobl/bill/delivery.rb +5 -47
- data/lib/gobl/bill/discount.rb +11 -65
- data/lib/gobl/bill/exchange_rates.rb +1 -77
- data/lib/gobl/bill/invoice.rb +28 -104
- data/lib/gobl/bill/invoice_type.rb +3 -134
- data/lib/gobl/bill/line.rb +13 -65
- data/lib/gobl/bill/line_charge.rb +5 -47
- data/lib/gobl/bill/line_discount.rb +5 -47
- data/lib/gobl/bill/ordering.rb +2 -38
- data/lib/gobl/bill/outlay.rb +11 -59
- data/lib/gobl/bill/payment.rb +5 -47
- data/lib/gobl/bill/preceding.rb +12 -62
- data/lib/gobl/bill/scheme_keys.rb +1 -77
- data/lib/gobl/bill/tax.rb +4 -44
- data/lib/gobl/bill/totals.rb +17 -71
- data/lib/gobl/cal/date.rb +1 -90
- data/lib/gobl/cal/period.rb +5 -41
- data/lib/gobl/currency/code.rb +3 -134
- data/lib/gobl/currency/exchange_rate.rb +5 -41
- data/lib/gobl/document.rb +1 -58
- data/lib/gobl/dsig/digest.rb +5 -41
- data/lib/gobl/dsig/signature.rb +1 -90
- data/lib/gobl/enum.rb +81 -0
- data/lib/gobl/envelope.rb +9 -48
- data/lib/gobl/header.rb +10 -56
- data/lib/gobl/i18n/string.rb +1 -58
- data/lib/gobl/l10n/code.rb +1 -90
- data/lib/gobl/l10n/country_code.rb +3 -134
- data/lib/gobl/map.rb +62 -0
- data/lib/gobl/note/message.rb +5 -44
- data/lib/gobl/num/amount.rb +6 -35
- data/lib/gobl/object.rb +21 -0
- data/lib/gobl/operations.rb +3 -3
- data/lib/gobl/org/address.rb +18 -80
- data/lib/gobl/org/code.rb +1 -90
- data/lib/gobl/org/coordinates.rb +5 -47
- data/lib/gobl/org/email.rb +6 -47
- data/lib/gobl/org/inbox.rb +8 -50
- data/lib/gobl/org/item.rb +13 -65
- data/lib/gobl/org/item_code.rb +4 -41
- data/lib/gobl/org/key.rb +1 -90
- data/lib/gobl/org/meta.rb +1 -58
- data/lib/gobl/org/name.rb +12 -62
- data/lib/gobl/org/note.rb +6 -47
- data/lib/gobl/org/note_key.rb +3 -134
- data/lib/gobl/org/party.rb +14 -71
- data/lib/gobl/org/person.rb +9 -56
- data/lib/gobl/org/registration.rb +9 -59
- data/lib/gobl/org/source_key.rb +3 -134
- data/lib/gobl/org/tax_identity.rb +8 -53
- data/lib/gobl/org/telephone.rb +5 -44
- data/lib/gobl/org/unit.rb +1 -136
- data/lib/gobl/pay/advance.rb +11 -59
- data/lib/gobl/pay/card.rb +5 -41
- data/lib/gobl/pay/credit_transfer.rb +6 -50
- data/lib/gobl/pay/direct_debit.rb +4 -44
- data/lib/gobl/pay/due_date.rb +8 -50
- data/lib/gobl/pay/instructions.rb +11 -62
- data/lib/gobl/pay/method_key.rb +3 -134
- data/lib/gobl/pay/online.rb +4 -41
- data/lib/gobl/pay/term_key.rb +3 -134
- data/lib/gobl/pay/terms.rb +6 -47
- data/lib/gobl/stamp.rb +5 -41
- data/lib/gobl/struct.rb +15 -15
- data/lib/gobl/tax/category.rb +9 -50
- data/lib/gobl/tax/category_total.rb +11 -53
- data/lib/gobl/tax/combo.rb +6 -47
- data/lib/gobl/tax/localities.rb +1 -77
- data/lib/gobl/tax/locality.rb +6 -44
- data/lib/gobl/tax/rate.rb +8 -47
- data/lib/gobl/tax/rate_total.rb +9 -50
- data/lib/gobl/tax/rate_total_surcharge.rb +5 -41
- data/lib/gobl/tax/rate_value.rb +6 -47
- data/lib/gobl/tax/region.rb +12 -56
- data/lib/gobl/tax/scheme.rb +8 -50
- data/lib/gobl/tax/schemes.rb +1 -77
- data/lib/gobl/tax/set.rb +1 -77
- data/lib/gobl/tax/total.rb +4 -41
- data/lib/gobl/uuid/uuid.rb +1 -90
- data/lib/gobl/value.rb +58 -0
- data/lib/gobl/version.rb +1 -1
- data/lib/gobl.rb +3 -1
- data/lib/gobl_extensions/document_helper.rb +3 -3
- data/lib/gobl_extensions/envelope_helper.rb +5 -0
- data/lib/gobl_extensions/i18n/value_keys_helper.rb +3 -3
- data/lib/gobl_extensions/tax/region_helper.rb +3 -9
- metadata +12 -35
- data/lib/gobl/types.rb +0 -17
data/lib/gobl/tax/region.rb
CHANGED
@@ -9,92 +9,48 @@
|
|
9
9
|
module GOBL
|
10
10
|
module Tax
|
11
11
|
# Region defines the holding structure for a regions categories and subsequent Rates and Values.
|
12
|
-
class Region < GOBL::
|
12
|
+
class Region < GOBL::Object
|
13
13
|
# The Schema ID of the GOBL Region structure
|
14
14
|
SCHEMA_ID = 'https://gobl.org/draft-0/tax/region'
|
15
15
|
|
16
16
|
# @!attribute [r] name
|
17
17
|
# Name of the region
|
18
18
|
# @return [GOBL::I18n::String]
|
19
|
-
|
19
|
+
property :name, GOBL::I18n::String
|
20
|
+
validates :name, presence: true
|
20
21
|
|
21
22
|
# @!attribute [r] country
|
22
23
|
# Country code for the region
|
23
24
|
# @return [GOBL::L10n::CountryCode]
|
24
|
-
|
25
|
+
property :country, GOBL::L10n::CountryCode
|
26
|
+
validates :country, presence: true
|
25
27
|
|
26
28
|
# @!attribute [r] locality
|
27
29
|
# Locality, city, province, county, or similar code inside the country, if needed.
|
28
30
|
# @return [GOBL::L10n::Code]
|
29
|
-
|
31
|
+
property :locality, GOBL::L10n::Code
|
30
32
|
|
31
33
|
# @!attribute [r] localities
|
32
34
|
# List of sub-localities inside a region.
|
33
35
|
# @return [Localities]
|
34
|
-
|
36
|
+
property :localities, Localities
|
35
37
|
|
36
38
|
# @!attribute [r] currency
|
37
39
|
# Currency used by the region for tax purposes.
|
38
40
|
# @return [GOBL::Currency::Code]
|
39
|
-
|
41
|
+
property :currency, GOBL::Currency::Code
|
42
|
+
validates :currency, presence: true
|
40
43
|
|
41
44
|
# @!attribute [r] schemes
|
42
45
|
# Set of specific scheme definitions inside the region.
|
43
46
|
# @return [Schemes]
|
44
|
-
|
47
|
+
property :schemes, Schemes
|
45
48
|
|
46
49
|
# @!attribute [r] categories
|
47
50
|
# List of tax categories.
|
48
51
|
# @return [Array<Category>]
|
49
|
-
|
50
|
-
|
51
|
-
# Creates a new object from a hash of GOBL data
|
52
|
-
#
|
53
|
-
# @param data [Hash] a hash of GOBL data
|
54
|
-
#
|
55
|
-
# @return [Region] the object created from the given data
|
56
|
-
def self.from_gobl!(data)
|
57
|
-
data = GOBL::Types::Hash[data]
|
58
|
-
|
59
|
-
new(
|
60
|
-
name: GOBL::I18n::String.from_gobl!(data['name']),
|
61
|
-
country: GOBL::L10n::CountryCode.from_gobl!(data['country']),
|
62
|
-
locality: data['locality'] ? GOBL::L10n::Code.from_gobl!(data['locality']) : nil,
|
63
|
-
localities: data['localities'] ? Localities.from_gobl!(data['localities']) : nil,
|
64
|
-
currency: GOBL::Currency::Code.from_gobl!(data['currency']),
|
65
|
-
schemes: data['schemes'] ? Schemes.from_gobl!(data['schemes']) : nil,
|
66
|
-
categories: data['categories']&.map { |item| Category.from_gobl!(item) }
|
67
|
-
)
|
68
|
-
end
|
69
|
-
|
70
|
-
# Returns a hash of GOBL data representing the current object
|
71
|
-
#
|
72
|
-
# @return [Hash] the array of GOBL data that represents the current object
|
73
|
-
def to_gobl
|
74
|
-
{
|
75
|
-
'name' => attributes[:name]&.to_gobl,
|
76
|
-
'country' => attributes[:country]&.to_gobl,
|
77
|
-
'locality' => attributes[:locality]&.to_gobl,
|
78
|
-
'localities' => attributes[:localities]&.to_gobl,
|
79
|
-
'currency' => attributes[:currency]&.to_gobl,
|
80
|
-
'schemes' => attributes[:schemes]&.to_gobl,
|
81
|
-
'categories' => attributes[:categories]&.map { |item| item&.to_gobl }
|
82
|
-
}.compact
|
83
|
-
end
|
84
|
-
|
85
|
-
# @!method self.new(attrs)
|
86
|
-
#
|
87
|
-
# Returns a {Region} object from a given hash of attributes. Nested
|
88
|
-
# attributes are supported: the constructor will instantiate the proper GOBL
|
89
|
-
# objects when nested hashes or arrays are given as part of the `attrs`
|
90
|
-
# parameter.
|
91
|
-
#
|
92
|
-
# The `new` method will only allow to create a new object if all attributes
|
93
|
-
# marked as mandatory and not calculated in the JSON schema are provided.
|
94
|
-
#
|
95
|
-
# @param attrs [Hash] the hash of attributes
|
96
|
-
#
|
97
|
-
# @return [Region] the object corresponding to the given input
|
52
|
+
property :categories, [Category]
|
53
|
+
validates :categories, presence: true
|
98
54
|
end
|
99
55
|
end
|
100
56
|
end
|
data/lib/gobl/tax/scheme.rb
CHANGED
@@ -9,78 +9,36 @@
|
|
9
9
|
module GOBL
|
10
10
|
module Tax
|
11
11
|
# Scheme contains the definition of a scheme that belongs to a region and can be used to simplify validation processes for document contents.
|
12
|
-
class Scheme < GOBL::
|
12
|
+
class Scheme < GOBL::Object
|
13
13
|
# The Schema ID of the GOBL Scheme structure
|
14
14
|
SCHEMA_ID = 'https://gobl.org/draft-0/tax/region#/$defs/Scheme'
|
15
15
|
|
16
16
|
# @!attribute [r] key
|
17
17
|
# Key used to identify this scheme
|
18
18
|
# @return [GOBL::Org::Key]
|
19
|
-
|
19
|
+
property :key, GOBL::Org::Key
|
20
|
+
validates :key, presence: true
|
20
21
|
|
21
22
|
# @!attribute [r] name
|
22
23
|
# Name of this scheme.
|
23
24
|
# @return [GOBL::I18n::String]
|
24
|
-
|
25
|
+
property :name, GOBL::I18n::String
|
26
|
+
validates :name, presence: true
|
25
27
|
|
26
28
|
# @!attribute [r] description
|
27
29
|
# Human details describing what this scheme is used for.
|
28
30
|
# @return [GOBL::I18n::String]
|
29
|
-
|
31
|
+
property :description, GOBL::I18n::String
|
30
32
|
|
31
33
|
# @!attribute [r] categories
|
32
34
|
# List of tax category codes that can be used when this scheme is applied.
|
33
35
|
# @return [Array<GOBL::Org::Code>]
|
34
|
-
|
36
|
+
property :categories, [GOBL::Org::Code]
|
35
37
|
|
36
38
|
# @!attribute [r] note
|
37
39
|
# Note defines a message that should be added to a document when this scheme is used.
|
38
40
|
# @return [GOBL::Org::Note]
|
39
|
-
|
40
|
-
|
41
|
-
# Creates a new object from a hash of GOBL data
|
42
|
-
#
|
43
|
-
# @param data [Hash] a hash of GOBL data
|
44
|
-
#
|
45
|
-
# @return [Scheme] the object created from the given data
|
46
|
-
def self.from_gobl!(data)
|
47
|
-
data = GOBL::Types::Hash[data]
|
48
|
-
|
49
|
-
new(
|
50
|
-
key: GOBL::Org::Key.from_gobl!(data['key']),
|
51
|
-
name: GOBL::I18n::String.from_gobl!(data['name']),
|
52
|
-
description: data['description'] ? GOBL::I18n::String.from_gobl!(data['description']) : nil,
|
53
|
-
categories: data['categories']&.map { |item| GOBL::Org::Code.from_gobl!(item) },
|
54
|
-
note: data['note'] ? GOBL::Org::Note.from_gobl!(data['note']) : nil
|
55
|
-
)
|
56
|
-
end
|
57
|
-
|
58
|
-
# Returns a hash of GOBL data representing the current object
|
59
|
-
#
|
60
|
-
# @return [Hash] the array of GOBL data that represents the current object
|
61
|
-
def to_gobl
|
62
|
-
{
|
63
|
-
'key' => attributes[:key]&.to_gobl,
|
64
|
-
'name' => attributes[:name]&.to_gobl,
|
65
|
-
'description' => attributes[:description]&.to_gobl,
|
66
|
-
'categories' => attributes[:categories]&.map { |item| item&.to_gobl },
|
67
|
-
'note' => attributes[:note]&.to_gobl
|
68
|
-
}.compact
|
69
|
-
end
|
70
|
-
|
71
|
-
# @!method self.new(attrs)
|
72
|
-
#
|
73
|
-
# Returns a {Scheme} object from a given hash of attributes. Nested
|
74
|
-
# attributes are supported: the constructor will instantiate the proper GOBL
|
75
|
-
# objects when nested hashes or arrays are given as part of the `attrs`
|
76
|
-
# parameter.
|
77
|
-
#
|
78
|
-
# The `new` method will only allow to create a new object if all attributes
|
79
|
-
# marked as mandatory and not calculated in the JSON schema are provided.
|
80
|
-
#
|
81
|
-
# @param attrs [Hash] the hash of attributes
|
82
|
-
#
|
83
|
-
# @return [Scheme] the object corresponding to the given input
|
41
|
+
property :note, GOBL::Org::Note
|
84
42
|
end
|
85
43
|
end
|
86
44
|
end
|
data/lib/gobl/tax/schemes.rb
CHANGED
@@ -9,82 +9,6 @@
|
|
9
9
|
module GOBL
|
10
10
|
module Tax
|
11
11
|
# Schemes defines an array of scheme objects with helper functions.
|
12
|
-
|
13
|
-
extend Forwardable
|
14
|
-
include Enumerable
|
15
|
-
|
16
|
-
# The Schema ID of the GOBL Schemes structure
|
17
|
-
SCHEMA_ID = 'https://gobl.org/draft-0/tax/region#/$defs/Schemes'
|
18
|
-
|
19
|
-
attribute :_ary, GOBL::Types::Array.of(Scheme)
|
20
|
-
|
21
|
-
# @!method [](*args)
|
22
|
-
# Returns elements from the array
|
23
|
-
# @overload [](index)
|
24
|
-
# Returns the element in a specific position
|
25
|
-
# @param index [Integer] the position of the object
|
26
|
-
# @return [Scheme] the element in the `index` position
|
27
|
-
# @overload [](start, length)
|
28
|
-
# Returns all the elements within a range of positions
|
29
|
-
# @param start [Integer] start position of the range
|
30
|
-
# @param length [Integer] length of the range
|
31
|
-
# @return [Array<Scheme>] the elements in the range
|
32
|
-
# @overload [](range)
|
33
|
-
# Returns all the elements within a range
|
34
|
-
# @param range [Range] start and end positions of the range
|
35
|
-
# @return [Array<Scheme>] the elements in the range
|
36
|
-
#
|
37
|
-
# @!method each
|
38
|
-
# Iterates over the array elements
|
39
|
-
# @overload each(&block)
|
40
|
-
# Calls the given block with each element in the array
|
41
|
-
# @yield [element] element of the array
|
42
|
-
# @return [self] the object itself
|
43
|
-
# @overload each
|
44
|
-
# Returns an Enumerator that iterates over the array elements
|
45
|
-
# @return [Enumerator] the enumerator of the array elements
|
46
|
-
#
|
47
|
-
# @!method empty?
|
48
|
-
# Returns `true` if the number of elements in the array is zero, `false` otherwise.
|
49
|
-
# @return [Boolean] whether the array is empty or not
|
50
|
-
#
|
51
|
-
# @!method length
|
52
|
-
# Returns the number of elements in the array
|
53
|
-
# @return [Integer] the number of elements in the array
|
54
|
-
def_delegators :_ary, :[], :each, :empty?, :length
|
55
|
-
|
56
|
-
# Creates a new object from an array of GOBL data
|
57
|
-
#
|
58
|
-
# @param data [Array] an array of GOBL data
|
59
|
-
#
|
60
|
-
# @return [Schemes] the object created from the given data
|
61
|
-
def self.from_gobl!(data)
|
62
|
-
new(_ary: data&.map { |item| Scheme.from_gobl!(item) })
|
63
|
-
end
|
64
|
-
|
65
|
-
# Returns an array of GOBL data representing the current object
|
66
|
-
#
|
67
|
-
# @return [Array<Hash>] the array of GOBL data that represents the current object
|
68
|
-
def to_gobl
|
69
|
-
_ary.map(&:to_gobl)
|
70
|
-
end
|
71
|
-
|
72
|
-
# Returns a {Schemes} object from a given array of structs. The array may
|
73
|
-
# contain {Scheme} objects or hashes. If hashes are provided, the constructor
|
74
|
-
# will call `Scheme#new` to generate the objects that will be part of the
|
75
|
-
# returned {Schemes} object
|
76
|
-
#
|
77
|
-
# @param object [Array<Scheme, Hash>] the array containing the structs
|
78
|
-
#
|
79
|
-
# @return [Schemes] the object corresponding to the given input
|
80
|
-
def self.new(object)
|
81
|
-
case object
|
82
|
-
when Array
|
83
|
-
super _ary: object
|
84
|
-
else # internal use, not to be used in public calls
|
85
|
-
super
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
12
|
+
Schemes = [Scheme] # rubocop:disable Naming/ConstantName, Style/MutableConstant
|
89
13
|
end
|
90
14
|
end
|
data/lib/gobl/tax/set.rb
CHANGED
@@ -9,82 +9,6 @@
|
|
9
9
|
module GOBL
|
10
10
|
module Tax
|
11
11
|
# Set defines a list of tax categories and their rates to be used alongside taxable items.
|
12
|
-
|
13
|
-
extend Forwardable
|
14
|
-
include Enumerable
|
15
|
-
|
16
|
-
# The Schema ID of the GOBL Set structure
|
17
|
-
SCHEMA_ID = 'https://gobl.org/draft-0/tax/set'
|
18
|
-
|
19
|
-
attribute :_ary, GOBL::Types::Array.of(Combo)
|
20
|
-
|
21
|
-
# @!method [](*args)
|
22
|
-
# Returns elements from the array
|
23
|
-
# @overload [](index)
|
24
|
-
# Returns the element in a specific position
|
25
|
-
# @param index [Integer] the position of the object
|
26
|
-
# @return [Combo] the element in the `index` position
|
27
|
-
# @overload [](start, length)
|
28
|
-
# Returns all the elements within a range of positions
|
29
|
-
# @param start [Integer] start position of the range
|
30
|
-
# @param length [Integer] length of the range
|
31
|
-
# @return [Array<Combo>] the elements in the range
|
32
|
-
# @overload [](range)
|
33
|
-
# Returns all the elements within a range
|
34
|
-
# @param range [Range] start and end positions of the range
|
35
|
-
# @return [Array<Combo>] the elements in the range
|
36
|
-
#
|
37
|
-
# @!method each
|
38
|
-
# Iterates over the array elements
|
39
|
-
# @overload each(&block)
|
40
|
-
# Calls the given block with each element in the array
|
41
|
-
# @yield [element] element of the array
|
42
|
-
# @return [self] the object itself
|
43
|
-
# @overload each
|
44
|
-
# Returns an Enumerator that iterates over the array elements
|
45
|
-
# @return [Enumerator] the enumerator of the array elements
|
46
|
-
#
|
47
|
-
# @!method empty?
|
48
|
-
# Returns `true` if the number of elements in the array is zero, `false` otherwise.
|
49
|
-
# @return [Boolean] whether the array is empty or not
|
50
|
-
#
|
51
|
-
# @!method length
|
52
|
-
# Returns the number of elements in the array
|
53
|
-
# @return [Integer] the number of elements in the array
|
54
|
-
def_delegators :_ary, :[], :each, :empty?, :length
|
55
|
-
|
56
|
-
# Creates a new object from an array of GOBL data
|
57
|
-
#
|
58
|
-
# @param data [Array] an array of GOBL data
|
59
|
-
#
|
60
|
-
# @return [Set] the object created from the given data
|
61
|
-
def self.from_gobl!(data)
|
62
|
-
new(_ary: data&.map { |item| Combo.from_gobl!(item) })
|
63
|
-
end
|
64
|
-
|
65
|
-
# Returns an array of GOBL data representing the current object
|
66
|
-
#
|
67
|
-
# @return [Array<Hash>] the array of GOBL data that represents the current object
|
68
|
-
def to_gobl
|
69
|
-
_ary.map(&:to_gobl)
|
70
|
-
end
|
71
|
-
|
72
|
-
# Returns a {Set} object from a given array of structs. The array may
|
73
|
-
# contain {Combo} objects or hashes. If hashes are provided, the constructor
|
74
|
-
# will call `Combo#new` to generate the objects that will be part of the
|
75
|
-
# returned {Set} object
|
76
|
-
#
|
77
|
-
# @param object [Array<Combo, Hash>] the array containing the structs
|
78
|
-
#
|
79
|
-
# @return [Set] the object corresponding to the given input
|
80
|
-
def self.new(object)
|
81
|
-
case object
|
82
|
-
when Array
|
83
|
-
super _ary: object
|
84
|
-
else # internal use, not to be used in public calls
|
85
|
-
super
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
12
|
+
Set = [Combo] # rubocop:disable Naming/ConstantName, Style/MutableConstant
|
89
13
|
end
|
90
14
|
end
|
data/lib/gobl/tax/total.rb
CHANGED
@@ -9,57 +9,20 @@
|
|
9
9
|
module GOBL
|
10
10
|
module Tax
|
11
11
|
# Total contains a set of Category Totals which in turn contain all the accumulated taxes contained in the document.
|
12
|
-
class Total < GOBL::
|
12
|
+
class Total < GOBL::Object
|
13
13
|
# The Schema ID of the GOBL Total structure
|
14
14
|
SCHEMA_ID = 'https://gobl.org/draft-0/tax/total'
|
15
15
|
|
16
16
|
# @!attribute [r] categories
|
17
17
|
# Grouping of all the taxes by their category
|
18
18
|
# @return [Array<CategoryTotal>]
|
19
|
-
|
19
|
+
property :categories, [CategoryTotal]
|
20
20
|
|
21
21
|
# @!attribute [r] sum
|
22
22
|
# Total value of all the taxes applied.
|
23
23
|
# @return [GOBL::Num::Amount]
|
24
|
-
|
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 [Total] the object created from the given data
|
31
|
-
def self.from_gobl!(data)
|
32
|
-
data = GOBL::Types::Hash[data]
|
33
|
-
|
34
|
-
new(
|
35
|
-
categories: data['categories']&.map { |item| CategoryTotal.from_gobl!(item) },
|
36
|
-
sum: data['sum']
|
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
|
-
'categories' => attributes[:categories]&.map { |item| item&.to_gobl },
|
46
|
-
'sum' => attributes[:sum]&.to_gobl
|
47
|
-
}.compact
|
48
|
-
end
|
49
|
-
|
50
|
-
# @!method self.new(attrs)
|
51
|
-
#
|
52
|
-
# Returns a {Total} 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 [Total] the object corresponding to the given input
|
24
|
+
property :sum, GOBL::Num::Amount
|
25
|
+
validates :sum, presence: true
|
63
26
|
end
|
64
27
|
end
|
65
28
|
end
|
data/lib/gobl/uuid/uuid.rb
CHANGED
@@ -9,98 +9,9 @@
|
|
9
9
|
module GOBL
|
10
10
|
module UUID
|
11
11
|
# Universally Unique Identifier. We only recommend using versions 1 and 4 within GOBL.
|
12
|
-
class UUID < GOBL::
|
12
|
+
class UUID < GOBL::Value
|
13
13
|
# The Schema ID of the GOBL UUID structure
|
14
14
|
SCHEMA_ID = 'https://gobl.org/draft-0/uuid/uuid'
|
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 [UUID] 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 {UUID} 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 [UUID] the object corresponding to the given value.
|
41
|
-
#
|
42
|
-
# @example Instantiating from a symbol
|
43
|
-
# uuid = GOBL::UUID::UUID.new(:value)
|
44
|
-
#
|
45
|
-
# @example Instantiating from a string
|
46
|
-
# uuid = GOBL::UUID::UUID.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 {UUID} 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 [UUID, 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 {UUID} object
|
81
|
-
# uuid = GOBL::UUID::UUID.new('value')
|
82
|
-
# uuid == GOBL::UUID::UUID.new('value') #=> true
|
83
|
-
# uuid == GOBL::UUID::UUID.new('other') #=> false
|
84
|
-
#
|
85
|
-
# @example Comparing to a string
|
86
|
-
# uuid = GOBL::UUID::UUID.new('value')
|
87
|
-
# uuid == 'value' #=> true
|
88
|
-
# uuid == 'other' #=> false
|
89
|
-
#
|
90
|
-
# @example Comparing to a symbol
|
91
|
-
# uuid = GOBL::UUID::UUID.new('value')
|
92
|
-
# uuid == :value #=> true
|
93
|
-
# uuid == :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/value.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GOBL
|
4
|
+
# Base class for single value structs in the GOBL Schema
|
5
|
+
class Value < Struct
|
6
|
+
# Initializes a new value object that corresponds to a given value. The value can be a
|
7
|
+
# `Symbol`, a `String` or anything coercible into one (via `#to_s`).
|
8
|
+
#
|
9
|
+
# @param value [Symbol, String, #to_s] the value of the object.
|
10
|
+
#
|
11
|
+
# @return [Value] the value object corresponding to the given value.
|
12
|
+
def initialize(value)
|
13
|
+
super()
|
14
|
+
self._value = value.to_s
|
15
|
+
end
|
16
|
+
|
17
|
+
# Returns the string representation of the current object
|
18
|
+
#
|
19
|
+
# @return [String] the string representation of the current object
|
20
|
+
def to_s
|
21
|
+
_value.to_s
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns the symbol representation of the current object
|
25
|
+
#
|
26
|
+
# @return [Symbol] the symbol representation of the current object
|
27
|
+
def to_sym
|
28
|
+
to_s.parameterize.underscore.to_sym
|
29
|
+
end
|
30
|
+
|
31
|
+
# Returns whether the current value is equal to a given one. In addition to
|
32
|
+
# objects of the same type, the current object can be compared to a `Symbol`, a
|
33
|
+
# `String` or anything coercible into one (via `#to_s`)
|
34
|
+
#
|
35
|
+
# @param other [Value, Symbol, String, #to_s] the other value to compare with
|
36
|
+
#
|
37
|
+
# @return [Boolean] `true` if the values are equal, `false` otherwise
|
38
|
+
def ==(other)
|
39
|
+
case other
|
40
|
+
when self.class
|
41
|
+
_value == other._value
|
42
|
+
when Symbol
|
43
|
+
to_sym == other
|
44
|
+
else
|
45
|
+
to_s == other.to_s
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# @api private
|
50
|
+
def as_json(...)
|
51
|
+
_value.as_json(...)
|
52
|
+
end
|
53
|
+
|
54
|
+
protected
|
55
|
+
|
56
|
+
attr_accessor :_value
|
57
|
+
end
|
58
|
+
end
|
data/lib/gobl/version.rb
CHANGED
data/lib/gobl.rb
CHANGED
@@ -2,11 +2,13 @@
|
|
2
2
|
|
3
3
|
require 'json'
|
4
4
|
require 'zeitwerk'
|
5
|
-
require 'dry-struct'
|
6
5
|
require 'active_support/core_ext/string/inflections'
|
7
6
|
require 'forwardable'
|
8
7
|
require 'net/http'
|
9
8
|
require 'base64'
|
9
|
+
require 'hashme'
|
10
|
+
|
11
|
+
require_relative 'ext/hashme'
|
10
12
|
|
11
13
|
loader = Zeitwerk::Loader.for_gem
|
12
14
|
loader.inflector.inflect(
|
@@ -7,7 +7,7 @@ module GOBLExtensions
|
|
7
7
|
#
|
8
8
|
# @return [GOBL::ID] the Schema ID
|
9
9
|
def schema
|
10
|
-
@schema ||= GOBL::ID.new(
|
10
|
+
@schema ||= GOBL::ID.new(_map['$schema'])
|
11
11
|
end
|
12
12
|
|
13
13
|
# Extracts the GOBL struct embedded in the document. It determines the type of document
|
@@ -28,7 +28,7 @@ module GOBLExtensions
|
|
28
28
|
# Sanity check
|
29
29
|
raise "#{klass.name}::SCHEMA_ID expected to be '#{schema}'" unless schema == klass::SCHEMA_ID
|
30
30
|
|
31
|
-
klass.
|
31
|
+
klass.new _map.except('$schema')
|
32
32
|
end
|
33
33
|
|
34
34
|
module ClassMethods
|
@@ -36,7 +36,7 @@ module GOBLExtensions
|
|
36
36
|
#
|
37
37
|
# @return [Document] the document embedding the given struct
|
38
38
|
def embed(struct)
|
39
|
-
|
39
|
+
new struct.as_json.merge(
|
40
40
|
'$schema' => struct.class::SCHEMA_ID
|
41
41
|
)
|
42
42
|
end
|
@@ -3,6 +3,11 @@
|
|
3
3
|
module GOBLExtensions
|
4
4
|
# Additional methods for the generated {GOBL::Envelope} class
|
5
5
|
module EnvelopeHelper
|
6
|
+
def self.included(klass)
|
7
|
+
klass.alias_method :schema, '$schema'
|
8
|
+
klass.alias_method :schema=, '$schema='
|
9
|
+
end
|
10
|
+
|
6
11
|
# Extracts the GOBL struct embedded in the envelope's document
|
7
12
|
#
|
8
13
|
# @see GOBLExtensions::DocumentHelper#extract
|
@@ -11,8 +11,8 @@ module GOBLExtensions
|
|
11
11
|
# str.en #=> "Name"
|
12
12
|
# str.es #=> "Nombre"
|
13
13
|
def method_missing(method_name, *args, &block)
|
14
|
-
if
|
15
|
-
|
14
|
+
if _map.key?(method_name) || _map.key?(method_name.to_s)
|
15
|
+
_map[method_name] || _map[method_name.to_s]
|
16
16
|
else
|
17
17
|
super
|
18
18
|
end
|
@@ -20,7 +20,7 @@ module GOBLExtensions
|
|
20
20
|
|
21
21
|
# @api private
|
22
22
|
def respond_to_missing?(method_name, *)
|
23
|
-
|
23
|
+
_map.key?(method_name) || _map.key?(method_name.to_s) || super
|
24
24
|
end
|
25
25
|
end
|
26
26
|
end
|