gobl 0.6.1 → 0.6.2
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/lib/gobl/extensions/document_helper.rb +47 -0
- data/lib/gobl/extensions/envelope_helper.rb +22 -0
- data/lib/gobl/extensions/i18n/value_keys_helper.rb +29 -0
- data/lib/gobl/extensions/tax/regime_helper.rb +37 -0
- data/lib/gobl/version.rb +1 -1
- data/lib/gobl.rb +12 -3
- metadata +5 -6
- data/lib/extensions.rb +0 -12
- data/lib/gobl_extensions/document_helper.rb +0 -45
- data/lib/gobl_extensions/envelope_helper.rb +0 -20
- data/lib/gobl_extensions/i18n/value_keys_helper.rb +0 -27
- data/lib/gobl_extensions/tax/regime_helper.rb +0 -35
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 260a74838fdcc4fb1f061650fab0f2372d75cc641433e364ace0e6f0c7210341
|
4
|
+
data.tar.gz: 3295ae2a0a090e2a645656ad37e952884d32cec81885af60344e1a93f8d1cec0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 61d1572d9ab2d1c112b3c3a0138aa6e5e01c8c8e41b416499abc33ed5d400f8a23b8c3dd6b6b14a89df365d2063595c97873d8f15b2b1f05bc732d9c6138820e
|
7
|
+
data.tar.gz: c788ee19f631e61c18ad1acc2db7279bed92c37783f7b192f8b7863a26585d8c8818ab376f6e10a883e1612648ce74e7d51ef5aaa4c913255d79916013e2d16a
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GOBL
|
4
|
+
module Extensions
|
5
|
+
# Additional methods for the generated {GOBL::Document} class
|
6
|
+
module DocumentHelper
|
7
|
+
# Returns the Schema ID of the current document
|
8
|
+
#
|
9
|
+
# @return [GOBL::ID] the Schema ID
|
10
|
+
def schema
|
11
|
+
@schema ||= GOBL::ID.new(_map['$schema'])
|
12
|
+
end
|
13
|
+
|
14
|
+
# Extracts the GOBL struct embedded in the document. It determines the type of document
|
15
|
+
# currently embedded by reading the schema and attemps to instantiate the detected
|
16
|
+
# class.
|
17
|
+
#
|
18
|
+
# @return [GOBL::Struct] the GOBL struct embedded in the document
|
19
|
+
def extract
|
20
|
+
raise 'unknown schema' unless schema.gobl?
|
21
|
+
|
22
|
+
typs = ['GOBL']
|
23
|
+
schema.modules.each do |mod|
|
24
|
+
typs << mod.underscore.camelize
|
25
|
+
end
|
26
|
+
typs << schema.name.underscore.camelize
|
27
|
+
klass = typs.join('::').constantize
|
28
|
+
|
29
|
+
# Sanity check
|
30
|
+
raise "#{klass.name}::SCHEMA_ID expected to be '#{schema}'" unless schema == klass::SCHEMA_ID
|
31
|
+
|
32
|
+
klass.new _map.except('$schema')
|
33
|
+
end
|
34
|
+
|
35
|
+
module ClassMethods
|
36
|
+
# Embeds the given GOBL struct in a new document injecting the proper Schema ID.
|
37
|
+
#
|
38
|
+
# @return [Document] the document embedding the given struct
|
39
|
+
def embed(struct)
|
40
|
+
new struct.as_json.merge(
|
41
|
+
'$schema' => struct.class::SCHEMA_ID
|
42
|
+
)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GOBL
|
4
|
+
module Extensions
|
5
|
+
# Additional methods for the generated {GOBL::Envelope} class
|
6
|
+
module EnvelopeHelper
|
7
|
+
def self.included(klass)
|
8
|
+
klass.alias_method :schema, '$schema'
|
9
|
+
klass.alias_method :schema=, '$schema='
|
10
|
+
end
|
11
|
+
|
12
|
+
# Extracts the GOBL struct embedded in the envelope's document
|
13
|
+
#
|
14
|
+
# @see GOBL::Extensions::DocumentHelper#extract
|
15
|
+
#
|
16
|
+
# @return [GOBL::Struct] the GOBL struct embedded in the envelope's document
|
17
|
+
def extract
|
18
|
+
doc.extract
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GOBL
|
4
|
+
module Extensions
|
5
|
+
module I18n
|
6
|
+
# Additional methods for the generated {GOBL::I18n::String} class
|
7
|
+
module ValueKeysHelper
|
8
|
+
# Enables dynamic getter methods for the mapped keys.
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# str = GOBL::I18n::String.new(en: 'Name', es: 'Nombre')
|
12
|
+
# str.en #=> "Name"
|
13
|
+
# str.es #=> "Nombre"
|
14
|
+
def method_missing(method_name, *args, &block)
|
15
|
+
if _map.key?(method_name) || _map.key?(method_name.to_s)
|
16
|
+
_map[method_name] || _map[method_name.to_s]
|
17
|
+
else
|
18
|
+
super
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# @api private
|
23
|
+
def respond_to_missing?(method_name, *)
|
24
|
+
_map.key?(method_name) || _map.key?(method_name.to_s) || super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module GOBL
|
4
|
+
module Extensions
|
5
|
+
module Tax
|
6
|
+
# Additional methods for the generated {GOBL::Tax::Regime} class
|
7
|
+
module RegimeHelper
|
8
|
+
module ClassMethods
|
9
|
+
# Loads a {GOBL::Tax::Regime} object identified by the given code with all its data
|
10
|
+
#
|
11
|
+
# @param code [String] the code of the regime to load
|
12
|
+
#
|
13
|
+
# @return [Regime] the requested regime
|
14
|
+
def fetch(code)
|
15
|
+
regimes[code] ||= load_regime(code)
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
# map of regime ID to regime instance
|
21
|
+
def regimes
|
22
|
+
@regimes ||= {}
|
23
|
+
end
|
24
|
+
|
25
|
+
def load_regime(code)
|
26
|
+
path = File.join("#{__dir__}/../../../../data/regimes", "#{code.to_s.downcase}.json")
|
27
|
+
msg = "regime definition for #{code} does not exist at #{path}"
|
28
|
+
raise StandardError, msg unless File.exist?(path)
|
29
|
+
|
30
|
+
data = File.read(path)
|
31
|
+
from_json!(data)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/gobl/version.rb
CHANGED
data/lib/gobl.rb
CHANGED
@@ -10,10 +10,9 @@ require 'hashme'
|
|
10
10
|
|
11
11
|
require_relative 'ext/hashme'
|
12
12
|
|
13
|
-
loader = Zeitwerk::Loader.for_gem
|
13
|
+
loader = Zeitwerk::Loader.for_gem
|
14
14
|
loader.inflector.inflect(
|
15
15
|
'gobl' => 'GOBL',
|
16
|
-
'gobl_extensions' => 'GOBLExtensions',
|
17
16
|
'uuid' => 'UUID',
|
18
17
|
'url' => 'URL',
|
19
18
|
'item_id' => 'ItemID',
|
@@ -22,6 +21,7 @@ loader.inflector.inflect(
|
|
22
21
|
'id' => 'ID',
|
23
22
|
'cbc' => 'CBC'
|
24
23
|
)
|
24
|
+
loader.ignore("#{__dir__}/ext")
|
25
25
|
loader.setup
|
26
26
|
|
27
27
|
# Main GOBL namespace. It provides direct access to the library's configuration (see
|
@@ -41,4 +41,13 @@ module GOBL
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
|
44
|
+
# Extensions are all defined here so they are auto-loaded by Zeitwerk.
|
45
|
+
#
|
46
|
+
# Both the instance and class method modules need to be included/extended explicitly here
|
47
|
+
# to get YARD parsing them properly.
|
48
|
+
|
49
|
+
GOBL::I18n::String.include GOBL::Extensions::I18n::ValueKeysHelper
|
50
|
+
GOBL::Document.include GOBL::Extensions::DocumentHelper
|
51
|
+
GOBL::Document.extend GOBL::Extensions::DocumentHelper::ClassMethods
|
52
|
+
GOBL::Envelope.include GOBL::Extensions::EnvelopeHelper
|
53
|
+
GOBL::Tax::Regime.extend GOBL::Extensions::Tax::RegimeHelper::ClassMethods
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gobl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.6.
|
4
|
+
version: 0.6.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Lilue
|
@@ -66,7 +66,6 @@ files:
|
|
66
66
|
- data/regimes/gb.json
|
67
67
|
- data/regimes/nl.json
|
68
68
|
- lib/ext/hashme.rb
|
69
|
-
- lib/extensions.rb
|
70
69
|
- lib/gobl.rb
|
71
70
|
- lib/gobl/bill/advances.rb
|
72
71
|
- lib/gobl/bill/charge.rb
|
@@ -101,6 +100,10 @@ files:
|
|
101
100
|
- lib/gobl/dsig/signature.rb
|
102
101
|
- lib/gobl/enum.rb
|
103
102
|
- lib/gobl/envelope.rb
|
103
|
+
- lib/gobl/extensions/document_helper.rb
|
104
|
+
- lib/gobl/extensions/envelope_helper.rb
|
105
|
+
- lib/gobl/extensions/i18n/value_keys_helper.rb
|
106
|
+
- lib/gobl/extensions/tax/regime_helper.rb
|
104
107
|
- lib/gobl/header.rb
|
105
108
|
- lib/gobl/i18n/string.rb
|
106
109
|
- lib/gobl/id.rb
|
@@ -155,10 +158,6 @@ files:
|
|
155
158
|
- lib/gobl/uuid/uuid.rb
|
156
159
|
- lib/gobl/value.rb
|
157
160
|
- lib/gobl/version.rb
|
158
|
-
- lib/gobl_extensions/document_helper.rb
|
159
|
-
- lib/gobl_extensions/envelope_helper.rb
|
160
|
-
- lib/gobl_extensions/i18n/value_keys_helper.rb
|
161
|
-
- lib/gobl_extensions/tax/regime_helper.rb
|
162
161
|
homepage: https://github.com/invopop/gobl.ruby
|
163
162
|
licenses:
|
164
163
|
- Apache-2.0
|
data/lib/extensions.rb
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# Extensions are all defined here so they are auto-loaded by Zeitwerk.
|
4
|
-
#
|
5
|
-
# Both the instance and class method modules need to be included/extended explicitly here
|
6
|
-
# to get YARD parsing them properly.
|
7
|
-
|
8
|
-
GOBL::I18n::String.include GOBLExtensions::I18n::ValueKeysHelper
|
9
|
-
GOBL::Document.include GOBLExtensions::DocumentHelper
|
10
|
-
GOBL::Document.extend GOBLExtensions::DocumentHelper::ClassMethods
|
11
|
-
GOBL::Envelope.include GOBLExtensions::EnvelopeHelper
|
12
|
-
GOBL::Tax::Regime.extend GOBLExtensions::Tax::RegimeHelper::ClassMethods
|
@@ -1,45 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module GOBLExtensions
|
4
|
-
# Additional methods for the generated {GOBL::Document} class
|
5
|
-
module DocumentHelper
|
6
|
-
# Returns the Schema ID of the current document
|
7
|
-
#
|
8
|
-
# @return [GOBL::ID] the Schema ID
|
9
|
-
def schema
|
10
|
-
@schema ||= GOBL::ID.new(_map['$schema'])
|
11
|
-
end
|
12
|
-
|
13
|
-
# Extracts the GOBL struct embedded in the document. It determines the type of document
|
14
|
-
# currently embedded by reading the schema and attemps to instantiate the detected
|
15
|
-
# class.
|
16
|
-
#
|
17
|
-
# @return [GOBL::Struct] the GOBL struct embedded in the document
|
18
|
-
def extract
|
19
|
-
raise 'unknown schema' unless schema.gobl?
|
20
|
-
|
21
|
-
typs = ['GOBL']
|
22
|
-
schema.modules.each do |mod|
|
23
|
-
typs << mod.underscore.camelize
|
24
|
-
end
|
25
|
-
typs << schema.name.underscore.camelize
|
26
|
-
klass = typs.join('::').constantize
|
27
|
-
|
28
|
-
# Sanity check
|
29
|
-
raise "#{klass.name}::SCHEMA_ID expected to be '#{schema}'" unless schema == klass::SCHEMA_ID
|
30
|
-
|
31
|
-
klass.new _map.except('$schema')
|
32
|
-
end
|
33
|
-
|
34
|
-
module ClassMethods
|
35
|
-
# Embeds the given GOBL struct in a new document injecting the proper Schema ID.
|
36
|
-
#
|
37
|
-
# @return [Document] the document embedding the given struct
|
38
|
-
def embed(struct)
|
39
|
-
new struct.as_json.merge(
|
40
|
-
'$schema' => struct.class::SCHEMA_ID
|
41
|
-
)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module GOBLExtensions
|
4
|
-
# Additional methods for the generated {GOBL::Envelope} class
|
5
|
-
module EnvelopeHelper
|
6
|
-
def self.included(klass)
|
7
|
-
klass.alias_method :schema, '$schema'
|
8
|
-
klass.alias_method :schema=, '$schema='
|
9
|
-
end
|
10
|
-
|
11
|
-
# Extracts the GOBL struct embedded in the envelope's document
|
12
|
-
#
|
13
|
-
# @see GOBLExtensions::DocumentHelper#extract
|
14
|
-
#
|
15
|
-
# @return [GOBL::Struct] the GOBL struct embedded in the envelope's document
|
16
|
-
def extract
|
17
|
-
doc.extract
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module GOBLExtensions
|
4
|
-
module I18n
|
5
|
-
# Additional methods for the generated {GOBL::I18n::String} class
|
6
|
-
module ValueKeysHelper
|
7
|
-
# Enables dynamic getter methods for the mapped keys.
|
8
|
-
#
|
9
|
-
# @example
|
10
|
-
# str = GOBL::I18n::String.new(en: 'Name', es: 'Nombre')
|
11
|
-
# str.en #=> "Name"
|
12
|
-
# str.es #=> "Nombre"
|
13
|
-
def method_missing(method_name, *args, &block)
|
14
|
-
if _map.key?(method_name) || _map.key?(method_name.to_s)
|
15
|
-
_map[method_name] || _map[method_name.to_s]
|
16
|
-
else
|
17
|
-
super
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
# @api private
|
22
|
-
def respond_to_missing?(method_name, *)
|
23
|
-
_map.key?(method_name) || _map.key?(method_name.to_s) || super
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module GOBLExtensions
|
4
|
-
module Tax
|
5
|
-
# Additional methods for the generated {GOBL::Tax::Regime} class
|
6
|
-
module RegimeHelper
|
7
|
-
module ClassMethods
|
8
|
-
# Loads a {GOBL::Tax::Regime} object identified by the given code with all its data
|
9
|
-
#
|
10
|
-
# @param code [String] the code of the regime to load
|
11
|
-
#
|
12
|
-
# @return [Regime] the requested regime
|
13
|
-
def fetch(code)
|
14
|
-
regimes[code] ||= load_regime(code)
|
15
|
-
end
|
16
|
-
|
17
|
-
protected
|
18
|
-
|
19
|
-
# map of regime ID to regime instance
|
20
|
-
def regimes
|
21
|
-
@regimes ||= {}
|
22
|
-
end
|
23
|
-
|
24
|
-
def load_regime(code)
|
25
|
-
path = File.join("#{__dir__}/../../../data/regimes", "#{code.to_s.downcase}.json")
|
26
|
-
msg = "regime definition for #{code} does not exist at #{path}"
|
27
|
-
raise StandardError, msg unless File.exist?(path)
|
28
|
-
|
29
|
-
data = File.read(path)
|
30
|
-
from_json!(data)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|