gobl 0.1.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (94) hide show
  1. checksums.yaml +4 -4
  2. data/data/tax/CO.json +420 -0
  3. data/data/tax/ES.json +61 -0
  4. data/lib/ext/hashme.rb +28 -0
  5. data/lib/gobl/bill/advances.rb +1 -77
  6. data/lib/gobl/bill/charge.rb +11 -65
  7. data/lib/gobl/bill/delivery.rb +5 -47
  8. data/lib/gobl/bill/discount.rb +11 -65
  9. data/lib/gobl/bill/exchange_rates.rb +1 -77
  10. data/lib/gobl/bill/invoice.rb +28 -104
  11. data/lib/gobl/bill/invoice_type.rb +3 -134
  12. data/lib/gobl/bill/line.rb +13 -65
  13. data/lib/gobl/bill/line_charge.rb +5 -47
  14. data/lib/gobl/bill/line_discount.rb +5 -47
  15. data/lib/gobl/bill/ordering.rb +2 -38
  16. data/lib/gobl/bill/outlay.rb +11 -59
  17. data/lib/gobl/bill/payment.rb +5 -47
  18. data/lib/gobl/bill/preceding.rb +12 -62
  19. data/lib/gobl/bill/scheme_keys.rb +1 -77
  20. data/lib/gobl/bill/tax.rb +4 -44
  21. data/lib/gobl/bill/totals.rb +17 -71
  22. data/lib/gobl/cal/date.rb +1 -90
  23. data/lib/gobl/cal/period.rb +5 -41
  24. data/lib/gobl/currency/code.rb +3 -134
  25. data/lib/gobl/currency/exchange_rate.rb +5 -41
  26. data/lib/gobl/document.rb +1 -58
  27. data/lib/gobl/dsig/digest.rb +5 -41
  28. data/lib/gobl/dsig/signature.rb +1 -90
  29. data/lib/gobl/enum.rb +81 -0
  30. data/lib/gobl/envelope.rb +9 -48
  31. data/lib/gobl/header.rb +10 -56
  32. data/lib/gobl/i18n/string.rb +1 -58
  33. data/lib/gobl/l10n/code.rb +1 -90
  34. data/lib/gobl/l10n/country_code.rb +3 -134
  35. data/lib/gobl/map.rb +62 -0
  36. data/lib/gobl/note/message.rb +5 -44
  37. data/lib/gobl/num/amount.rb +6 -35
  38. data/lib/gobl/object.rb +21 -0
  39. data/lib/gobl/operations.rb +3 -3
  40. data/lib/gobl/org/address.rb +18 -80
  41. data/lib/gobl/org/code.rb +1 -90
  42. data/lib/gobl/org/coordinates.rb +5 -47
  43. data/lib/gobl/org/email.rb +6 -47
  44. data/lib/gobl/org/inbox.rb +8 -50
  45. data/lib/gobl/org/item.rb +13 -65
  46. data/lib/gobl/org/item_code.rb +4 -41
  47. data/lib/gobl/org/key.rb +1 -90
  48. data/lib/gobl/org/meta.rb +1 -58
  49. data/lib/gobl/org/name.rb +12 -62
  50. data/lib/gobl/org/note.rb +6 -47
  51. data/lib/gobl/org/note_key.rb +3 -134
  52. data/lib/gobl/org/party.rb +14 -71
  53. data/lib/gobl/org/person.rb +9 -56
  54. data/lib/gobl/org/registration.rb +9 -59
  55. data/lib/gobl/org/source_key.rb +3 -134
  56. data/lib/gobl/org/tax_identity.rb +8 -53
  57. data/lib/gobl/org/telephone.rb +5 -44
  58. data/lib/gobl/org/unit.rb +1 -136
  59. data/lib/gobl/pay/advance.rb +11 -59
  60. data/lib/gobl/pay/card.rb +5 -41
  61. data/lib/gobl/pay/credit_transfer.rb +6 -50
  62. data/lib/gobl/pay/direct_debit.rb +4 -44
  63. data/lib/gobl/pay/due_date.rb +8 -50
  64. data/lib/gobl/pay/instructions.rb +11 -62
  65. data/lib/gobl/pay/method_key.rb +3 -134
  66. data/lib/gobl/pay/online.rb +4 -41
  67. data/lib/gobl/pay/term_key.rb +3 -134
  68. data/lib/gobl/pay/terms.rb +6 -47
  69. data/lib/gobl/stamp.rb +5 -41
  70. data/lib/gobl/struct.rb +15 -15
  71. data/lib/gobl/tax/category.rb +9 -50
  72. data/lib/gobl/tax/category_total.rb +11 -53
  73. data/lib/gobl/tax/combo.rb +6 -47
  74. data/lib/gobl/tax/localities.rb +1 -77
  75. data/lib/gobl/tax/locality.rb +6 -44
  76. data/lib/gobl/tax/rate.rb +8 -47
  77. data/lib/gobl/tax/rate_total.rb +9 -50
  78. data/lib/gobl/tax/rate_total_surcharge.rb +5 -41
  79. data/lib/gobl/tax/rate_value.rb +6 -47
  80. data/lib/gobl/tax/region.rb +12 -56
  81. data/lib/gobl/tax/scheme.rb +8 -50
  82. data/lib/gobl/tax/schemes.rb +1 -77
  83. data/lib/gobl/tax/set.rb +1 -77
  84. data/lib/gobl/tax/total.rb +4 -41
  85. data/lib/gobl/uuid/uuid.rb +1 -90
  86. data/lib/gobl/value.rb +58 -0
  87. data/lib/gobl/version.rb +1 -1
  88. data/lib/gobl.rb +3 -1
  89. data/lib/gobl_extensions/document_helper.rb +3 -3
  90. data/lib/gobl_extensions/envelope_helper.rb +5 -0
  91. data/lib/gobl_extensions/i18n/value_keys_helper.rb +3 -3
  92. data/lib/gobl_extensions/tax/region_helper.rb +3 -9
  93. metadata +12 -35
  94. data/lib/gobl/types.rb +0 -17
