]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | # Licensed to the Apache Software Foundation (ASF) under one |
2 | # or more contributor license agreements. See the NOTICE file | |
3 | # distributed with this work for additional information | |
4 | # regarding copyright ownership. The ASF licenses this file | |
5 | # to you under the Apache License, Version 2.0 (the | |
6 | # "License"); you may not use this file except in compliance | |
7 | # with the License. You may obtain a copy of the License at | |
8 | # | |
9 | # http://www.apache.org/licenses/LICENSE-2.0 | |
10 | # | |
11 | # Unless required by applicable law or agreed to in writing, | |
12 | # software distributed under the License is distributed on an | |
13 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
14 | # KIND, either express or implied. See the License for the | |
15 | # specific language governing permissions and limitations | |
16 | # under the License. | |
17 | ||
18 | module Arrow | |
19 | class ListDataType | |
20 | alias_method :initialize_raw, :initialize | |
21 | private :initialize_raw | |
22 | ||
23 | # Creates a new {Arrow::ListDataType}. | |
24 | # | |
25 | # @overload initialize(field) | |
26 | # | |
27 | # @param field [Arrow::Field, Hash] The field of the list data | |
28 | # type. You can also specify field description by `Hash`. | |
29 | # | |
30 | # See {Arrow::Field.new} how to specify field description. | |
31 | # | |
32 | # @example Create a list data type with {Arrow::Field} | |
33 | # visible_field = Arrow::Field.new("visible", :boolean) | |
34 | # Arrow::ListDataType.new(visible_field) | |
35 | # | |
36 | # @example Create a list data type with field description | |
37 | # Arrow::ListDataType.new(name: "visible", type: :boolean) | |
38 | # | |
39 | # @overload initialize(description) | |
40 | # | |
41 | # @param description [Hash] The description of the list data | |
42 | # type. It must have `:field` value. | |
43 | # | |
44 | # @option description [Arrow::Field, Hash] :field The field of | |
45 | # the list data type. You can also specify field description | |
46 | # by `Hash`. | |
47 | # | |
48 | # See {Arrow::Field.new} how to specify field description. | |
49 | # | |
50 | # @example Create a list data type with {Arrow::Field} | |
51 | # visible_field = Arrow::Field.new("visible", :boolean) | |
52 | # Arrow::ListDataType.new(field: visible_field) | |
53 | # | |
54 | # @example Create a list data type with field description | |
55 | # Arrow::ListDataType.new(field: {name: "visible", type: :boolean}) | |
56 | # | |
57 | # @overload initialize(data_type) | |
58 | # | |
59 | # @param data_type [Arrow::DataType, String, Symbol, | |
60 | # ::Array<String>, ::Array<Symbol>, Hash] The element data | |
61 | # type of the list data type. A field is created with the | |
62 | # default name `"item"` from the data type automatically. | |
63 | # | |
64 | # See {Arrow::DataType.resolve} how to specify data type. | |
65 | # | |
66 | # @example Create a list data type with {Arrow::DataType} | |
67 | # Arrow::ListDataType.new(Arrow::BooleanDataType.new) | |
68 | # | |
69 | # @example Create a list data type with data type name as String | |
70 | # Arrow::ListDataType.new("boolean") | |
71 | # | |
72 | # @example Create a list data type with data type name as Symbol | |
73 | # Arrow::ListDataType.new(:boolean) | |
74 | # | |
75 | # @example Create a list data type with data type as Array | |
76 | # Arrow::ListDataType.new([:time32, :milli]) | |
77 | def initialize(arg) | |
78 | data_type = resolve_data_type(arg) | |
79 | if data_type | |
80 | field = Field.new(default_field_name, data_type) | |
81 | else | |
82 | field = resolve_field(arg) | |
83 | end | |
84 | initialize_raw(field) | |
85 | end | |
86 | ||
87 | private | |
88 | def resolve_data_type(arg) | |
89 | case arg | |
90 | when DataType, String, Symbol, ::Array | |
91 | DataType.resolve(arg) | |
92 | when Hash | |
93 | return nil if arg[:name] | |
94 | return nil unless arg[:type] | |
95 | DataType.resolve(arg) | |
96 | else | |
97 | nil | |
98 | end | |
99 | end | |
100 | ||
101 | def default_field_name | |
102 | "item" | |
103 | end | |
104 | ||
105 | def resolve_field(arg) | |
106 | if arg.is_a?(Hash) and arg.key?(:field) | |
107 | description = arg | |
108 | arg = description[:field] | |
109 | end | |
110 | if arg.is_a?(Hash) | |
111 | field_description = arg | |
112 | Field.new(field_description) | |
113 | else | |
114 | arg | |
115 | end | |
116 | end | |
117 | end | |
118 | end |