tabulard 0.2.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 (72) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +202 -0
  3. data/README.md +43 -0
  4. data/VERSION +1 -0
  5. data/lib/sheetah/attribute.rb +60 -0
  6. data/lib/sheetah/attribute_types/composite.rb +57 -0
  7. data/lib/sheetah/attribute_types/scalar.rb +58 -0
  8. data/lib/sheetah/attribute_types/value.rb +62 -0
  9. data/lib/sheetah/attribute_types/value.rb.orig +68 -0
  10. data/lib/sheetah/attribute_types.rb +49 -0
  11. data/lib/sheetah/backends/csv.rb +92 -0
  12. data/lib/sheetah/backends/wrapper.rb +57 -0
  13. data/lib/sheetah/backends/xlsx.rb +80 -0
  14. data/lib/sheetah/backends.rb +11 -0
  15. data/lib/sheetah/column.rb +31 -0
  16. data/lib/sheetah/errors/error.rb +8 -0
  17. data/lib/sheetah/errors/spec_error.rb +10 -0
  18. data/lib/sheetah/errors/type_error.rb +10 -0
  19. data/lib/sheetah/frozen.rb +9 -0
  20. data/lib/sheetah/headers.rb +96 -0
  21. data/lib/sheetah/messaging/config.rb +19 -0
  22. data/lib/sheetah/messaging/constants.rb +17 -0
  23. data/lib/sheetah/messaging/message.rb +70 -0
  24. data/lib/sheetah/messaging/message_variant.rb +47 -0
  25. data/lib/sheetah/messaging/messages/cleaned_string.rb +18 -0
  26. data/lib/sheetah/messaging/messages/duplicated_header.rb +21 -0
  27. data/lib/sheetah/messaging/messages/invalid_header.rb +21 -0
  28. data/lib/sheetah/messaging/messages/missing_column.rb +21 -0
  29. data/lib/sheetah/messaging/messages/must_be_array.rb +18 -0
  30. data/lib/sheetah/messaging/messages/must_be_boolsy.rb +21 -0
  31. data/lib/sheetah/messaging/messages/must_be_date.rb +21 -0
  32. data/lib/sheetah/messaging/messages/must_be_email.rb +21 -0
  33. data/lib/sheetah/messaging/messages/must_be_string.rb +18 -0
  34. data/lib/sheetah/messaging/messages/must_exist.rb +18 -0
  35. data/lib/sheetah/messaging/messages/sheet_error.rb +18 -0
  36. data/lib/sheetah/messaging/messenger.rb +133 -0
  37. data/lib/sheetah/messaging/validations/base_validator.rb +43 -0
  38. data/lib/sheetah/messaging/validations/dsl.rb +31 -0
  39. data/lib/sheetah/messaging/validations/invalid_message.rb +12 -0
  40. data/lib/sheetah/messaging/validations/mixins.rb +57 -0
  41. data/lib/sheetah/messaging/validations.rb +35 -0
  42. data/lib/sheetah/messaging.rb +22 -0
  43. data/lib/sheetah/row_processor.rb +41 -0
  44. data/lib/sheetah/row_processor_result.rb +20 -0
  45. data/lib/sheetah/row_value_builder.rb +53 -0
  46. data/lib/sheetah/sheet/col_converter.rb +62 -0
  47. data/lib/sheetah/sheet.rb +107 -0
  48. data/lib/sheetah/sheet_processor.rb +61 -0
  49. data/lib/sheetah/sheet_processor_result.rb +18 -0
  50. data/lib/sheetah/specification.rb +30 -0
  51. data/lib/sheetah/template.rb +85 -0
  52. data/lib/sheetah/template_config.rb +35 -0
  53. data/lib/sheetah/types/cast.rb +20 -0
  54. data/lib/sheetah/types/cast_chain.rb +49 -0
  55. data/lib/sheetah/types/composites/array.rb +16 -0
  56. data/lib/sheetah/types/composites/array_compact.rb +13 -0
  57. data/lib/sheetah/types/composites/composite.rb +32 -0
  58. data/lib/sheetah/types/container.rb +81 -0
  59. data/lib/sheetah/types/scalars/boolsy.rb +12 -0
  60. data/lib/sheetah/types/scalars/boolsy_cast.rb +35 -0
  61. data/lib/sheetah/types/scalars/date_string.rb +12 -0
  62. data/lib/sheetah/types/scalars/date_string_cast.rb +43 -0
  63. data/lib/sheetah/types/scalars/email.rb +12 -0
  64. data/lib/sheetah/types/scalars/email_cast.rb +28 -0
  65. data/lib/sheetah/types/scalars/scalar.rb +29 -0
  66. data/lib/sheetah/types/scalars/scalar_cast.rb +49 -0
  67. data/lib/sheetah/types/scalars/string.rb +18 -0
  68. data/lib/sheetah/types/type.rb +103 -0
  69. data/lib/sheetah/utils/cell_string_cleaner.rb +29 -0
  70. data/lib/sheetah/utils/monadic_result.rb +174 -0
  71. data/lib/sheetah.rb +31 -0
  72. metadata +118 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 37d1717d37660df0f23595ffc330f02e879bfb1120d48e21ab2ef9618b70e4a0
