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
@@ -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::Struct
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
- attribute :name, GOBL::I18n::String
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
- attribute :country, GOBL::L10n::CountryCode
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
- attribute? :locality, GOBL::L10n::Code.optional
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
- attribute? :localities, Localities.optional
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
- attribute :currency, GOBL::Currency::Code
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
- attribute? :schemes, Schemes.optional
47
+ property :schemes, Schemes
45
48
 
46
49
  # @!attribute [r] categories
47
50
  # List of tax categories.
48
51
  # @return [Array<Category>]
49
- attribute :categories, GOBL::Types::Array.of(Category)
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
@@ -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::Struct
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
- attribute :key, GOBL::Org::Key
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
- attribute :name, GOBL::I18n::String
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
- attribute? :description, GOBL::I18n::String.optional
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
- attribute? :categories, GOBL::Types::Array.of(GOBL::Org::Code).optional
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
- attribute? :note, GOBL::Org::Note.optional
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
@@ -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
- class Schemes < GOBL::Struct
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
- class Set < GOBL::Struct
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
@@ -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::Struct
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
- attribute? :categories, GOBL::Types::Array.of(CategoryTotal).optional
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
- attribute :sum, 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 [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
@@ -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::Struct
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module GOBL
4
- VERSION = '0.1.2'
4
+ VERSION = '0.2.0'
5
5
  end
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(_value['$schema'])
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.from_gobl!(_value.except('$schema'))
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
- from_gobl! struct.to_gobl.merge(
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 _value.key?(method_name) || _value.key?(method_name.to_s)
15
- _value[method_name] || _value[method_name.to_s]
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
- _value.key?(method_name) || _value.key?(method_name.to_s) || super
23
+ _map.key?(method_name) || _map.key?(method_name.to_s) || super
24
24
  end
25
25
  end
26
26
  end