@@ -9,106 +9,56 @@
9
9
  module GOBL
10
10
  module Bill
11
11
  # Preceding allows for information to be provided about a previous invoice that this one will replace or subtract from.
12
- class Preceding < GOBL::Struct
12
+ class Preceding < GOBL::Object
13
13
  # The Schema ID of the GOBL Preceding structure
14
14
  SCHEMA_ID = 'https://gobl.org/draft-0/bill/invoice#/$defs/Preceding'
15
15
 
16
16
  # @!attribute [r] uuid
17
17
  # Preceding document's UUID if available can be useful for tracing.
18
18
  # @return [GOBL::UUID::UUID]
19
- attribute? :uuid, GOBL::UUID::UUID.optional
19
+ property :uuid, GOBL::UUID::UUID
20
20
 
21
21
  # @!attribute [r] code
22
22
  # Identity code of the previous invoice.
23
23
  # @return [String]
24
- attribute :code, GOBL::Types::String
24
+ property :code, String
25
+ validates :code, presence: true
25
26
 
26
27
  # @!attribute [r] series
27
28
  # Additional identification details
28
29
  # @return [String]
29
- attribute? :series, GOBL::Types::String.optional
30
+ property :series, String
30
31
 
31
32
  # @!attribute [r] issue_date
32
33
  # When the preceding invoice was issued.
33
34
  # @return [GOBL::Cal::Date]
34
- attribute :issue_date, GOBL::Cal::Date
35
+ property :issue_date, GOBL::Cal::Date
36
+ validates :issue_date, presence: true
35
37
 
36
38
  # @!attribute [r] period
37
39
  # Tax period in which the previous invoice has an effect.
38
40
  # @return [GOBL::Cal::Period]
39
- attribute? :period, GOBL::Cal::Period.optional
41
+ property :period, GOBL::Cal::Period
40
42
 
41
43
  # @!attribute [r] corrections
42
44
  # Specific codes for the corrections made.
43
45
  # @return [Array<String>]
44
- attribute? :corrections, GOBL::Types::Array.of(GOBL::Types::String).optional
46
+ property :corrections, [String]
45
47
 
46
48
  # @!attribute [r] correction_method
47
49
  # How has the previous invoice been corrected?
48
50
  # @return [String]
49
- attribute? :correction_method, GOBL::Types::String.optional
51
+ property :correction_method, String
50
52
 
51
53
  # @!attribute [r] notes