4
+ data.tar.gz: 1cd3e6db0b7d53e8f9bf99256d70a41ba3be98672992ab0e50ea497215b1fea1
5
+ SHA512:
6
+ metadata.gz: 6925cbea3514865542b508f1f125c7eb61090e6574440eb26a4be8a5a92cb84dbf942d59d0e6308b9bf7d774c7f42984102ef5ee359ae58f51778b493e6aaf2c
7
+ data.tar.gz: 9f4dcfd80596aa30d1875ca1755bf4f61b896d27eb37fa21de326ccda2bb73919a480ad807da7de418e4570549784a7809a1d946b0f46e63818b7167d50012a0
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2022 Steeple
190
+ Copyright 2024 Erwan Thomas
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Tabulard
2
+
3
+ Tabulard is a library designed to process tabular data of any kind. It
4
+ is able to handle anything that looks like a table, and currently
5
+ provides dedicated adapters for CSV documents and XSLX sheets.
6
+
7
+ It focuses on typed results and fine-grain error management, while being
8
+ performant enough to handle (very) large documents and keep a low memory
9
+ footprint.
10
+
11
+ ## Documentation
12
+
13
+ The following resources document the latest valid state of the `master`
14
+ branch:
15
+
16
+ - [Ruby API](https://tabulard.github.io/tabulard/ruby)
17
+ - [Coverage](https://tabulard.github.io/tabulard/coverage)
18
+
19
+ ## Usage
20
+
21
+ Add the following to your Gemfile:
22
+
23
+ ```ruby
24
+ gem "tabulard", require: "sheetah/frozen"
25
+ ```
26
+
27
+ Then `bundle install`.
28
+
29
+ For examples of common use cases, please have a look at `spec/sheetah_spec.rb`.
30
+
31
+ ## Project status
32
+
33
+ Tabulard already works pretty well but is still under active
34
+ development.
35
+
36
+ It started as a fork of [Sheetah](https://github.com/steeple-org/sheetah),
37
+ a library used in production at Steeple and originally authored by the
38
+ now-maintainer of Tabulard.
39
+
40
+ For now, Tabulard aims to stay mostly retrocompatible with Sheetah. That
41
+ said, retrocompatibility is not a long-term goal of Tabulard, as its
42
+ roadmap will eventually bring important changes that can't be compatible
43
+ with Sheetah (such as renaming the top-level Ruby module, for example).
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.0
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "attribute_types"
4
+ require_relative "column"
5
+
6
+ module Sheetah
7
+ # The main building block of a {Template}.
8
+ class Attribute
9
+ # A smarter version of {#initialize}.
10
+ #
11
+ # - It automatically freezes the instance before returning it.
12
+ # - It instantiates and injects a type automatically by passing the arguments to
13
+ # {AttributeTypes.build}.
14
+ #
15
+ # @return [Attribute] a frozen instance
16
+ def self.build(key:, type:)
17
+ type = AttributeTypes.build(type)
18
+
19
+ attribute = new(key: key, type: type)
20
+ attribute.freeze
21
+ end
22
+
23
+ # @param key [String, Symbol] The key in the resulting Hash after processing a row.
24
+ # @param type [AttributeType] The type of the value.
25
+ def initialize(key:, type:)
26
+ @key = key
27
+ @type = type
28
+ end
29
+
30
+ # @return [Symbol, String]
31
+ attr_reader :key
32
+
33
+ # An abstract specification of the type of a value in the resulting hash.
34
+ #
35
+ # It will be used to produce the {Types::Type concrete type} of a column (or a list of columns)
36
+ # when a {TemplateConfig} is {Template#apply applied} to the {Template} owning the attribtue.
37
+ #
38
+ # @return [AttributeType]
39
+ attr_reader :type
40
+
41
+ def each_column(config)
42
+ return enum_for(:each_column, config) unless block_given?
43
+
44
+ compiled_type = type.compile(config.types)
45
+
46
+ type.each_column do |index, required|
47
+ header, header_pattern = config.header(key, index)
48
+
49
+ yield Column.new(
50
+ key: key,
51
+ type: compiled_type,
52
+ index: index,
53
+ header: header,
54
+ header_pattern: header_pattern,
55
+ required: required
56
+ )
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "value"
4
+
5
+ module Sheetah
6
+ module AttributeTypes
7
+ class Composite
8
+ # A smarter version of {#initialize}.
9
+ #
10
+ # - It automatically freezes the instance before returning it.
11
+ # - It instantiates, freezes and injects a list of values automatically by mapping the given
12
+ # list of scalars to a list of {Value values} using {Value.build}.
13
+ #
14
+ # @return [Composite] a frozen instance
15
+ def self.build(composite:, scalars:)
16
+ scalars = scalars.map { |scalar| Value.build(scalar) }
17
+ scalars.freeze
18
+
19
+ composite = new(composite: composite, scalars: scalars)
20
+ composite.freeze
21
+ end
22
+
23
+ # @param composite [Symbol] The name used to refer to a composite type from a
24
+ # {Types::Container}.
25
+ # @param scalars [Array<Value>] The list of values of the composite.
26
+ # @see .build
27
+ def initialize(composite:, scalars:)
28
+ @composite_type = composite
29
+ @values = scalars
30
+ end
31
+
32
+ def compile(container)
33
+ container.composite(composite_type, values.map(&:type))
34
+ end
35
+
36
+ def each_column
37
+ return enum_for(:each_column) { values.size } unless block_given?
38
+
39
+ values.each_with_index do |value, index|
40
+ yield index, value.required
41
+ end
42
+
43
+ self
44
+ end
45
+
46
+ def ==(other)
47
+ other.is_a?(self.class) &&
48
+ composite_type == other.composite_type &&
49
+ values == other.values
50
+ end
51
+
52
+ protected
53
+
54
+ attr_reader :composite_type, :values
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "value"
4
+
5
+ module Sheetah
6
+ module AttributeTypes
7
+ # @see AttributeType
8
+ class Scalar
9
+ # @!parse
10
+ # include AttributeType
11
+
12
+ # A smarter version of {#initialize}.
13
+ #
14
+ # - It automatically freezes the instance before returning it.
15
+ # - It instantiates and injects a value automatically by passing the arguments to
16
+ # {Value.build}.
17
+ #
18
+ # The method signature is identical to the one of {Value.build}.
19
+ #
20
+ # @return [Scalar] a frozen instance
21
+ def self.build(...)
22
+ value = Value.build(...)
23
+
24
+ scalar = new(value)
25
+ scalar.freeze
26
+ end
27
+
28
+ # @param value [Value] The value of the scalar.
29
+ # @see .build
30
+ def initialize(value)
31
+ @value = value
32
+ end
33
+
34
+ # @see AttributeType#compile
35
+ def compile(container)
36
+ container.scalar(value.type)
37
+ end
38
+
39
+ # @see AttributeType#each_column
40
+ def each_column
41
+ return enum_for(:each_column) { 1 } unless block_given?
42
+
43
+ yield nil, value.required
44
+
45
+ self
46
+ end
47
+
48
+ def ==(other)
49
+ other.is_a?(self.class) &&
50
+ value == other.value
51
+ end
52
+
53
+ protected
54
+
55
+ attr_reader :value
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sheetah
4
+ module AttributeTypes
5
+ class Value
6
+ # A smarter version of {#initialize}.
7
+ #
8
+ # - It automatically freezes the instance before returning it.
9
+ # - It accepts two kinds of parameters: either those of {#initialize}, or a shortcut. See the
10
+ # overloaded method signature for details.
11
+ #
12
+ # @overload def build(type:, required:)
13
+ # @example
14
+ # Value.build(type: :foo, required: true)
15
+ # @see #initialize
16
+ #
17
+ # @overload def build(type)
18
+ # @param type [Symbol] The name of the type, optionally suffixed with `!` to indicate that
19
+ # the value is required.
20
+ # @example
21
+ # Value.build(:foo) #=> Value.build(type: :foo, required: false)
22
+ # Value.build(:foo!) #=> Value.build(type: :foo, required: true)
23
+ #
24
+ # @return [Value] a frozen instance
25
+ def self.build(arg)
26
+ value = arg.is_a?(Hash) ? new(**arg) : from_type_name(arg)
27
+ value.freeze
28
+ end
29
+
30
+ def self.from_type_name(type)
31
+ type = type.to_s
32
+
33
+ optional = type.end_with?("?")
34
+ type = (optional ? type.slice(0..-2) : type).to_sym
35
+
36
+ new(type: type, required: !optional)
37
+ end
38
+
39
+ private_class_method :from_type_name
40
+
41
+ # @param type [Symbol] The name used to refer to a scalar type from a {Types::Container}.
42
+ # @param required [Boolean] Is the value required to be given in the input ?
43
+ # @see .build
44
+ def initialize(type:, required: true)
45
+ @type = type
46
+ @required = required
47
+ end
48
+
49
+ # @return [Symbol]
50
+ attr_reader :type
51
+
52
+ # @return [Boolean]
53
+ attr_reader :required
54
+
55
+ def ==(other)
56
+ other.is_a?(self.class) &&
57
+ type == other.type &&
58
+ required == other.required
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sheetah
4
+ module AttributeTypes
5
+ class Value
6
+ # A smarter version of {#initialize}.
7
+ #
8
+ # - It automatically freezes the instance before returning it.
9
+ # - It accepts two kinds of parameters: either those of {#initialize}, or a shortcut. See the
10
+ # overloaded method signature for details.
11
+ #
12
+ # @overload def build(type:, required:)
13
+ # @example
14
+ # Value.build(type: :foo, required: true)
15
+ # @see #initialize
16
+ #
17
+ # @overload def build(type)
18
+ # @param type [Symbol] The name of the type, optionally suffixed with `!` to indicate that
19
+ # the value is required.
20
+ # @example
21
+ # Value.build(:foo) #=> Value.build(type: :foo, required: false)
22
+ # Value.build(:foo!) #=> Value.build(type: :foo, required: true)
23
+ #
24
+ # @return [Value] a frozen instance
25
+ def self.build(arg)
26
+ value = arg.is_a?(Hash) ? new(**arg) : from_type_name(arg)
27
+ value.freeze
28
+ end
29
+
30
+ def self.from_type_name(type)
31
+ type = type.to_s
32
+
33
+ optional = type.end_with?("?")
34
+ type = (optional ? type.slice(0..-2) : type).to_sym
35
+
36
+ new(type: type, required: !optional)
37
+ end
38
+
39
+ private_class_method :from_type_name
40
+
41
+ <<<<<<< HEAD
42
+ # @param type [Symbol] The name used to refer to a scalar type from a {Types::Container}.
43
+ # @param required [Boolean] Is the value required to be given in the input ?
44
+ # @see .build
45
+ def initialize(type:, required: false)
46
+ ||||||| parent of 98cec2b (Value required by default)
47
+ def initialize(type:, required: false)
48
+ =======
49
+ def initialize(type:, required: true)
50
+ >>>>>>> 98cec2b (Value required by default)
51
+ @type = type
52
+ @required = required
53
+ end
54
+
55
+ # @return [Symbol]
56
+ attr_reader :type
57
+
58
+ # @return [Boolean]
59
+ attr_reader :required
60
+
61
+ def ==(other)
62
+ other.is_a?(self.class) &&
63
+ type == other.type &&
64
+ required == other.required
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "attribute_types/scalar"
4
+ require_relative "attribute_types/composite"
5
+
6
+ module Sheetah
7
+ module AttributeTypes
8
+ # A factory for all {AttributeType}.
9
+ def self.build(type)
10
+ if type.is_a?(Hash) && type.key?(:composite)
11
+ Composite.build(**type)
12
+ elsif type.is_a?(Array)
13
+ Composite.build(composite: :array, scalars: type)
14
+ else
15
+ Scalar.build(type)
16
+ end
17
+ end
18
+ end
19
+
20
+ # @!parse
21
+ # # The minimal interface of an {Attribute} type.
22
+ # # @abstract It only exists to document the interface implemented by the different classes of
23
+ # # {AttributeTypes}.
24
+ # module AttributeType
25
+ # # A smarter version of `#initialize`, that will always return a frozen instance of the
26
+ # # type, with optional syntactic sugar for its arguments compared to `#initialize`.
27
+ # def self.build(*); end
28
+ #
29
+ # # Given a type container, return the actual, usable type for the attribute.
30
+ # # @param container [Types::Container]
31
+ # # @return [Types::Type]
32
+ # def compile(container); end
33
+ #
34
+ # # Enumerate the columns (one or more) that may compose the attribute in the tabular
35
+ # # document.
36
+ # #
37
+ # # @yieldparam index [Integer, nil]
38
+ # # If there is only one column involved, then `index` will be `nil`. Otherwise, `index`
39
+ # # will start at `0` and increase by `1` at each step.
40
+ # # @yieldparam required [Boolean]
41
+ # # Whether the column must be present in the tabular document.
42
+ # #
43
+ # # @overload def each_column(&block)
44
+ # # @return [self]
45
+ # # @overload def each_column
46
+ # # @return [Enumerator]
47
+ # def each_column; end
48
+ # end
49
+ end