Class: Expressir::Express::Builders::TypeBuilder
- Inherits:
-
Object
- Object
- Expressir::Express::Builders::TypeBuilder
- Includes:
- Helpers
- Defined in:
- lib/expressir/express/builders/type_builder.rb
Overview
Builds data type nodes (enumeration, select, array, bag, list, set, etc.).
Instance Method Summary collapse
-
#build_aggregate_type(ast_data) ⇒ Object
Aggregate type (AGGREGATE OF type).
- #build_aggregation_type(ast_data, type_class) ⇒ Object
-
#build_array_type(ast_data) ⇒ Object
Aggregation types.
- #build_bag_type(ast_data) ⇒ Object
-
#build_binary_type(ast_data) ⇒ Object
Binary type.
- #build_boolean_type(_ast_data) ⇒ Object
-
#build_enumeration_item(ast_data) ⇒ Object
Enumeration item.
-
#build_enumeration_type(ast_data) ⇒ Object
Enumeration type.
-
#build_general_aggregation_type(ast_data, type_class) ⇒ Object
General aggregation types (used in parameter types) These use parameter_type instead of instantiable_type.
- #build_general_array_type(ast_data) ⇒ Object
- #build_general_bag_type(ast_data) ⇒ Object
- #build_general_list_type(ast_data) ⇒ Object
- #build_general_set_type(ast_data) ⇒ Object
- #build_integer_type(_ast_data) ⇒ Object
- #build_list_type(ast_data) ⇒ Object
- #build_logical_type(_ast_data) ⇒ Object
- #build_number_type(_ast_data) ⇒ Object
-
#build_real_type(ast_data) ⇒ Object
Real type.
-
#build_select_type(ast_data) ⇒ Object
Select type.
- #build_set_type(ast_data) ⇒ Object
-
#build_string_type(ast_data) ⇒ Object
String type.
-
#build_type_wrapper(ast_data) ⇒ Object
Type wrappers - choice types that dispatch to inner type.
Methods included from Helpers
#apply_qualifier, #extract_id_ref, #extract_interval_op, #extract_nested_text, #extract_operator, #extract_rel_op, #extract_text, #extract_unary_op, #first_value
Instance Method Details
#build_aggregate_type(ast_data) ⇒ Object
Aggregate type (AGGREGATE OF type)
55 56 57 58 |
# File 'lib/expressir/express/builders/type_builder.rb', line 55 def build_aggregate_type(ast_data) base_type = Builder.build_optional(ast_data[:parameter_type]) Expressir::Model::DataTypes::Aggregate.new(base_type: base_type) end |
#build_aggregation_type(ast_data, type_class) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/expressir/express/builders/type_builder.rb', line 93 def build_aggregation_type(ast_data, type_class) bound_spec = ast_data[:bound_spec] || {} bound1 = Builder.build_optional(bound_spec[:bound1]) bound2 = Builder.build_optional(bound_spec[:bound2]) optional = !ast_data[:t_optional].nil? unique = !ast_data[:t_unique].nil? base_type = Builder.build_optional(ast_data[:instantiable_type]) type_class.new( bound1: bound1, bound2: bound2, optional: optional, unique: unique, base_type: base_type, ) end |
#build_array_type(ast_data) ⇒ Object
Aggregation types
61 62 63 |
# File 'lib/expressir/express/builders/type_builder.rb', line 61 def build_array_type(ast_data) build_aggregation_type(ast_data, Expressir::Model::DataTypes::Array) end |
#build_bag_type(ast_data) ⇒ Object
65 66 67 |
# File 'lib/expressir/express/builders/type_builder.rb', line 65 def build_bag_type(ast_data) build_aggregation_type(ast_data, Expressir::Model::DataTypes::Bag) end |
#build_binary_type(ast_data) ⇒ Object
Binary type
36 37 38 39 40 41 42 |
# File 'lib/expressir/express/builders/type_builder.rb', line 36 def build_binary_type(ast_data) width = Builder.build_optional(ast_data[:width_spec]&.dig(:width)) fixed = !ast_data[:width_spec]&.dig(:t_fixed).nil? kwargs = { width: width } kwargs[:fixed] = fixed if fixed Expressir::Model::DataTypes::Binary.new(**kwargs) end |
#build_boolean_type(_ast_data) ⇒ Object
10 11 12 |
# File 'lib/expressir/express/builders/type_builder.rb', line 10 def build_boolean_type(_ast_data) Expressir::Model::DataTypes::Boolean.new end |
#build_enumeration_item(ast_data) ⇒ Object
Enumeration item
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/expressir/express/builders/type_builder.rb', line 167 def build_enumeration_item(ast_data) id = nil if ast_data.is_a?(Hash) if ast_data[:enumeration_id] enum_id_data = ast_data[:enumeration_id] id = if enum_id_data.is_a?(Hash) extract_text(enum_id_data[:str]) || extract_text(enum_id_data[:simple_id]&.dig(:str)) else enum_id_data.to_s end elsif ast_data[:simple_id] id = extract_text(ast_data[:simple_id]&.dig(:str)) || extract_text(ast_data[:str]) elsif ast_data[:str] id = extract_text(ast_data[:str]) end elsif ast_data.is_a?(String) id = ast_data end Expressir::Model::DataTypes::EnumerationItem.new(id: id) end |
#build_enumeration_type(ast_data) ⇒ Object
Enumeration type
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/expressir/express/builders/type_builder.rb', line 126 def build_enumeration_type(ast_data) extensible = !ast_data[:t_extensible].nil? items = [] based_on = nil if ast_data[:enumeration_items].is_a?(Hash) items_data = ast_data[:enumeration_items][:list_of_enumeration_item] || ast_data[:enumeration_items][:enumeration_item] if items_data items_data = items_data[:enumeration_item] if items_data.is_a?(Hash) && items_data[:enumeration_item] items_data = [items_data] unless items_data.is_a?(Array) items = items_data.filter_map do |item| actual_item = item.is_a?(Hash) ? (item[:enumeration_item] || item) : item Builder.build({ enumeration_item: actual_item }) end end elsif ast_data[:enumeration_extension].is_a?(Hash) # Wrap type_ref data so the correct handler is called type_ref_data = ast_data[:enumeration_extension][:type_ref] based_on = Builder.build({ type_ref: type_ref_data }) if type_ref_data if ast_data[:enumeration_extension][:enumeration_items].is_a?(Hash) items_data = ast_data[:enumeration_extension][:enumeration_items][:list_of_enumeration_item] || ast_data[:enumeration_extension][:enumeration_items][:enumeration_item] if items_data items_data = items_data[:enumeration_item] if items_data.is_a?(Hash) && items_data[:enumeration_item] items_data = [items_data] unless items_data.is_a?(Array) items = items_data.filter_map do |item| actual_item = item.is_a?(Hash) ? (item[:enumeration_item] || item) : item Builder.build({ enumeration_item: actual_item }) end end end end kwargs = { based_on: based_on, items: items } kwargs[:extensible] = extensible if extensible Expressir::Model::DataTypes::Enumeration.new(**kwargs) end |
#build_general_aggregation_type(ast_data, type_class) ⇒ Object
General aggregation types (used in parameter types) These use parameter_type instead of instantiable_type
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/expressir/express/builders/type_builder.rb', line 112 def build_general_aggregation_type(ast_data, type_class) bound_spec = ast_data[:bound_spec] || {} bound1 = Builder.build_optional(bound_spec[:bound1]) bound2 = Builder.build_optional(bound_spec[:bound2]) base_type = Builder.build_optional(ast_data[:parameter_type]) type_class.new( bound1: bound1, bound2: bound2, base_type: base_type, ) end |
#build_general_array_type(ast_data) ⇒ Object
89 90 91 |
# File 'lib/expressir/express/builders/type_builder.rb', line 89 def build_general_array_type(ast_data) build_general_aggregation_type(ast_data, Expressir::Model::DataTypes::Array) end |
#build_general_bag_type(ast_data) ⇒ Object
85 86 87 |
# File 'lib/expressir/express/builders/type_builder.rb', line 85 def build_general_bag_type(ast_data) build_general_aggregation_type(ast_data, Expressir::Model::DataTypes::Bag) end |
#build_general_list_type(ast_data) ⇒ Object
81 82 83 |
# File 'lib/expressir/express/builders/type_builder.rb', line 81 def build_general_list_type(ast_data) build_general_aggregation_type(ast_data, Expressir::Model::DataTypes::List) end |
#build_general_set_type(ast_data) ⇒ Object
77 78 79 |
# File 'lib/expressir/express/builders/type_builder.rb', line 77 def build_general_set_type(ast_data) build_general_aggregation_type(ast_data, Expressir::Model::DataTypes::Set) end |
#build_integer_type(_ast_data) ⇒ Object
14 15 16 |
# File 'lib/expressir/express/builders/type_builder.rb', line 14 def build_integer_type(_ast_data) Expressir::Model::DataTypes::Integer.new end |
#build_list_type(ast_data) ⇒ Object
69 70 71 |
# File 'lib/expressir/express/builders/type_builder.rb', line 69 def build_list_type(ast_data) build_aggregation_type(ast_data, Expressir::Model::DataTypes::List) end |
#build_logical_type(_ast_data) ⇒ Object
18 19 20 |
# File 'lib/expressir/express/builders/type_builder.rb', line 18 def build_logical_type(_ast_data) Expressir::Model::DataTypes::Logical.new end |
#build_number_type(_ast_data) ⇒ Object
22 23 24 |
# File 'lib/expressir/express/builders/type_builder.rb', line 22 def build_number_type(_ast_data) Expressir::Model::DataTypes::Number.new end |
#build_real_type(ast_data) ⇒ Object
Real type
45 46 47 48 49 50 51 52 |
# File 'lib/expressir/express/builders/type_builder.rb', line 45 def build_real_type(ast_data) precision = Builder.build_optional(ast_data[:precision_spec]) if precision Expressir::Model::DataTypes::Real.new(precision: precision) else Expressir::Model::DataTypes::Real.new end end |
#build_select_type(ast_data) ⇒ Object
Select type
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/expressir/express/builders/type_builder.rb', line 189 def build_select_type(ast_data) extensible = !ast_data[:t_extensible].nil? generic_entity = !ast_data[:t_generic_entity].nil? items = [] based_on = nil if ast_data[:select_list].is_a?(Hash) named_types = ast_data[:select_list][:list_of_named_types] items = Builder.build_children(named_types) if named_types elsif ast_data[:select_extension].is_a?(Hash) # Wrap type_ref data so the correct handler is called if ast_data[:select_extension][:type_ref] based_on = Builder.build({ type_ref: ast_data[:select_extension][:type_ref] }) end if ast_data[:select_extension][:select_list].is_a?(Hash) named_types = ast_data[:select_extension][:select_list][:list_of_named_types] items = Builder.build_children(named_types) if named_types end end kwargs = { based_on: based_on, items: items.compact } kwargs[:extensible] = extensible if extensible kwargs[:generic_entity] = generic_entity if generic_entity Expressir::Model::DataTypes::Select.new(**kwargs) end |
#build_set_type(ast_data) ⇒ Object
73 74 75 |
# File 'lib/expressir/express/builders/type_builder.rb', line 73 def build_set_type(ast_data) build_aggregation_type(ast_data, Expressir::Model::DataTypes::Set) end |
#build_string_type(ast_data) ⇒ Object
String type
27 28 29 30 31 32 33 |
# File 'lib/expressir/express/builders/type_builder.rb', line 27 def build_string_type(ast_data) width = Builder.build_optional(ast_data[:width_spec]&.dig(:width)) fixed = !ast_data[:width_spec]&.dig(:t_fixed).nil? kwargs = { width: width } kwargs[:fixed] = fixed if fixed Expressir::Model::DataTypes::String.new(**kwargs) end |
#build_type_wrapper(ast_data) ⇒ Object
Type wrappers - choice types that dispatch to inner type
216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/expressir/express/builders/type_builder.rb', line 216 def build_type_wrapper(ast_data) return nil unless ast_data.is_a?(Hash) ast_data.each do |k, v| next unless v result = Builder.build({ k => v }) return result if result end nil end |