52
54
  # Additional details regarding preceding invoice
53
55
  # @return [String]
54
- attribute? :notes, GOBL::Types::String.optional
56
+ property :notes, String
55
57
 
56
58
  # @!attribute [r] meta
57
59
  # Additional semi-structured data that may be useful in specific regions
58
60
  # @return [GOBL::Org::Meta]
59
- attribute? :meta, GOBL::Org::Meta.optional
60
-
61
- # Creates a new object from a hash of GOBL data
62
- #
63
- # @param data [Hash] a hash of GOBL data
64
- #
65
- # @return [Preceding] the object created from the given data
66
- def self.from_gobl!(data)
67
- data = GOBL::Types::Hash[data]
68
-
69
- new(
70
- uuid: data['uuid'] ? GOBL::UUID::UUID.from_gobl!(data['uuid']) : nil,
71
- code: data['code'],
72
- series: data['series'],
73
- issue_date: GOBL::Cal::Date.from_gobl!(data['issue_date']),
74
- period: data['period'] ? GOBL::Cal::Period.from_gobl!(data['period']) : nil,
75
- corrections: data['corrections']&.map { |item| item },
76
- correction_method: data['correction_method'],
77
- notes: data['notes'],
78
- meta: data['meta'] ? GOBL::Org::Meta.from_gobl!(data['meta']) : nil
79
- )
80
- end
81
-
82
- # Returns a hash of GOBL data representing the current object
83
- #
84
- # @return [Hash] the array of GOBL data that represents the current object
85
- def to_gobl
86
- {
87
- 'uuid' => attributes[:uuid]&.to_gobl,
88
- 'code' => attributes[:code],
89
- 'series' => attributes[:series],
90
- 'issue_date' => attributes[:issue_date]&.to_gobl,
91
- 'period' => attributes[:period]&.to_gobl,
92
- 'corrections' => attributes[:corrections]&.map { |item| item },
93
- 'correction_method' => attributes[:correction_method],
94
- 'notes' => attributes[:notes],
95
- 'meta' => attributes[:meta]&.to_gobl
96
- }.compact
97
- end
98
-
99
- # @!method self.new(attrs)
100
- #
101
- # Returns a {Preceding} object from a given hash of attributes. Nested
102
- # attributes are supported: the constructor will instantiate the proper GOBL
103
- # objects when nested hashes or arrays are given as part of the `attrs`
104
- # parameter.
105
- #
106
- # The `new` method will only allow to create a new object if all attributes
107
- # marked as mandatory and not calculated in the JSON schema are provided.
108
- #
109
- # @param attrs [Hash] the hash of attributes
110
- #
111
- # @return [Preceding] the object corresponding to the given input
61
+ property :meta, GOBL::Org::Meta
112
62
  end
113
63
  end
114
64
  end
@@ -9,82 +9,6 @@
9
9
  module GOBL
10
10
  module Bill
11
11
  # SchemeKeys stores a list of keys that makes it easier to perform matches.
