edible 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ce126c05c0061de2a26cf9ce7c1634529a30e5d76a1e5ef1b19e0c17a2a2f45f
4
+ data.tar.gz: 3e3a92ec35dedb7a371d98a7db6a26f7e3d3c2efd0393fab193e000148604cba
5
+ SHA512:
6
+ metadata.gz: 7a59a656ddc1680b46e0f45b2c67651867ca3d589898d43cab15cdf2058aa7737db6c118a4100b49e867f129b65eadcfe79ee95478cc9a4c58a84ef82ebc009c
7
+ data.tar.gz: c2886f4a157f3766248ae3113585c51271f320bce67a575912f1f98becba0aef354064449efbafcd110ac0b6b87711775d5659bc37abbd46678d79b74b58b77e
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) Manfred Stienstra
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # Edible
2
+
3
+ Edible is a basic builder for EDIFACT data. Support for a parser is on its way.
4
+
5
+ ```ruby
6
+ Edible.build(
7
+ decimal_notation: ',',
8
+ interchange: 'UNOA',
9
+ version: 2
10
+ ) do
11
+ segment('UNH', 1, 'PAORES', [93, 1, 'IA'])
12
+ end
13
+ ```
14
+
15
+ Produces this:
16
+
17
+ ```
18
+ UNA:+,? '
19
+ UNB+UNOA:2'
20
+ UNH+1+PAORES+93:1:IA'
21
+ ```
data/lib/edible.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Edible
4
+ autoload :Builder, 'edible/builder'
5
+
6
+ def self.build(**settings, &block)
7
+ out = String.new(capacity: 4096)
8
+ builder = Edible::Builder.new(out, **settings)
9
+ builder.una
10
+ builder.unb
11
+ builder.instance_eval(&block)
12
+ out
13
+ end
14
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Edible
4
+ class Builder
5
+ class InvalidError < StandardError; end
6
+
7
+ DEFAULTS = {
8
+ component_data_element_separator: ':',
9
+ data_element_separator: '+',
10
+ # The original decimal mark is actuall a comma, and depending on the UNB segment we must
11
+ # use either a dot, a comma, or either is acceptable. Any choice other than a dot will likely
12
+ # cause interoperability problems.
13
+ decimal_notation: '.',
14
+ release_indicator: '?',
15
+ segment_terminator: "'",
16
+
17
+ interchange: nil,
18
+ version: 1,
19
+
20
+ newline: "\n"
21
+ }.freeze
22
+
23
+ DELIMITERS = %i[
24
+ component_data_element_separator
25
+ data_element_separator
26
+ decimal_notation
27
+ release_indicator
28
+ segment_terminator
29
+ ].freeze
30
+
31
+ attr_reader(*DEFAULTS.keys)
32
+
33
+ def initialize(out, **settings)
34
+ @out = out
35
+ verify_settings(settings)
36
+ apply_settings(settings)
37
+ end
38
+
39
+ def delimiters?
40
+ @delimiters
41
+ end
42
+
43
+ def una
44
+ return unless delimiters?
45
+
46
+ segment('UNA') do
47
+ @out << component_data_element_separator
48
+ @out << data_element_separator
49
+ @out << decimal_notation
50
+ @out << release_indicator
51
+ @out << ' ' # A nice space that has been reserved since 1987.
52
+ end
53
+ end
54
+
55
+ def unb
56
+ return unless interchange
57
+
58
+ segment('UNB', [[interchange, version]])
59
+ end
60
+
61
+ def segment(segment, *components, &block)
62
+ @out << segment
63
+ instance_eval(&block) if block
64
+ components.each do |component|
65
+ @out << data_element_separator
66
+ @out << format(component)
67
+ end
68
+ @out << segment_terminator
69
+ @out << newline
70
+ end
71
+
72
+ def format(data)
73
+ data.is_a?(Enumerable) ? data.join(component_data_element_separator) : data.to_s
74
+ end
75
+
76
+ private
77
+
78
+ def verify_settings(settings)
79
+ unknown = settings.keys - DEFAULTS.keys
80
+ return if unknown.empty?
81
+
82
+ raise(ArgumentError, "unknown keywords: #{unknownmap(&:inspect).join(', ')}")
83
+ end
84
+
85
+ def apply_settings(settings)
86
+ @delimiters = !(DELIMITERS & settings.keys).empty?
87
+
88
+ DEFAULTS.each do |name, value|
89
+ instance_variable_set("@#{name}", settings.fetch(name, value))
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Edible
4
+ VERSION = '0.0.1'
5
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: edible
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Manfred Stienstra
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12'
41
+ description: |2
42
+ Edible providers a parser and builder for loading and generating EDIFACT data. It does not support
43
+ any specific message formats.
44
+ email:
45
+ - manfred@fngtps.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - LICENSE.txt
51
+ - README.md
52
+ - lib/edible.rb
53
+ - lib/edible/builder.rb
54
+ - lib/edible/version.rb
55
+ homepage: https://github.com/Manfred/edible
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '2.6'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.1.4
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Parser and builder for EDIFACT data.
78
+ test_files: []