12
- class SchemeKeys < GOBL::Struct
13
- extend Forwardable
14
- include Enumerable
15
-
16
- # The Schema ID of the GOBL SchemeKeys structure
17
- SCHEMA_ID = 'https://gobl.org/draft-0/bill/invoice#/$defs/SchemeKeys'
18
-
19
- attribute :_ary, GOBL::Types::Array.of(GOBL::Org::Key)
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 [GOBL::Org::Key] 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<GOBL::Org::Key>] 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<GOBL::Org::Key>] 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 [SchemeKeys] the object created from the given data
61
- def self.from_gobl!(data)
62
- new(_ary: data&.map { |item| GOBL::Org::Key.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 {SchemeKeys} object from a given array of structs. The array may
73
- # contain {GOBL::Org::Key} objects or hashes. If hashes are provided, the constructor
74
- # will call `GOBL::Org::Key#new` to generate the objects that will be part of the
75
- # returned {SchemeKeys} object
76
- #
77
- # @param object [Array<GOBL::Org::Key, Hash>] the array containing the structs
78
- #
79
- # @return [SchemeKeys] 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
+ SchemeKeys = [GOBL::Org::Key] # rubocop:disable Naming/ConstantName, Style/MutableConstant
89
13
  end
90
14
  end
data/lib/gobl/bill/tax.rb CHANGED
@@ -9,64 +9,24 @@
9
9
  module GOBL
10
10
  module Bill
11
11
  # Tax defines a summary of the taxes which may be applied to an invoice.
12
- class Tax < GOBL::Struct
12
+ class Tax < GOBL::Object
13
13
  # The Schema ID of the GOBL Tax structure
14
14
  SCHEMA_ID = 'https://gobl.org/draft-0/bill/invoice#/$defs/Tax'
15
15
 
16
16
  # @!attribute [r] prices_include
17
17
  # Category of the tax already included in the line item prices, especially useful for B2C retailers with customers who prefer final prices inclusive of tax.
18
18
  # @return [GOBL::Org::Code]
19
- attribute? :prices_include, GOBL::Org::Code.optional
19
+ property :prices_include, GOBL::Org::Code
20
20
 
21
21
  # @!attribute [r] schemes
22
22
  # Special tax schemes that apply to this invoice according to local requirements.
23
23
  # @return [SchemeKeys]
24
- attribute? :schemes, SchemeKeys.optional
24
+ property :schemes, SchemeKeys
25
25
 
26
26
  # @!attribute [r] meta
27
27
  # Any additional data that may be required for processing, but should never be relied upon by recipients.
28
28
  # @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 [Tax] the object created from the given data
36
- def self.from_gobl!(data)
37
- data = GOBL::Types::Hash[data]
38
-
39
- new(
40
- prices_include: data['prices_include'] ? GOBL::Org::Code.from_gobl!(data['prices_include']) : nil,
41
- schemes: data['schemes'] ? SchemeKeys.from_gobl!(data['schemes']) : nil,
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
- 'prices_include' => attributes[:prices_include]&.to_gobl,
52
- 'schemes' => attributes[:schemes]&.to_gobl,
53
- 'meta' => attributes[:meta]&.to_gobl
54
- }.compact
55
- end
56
-
57
- # @!method self.new(attrs)
58
- #
59
- # Returns a {Tax} 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 [Tax] the object corresponding to the given input
29
+ property :meta, GOBL::Org::Meta
70
30
  end
71
31
  end
72
32
  end
@@ -9,127 +9,73 @@
9
9
  module GOBL
10
10
  module Bill
11
11
  # Totals contains the summaries of all calculations for the invoice.
12
- class Totals < GOBL::Struct
12
+ class Totals < GOBL::Object
13
13
  # The Schema ID of the GOBL Totals structure
14
14
  SCHEMA_ID = 'https://gobl.org/draft-0/bill/invoice#/$defs/Totals'
15
15
 
16
16
  # @!attribute [r] sum
17
17
  # Sum of all line item sums
18
18
  # @return [GOBL::Num::Amount]
19
- attribute :sum, GOBL::Types.Constructor(GOBL::Num::Amount)
19
+ property :sum, GOBL::Num::Amount
20
+ validates :sum, presence: true
20
21
 
21
22
  # @!attribute [r] discount
22
23
  # Sum of all document level discounts
23
24
  # @return [GOBL::Num::Amount]
24
- attribute? :discount, GOBL::Types.Constructor(GOBL::Num::Amount).optional
25
+ property :discount, GOBL::Num::Amount
25
26
 
26
27
  # @!attribute [r] charge
27
28
  # Sum of all document level charges
28
29
  # @return [GOBL::Num::Amount]
29
- attribute? :charge, GOBL::Types.Constructor(GOBL::Num::Amount).optional
30
+ property :charge, GOBL::Num::Amount
30
31
 
31
32
  # @!attribute [r] tax_included
32
33
  # If prices include tax, this is the total tax included in the price.
33
34
  # @return [GOBL::Num::Amount]
34
- attribute? :tax_included, GOBL::Types.Constructor(GOBL::Num::Amount).optional
35
+ property :tax_included, GOBL::Num::Amount
35
36
 
36
37
  # @!attribute [r] total
37
38
  # Sum of all line sums minus the discounts, plus the charges, without tax.
38
39
  # @return [GOBL::Num::Amount]
39
- attribute :total, GOBL::Types.Constructor(GOBL::Num::Amount)
40
+ property :total, GOBL::Num::Amount
41
+ validates :total, presence: true
40
42
 
41
43
  # @!attribute [r] taxes
42
44
  # Summary of all the taxes included in the invoice.
43
45
  # @return [GOBL::Tax::Total]
44
- attribute? :taxes, GOBL::Tax::Total.optional
46
+ property :taxes, GOBL::Tax::Total
45
47
 
46
48
  # @!attribute [r] tax
47
49
  # Total amount of tax to apply to the invoice.
48
50
  # @return [GOBL::Num::Amount]
49
- attribute? :tax, GOBL::Types.Constructor(GOBL::Num::Amount).optional
51
+ property :tax, GOBL::Num::Amount
50
52
 
51
53
  # @!attribute [r] total_with_tax
52
54
  # Grand total after all taxes have been applied.
53
55
  # @return [GOBL::Num::Amount]
54
- attribute :total_with_tax, GOBL::Types.Constructor(GOBL::Num::Amount)
56
+ property :total_with_tax, GOBL::Num::Amount
57
+ validates :total_with_tax, presence: true
55
58
 
56
59
  # @!attribute [r] outlays
57
60
  # Total paid in outlays that need to be reimbursed
58
61
  # @return [GOBL::Num::Amount]
59
- attribute? :outlays, GOBL::Types.Constructor(GOBL::Num::Amount).optional
62
+ property :outlays, GOBL::Num::Amount
60
63
 
61
64
  # @!attribute [r] payable
62
65
  # Total amount to be paid after applying taxes and outlays.
63
66
  # @return [GOBL::Num::Amount]
64
- attribute :payable, GOBL::Types.Constructor(GOBL::Num::Amount)
67
+ property :payable, GOBL::Num::Amount
68
+ validates :payable, presence: true
65
69
 
66
70
  # @!attribute [r] advance
67
71
  # Total amount already paid in advance.
68
72
  # @return [GOBL::Num::Amount]
69
- attribute? :advance, GOBL::Types.Constructor(GOBL::Num::Amount).optional
73
+ property :advance, GOBL::Num::Amount
70
74
 
71
75
  # @!attribute [r] due
72
76
  # How much actually needs to be paid now.
73
77
  # @return [GOBL::Num::Amount]
74
- attribute? :due, GOBL::Types.Constructor(GOBL::Num::Amount).optional
75
-
76
- # Creates a new object from a hash of GOBL data
77
- #
78
- # @param data [Hash] a hash of GOBL data
79
- #
80
- # @return [Totals] the object created from the given data
81
- def self.from_gobl!(data)
82
- data = GOBL::Types::Hash[data]
83
-
84
- new(
85
- sum: data['sum'],
86
- discount: data['discount'] || nil,
87
- charge: data['charge'] || nil,
88
- tax_included: data['tax_included'] || nil,
89
- total: data['total'],
90
- taxes: data['taxes'] ? GOBL::Tax::Total.from_gobl!(data['taxes']) : nil,
91
- tax: data['tax'] || nil,
92
- total_with_tax: data['total_with_tax'],
93
- outlays: data['outlays'] || nil,
94
- payable: data['payable'],
95
- advance: data['advance'] || nil,
96
- due: data['due'] || nil
97
- )
98
- end
99
-
100
- # Returns a hash of GOBL data representing the current object
101
- #
102
- # @return [Hash] the array of GOBL data that represents the current object
103
- def to_gobl
104
- {
105
- 'sum' => attributes[:sum]&.to_gobl,
106
- 'discount' => attributes[:discount]&.to_gobl,
107
- 'charge' => attributes[:charge]&.to_gobl,
108
- 'tax_included' => attributes[:tax_included]&.to_gobl,
109
- 'total' => attributes[:total]&.to_gobl,
110
- 'taxes' => attributes[:taxes]&.to_gobl,
111
- 'tax' => attributes[:tax]&.to_gobl,
112
- 'total_with_tax' => attributes[:total_with_tax]&.to_gobl,
113
- 'outlays' => attributes[:outlays]&.to_gobl,
114
- 'payable' => attributes[:payable]&.to_gobl,
115
- 'advance' => attributes[:advance]&.to_gobl,
116
- 'due' => attributes[:due]&.to_gobl
117
- }.compact
118
- end
119
-
120
- # @!method self.new(attrs)
121
- #
122
- # Returns a {Totals} object from a given hash of attributes. Nested
123
- # attributes are supported: the constructor will instantiate the proper GOBL
124
- # objects when nested hashes or arrays are given as part of the `attrs`
125
- # parameter.
126
- #
127
- # The `new` method will only allow to create a new object if all attributes
128
- # marked as mandatory and not calculated in the JSON schema are provided.
129
- #
130
- # @param attrs [Hash] the hash of attributes
131
- #
132
- # @return [Totals] the object corresponding to the given input
78
+ property :due, GOBL::Num::Amount
133
79
  end
134
80
  end
135
81
  end
data/lib/gobl/cal/date.rb CHANGED
@@ -9,98 +9,9 @@
9
9
  module GOBL
10
10
  module Cal
11
11
  # Civil date in simplified ISO format, like 2021-05-26
12
- class Date < GOBL::Struct
12
+ class Date < GOBL::Value
13
13
  # The Schema ID of the GOBL Date structure
14
14
  SCHEMA_ID = 'https://gobl.org/draft-0/cal/date'
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 [Date] 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 {Date} 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 [Date] the object corresponding to the given value.
41
- #
42
- # @example Instantiating from a symbol
43
- # date = GOBL::Cal::Date.new(:value)
44
- #
45
- # @example Instantiating from a string
46
- # date = GOBL::Cal::Date.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 {Date} 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 [Date, 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 {Date} object
81
- # date = GOBL::Cal::Date.new('value')
82
- # date == GOBL::Cal::Date.new('value') #=> true
83
- # date == GOBL::Cal::Date.new('other') #=> false
84
- #
85
- # @example Comparing to a string
86
- # date = GOBL::Cal::Date.new('value')
87
- # date == 'value' #=> true
88
- # date == 'other' #=> false
89
- #
90
- # @example Comparing to a symbol
91
- # date = GOBL::Cal::Date.new('value')
92
- # date == :value #=> true
93
- # date == :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,55 +9,19 @@
9
9
  module GOBL
10
10
  module Cal
11
11
  # Period represents two dates with a start and finish.
12
- class Period < GOBL::Struct
12
+ class Period < GOBL::Object
13
13
  # The Schema ID of the GOBL Period structure
14
14
  SCHEMA_ID = 'https://gobl.org/draft-0/cal/period'
15
15
 
16
16
  # @!attribute [r] start
17
17
  # @return [GOBL::Cal::Date]
18
- attribute :start, GOBL::Cal::Date
18
+ property :start, GOBL::Cal::Date
19
+ validates :start, presence: true
19
20
 
20
21
  # @!attribute [r] end
21
22
  # @return [GOBL::Cal::Date]
22
- attribute :end, GOBL::Cal::Date
23
-
24
- # Creates a new object from a hash of GOBL data
25
- #
26
- # @param data [Hash] a hash of GOBL data
27
- #
28
- # @return [Period] the object created from the given data
29
- def self.from_gobl!(data)
30
- data = GOBL::Types::Hash[data]
31
-
32
- new(
33
- start: GOBL::Cal::Date.from_gobl!(data['start']),
34
- end: GOBL::Cal::Date.from_gobl!(data['end'])
35
- )
36
- end
37
-
38
- # Returns a hash of GOBL data representing the current object
39
- #
40
- # @return [Hash] the array of GOBL data that represents the current object
41
- def to_gobl
42
- {
43
- 'start' => attributes[:start]&.to_gobl,
44
- 'end' => attributes[:end]&.to_gobl
45
- }.compact
46
- end
47
-
48
- # @!method self.new(attrs)
49
- #
50
- # Returns a {Period} object from a given hash of attributes. Nested
51
- # attributes are supported: the constructor will instantiate the proper GOBL
52
- # objects when nested hashes or arrays are given as part of the `attrs`
53
- # parameter.
54
- #
55
- # The `new` method will only allow to create a new object if all attributes
56
- # marked as mandatory and not calculated in the JSON schema are provided.
57
- #
58
- # @param attrs [Hash] the hash of attributes
59
- #
60
- # @return [Period] the object corresponding to the given input
23
+ property :end, GOBL::Cal::Date
24
+ validates :end, presence: true
61
25
  end
62
26
  end
63
27
  end