Module: Lutaml::Model::Schema::Templates::SimpleType

Extended by:
SimpleType
Included in:
SimpleType
Defined in:
lib/lutaml/model/schema/templates/simple_type.rb

Constant Summary collapse

DEFAULT_CLASSES =
%w[int integer string boolean].freeze
SUPPORTED_DATA_TYPES =
{
  nonNegativeInteger: { class_name: "Lutaml::Model::Type::String", validations: { pattern: /\+?[0-9]+/ } },
  positiveInteger: { class_name: "Lutaml::Model::Type::Integer", validations: { min: 0 } },
  base64Binary: { class_name: "Lutaml::Model::Type::String", validations: { pattern: /\A([A-Za-z0-9+\/]+={0,2}|\s)*\z/ } },
  unsignedLong: { class_name: "Lutaml::Model::Type::Integer", validations: { min: 0, max: 18446744073709551615 } },
  unsignedInt: { class_name: "Lutaml::Model::Type::Integer", validations: { min: 0, max: 4294967295 } },
  hexBinary: { class_name: "Lutaml::Model::Type::String", validations: { pattern: /([0-9a-fA-F]{2})*/ } },
  dateTime: { skippable: true, class_name: "Lutaml::Model::Type::DateTime" },
  boolean: { skippable: true, class_name: "Lutaml::Model::Type::Boolean" },
  integer: { skippable: true, class_name: "Lutaml::Model::Type::Integer" },
  string: { skippable: true, class_name: "Lutaml::Model::Type::String" },
  token: { class_name: "Lutaml::Model::Type::String", validations: { pattern: /\A[^\t\n\f\r ]+(?: [^\t\n\f\r ]+)*\z/ } },
  long: { class_name: "Lutaml::Model::Type::Decimal" },
  int: { skippable: true, class_name: "Lutaml::Model::Type::Integer" },
}.freeze
REF_TEMPLATE =
ERB.new("# frozen_string_literal: true\n\nrequire \"lutaml/model\"\n<%= \"require_relative \\\#{Utils.snake_case(parent_class).inspect}\\n\" if require_parent -%>\n\nclass <%= klass_name %> < <%= parent_class %>; end\n", trim_mode: "-")
SUPPORTED_TYPES_TEMPLATE =
ERB.new("# frozen_string_literal: true\n\nrequire \"lutaml/model\"\n\nclass <%= Utils.camel_case(klass_name.to_s) %> < <%= properties[:class_name].to_s %>\n  def self.cast(value)\n    return nil if value.nil?\n\n    value = super(value)\n<%=\n  if pattern_exist = validations.key?(:pattern)\n    \"    pattern = %r{\\\#{validations[:pattern]}}\\n\\\#{indent}raise Lutaml::Model::Type::InvalidValueError, \\\\\"The value \\\\\\\#{value} does not match the required pattern: \\\\\\\#{pattern}\\\\\" unless value.match?(pattern)\\n\"\n  end\n-%>\n<%=\n  if min_exist = validations.key?(:min)\n    \"    min = \\\#{validations[:min]}\\n\\\#{indent}raise Lutaml::Model::Type::InvalidValueError, \\\\\"The value \\\\\\\#{value} is less than the set limit: \\\\\\\#{min}\\\\\" if value < min\\n\"\n  end\n-%>\n<%=\n  if max_exist = validations.key?(:max)\n    \"    max = \\\#{validations[:max]}\\n\\\#{indent}raise Lutaml::Model::Type::InvalidValueError, \\\\\"The value \\\\\\\#{value} is greater than the set limit: \\\\\\\#{max}\\\\\" if value > max\\n\"\n  end\n-%>\n    value\n  end\nend\n", trim_mode: "-")
UNION_TEMPLATE =
ERB.new("# frozen_string_literal: true\n\nrequire \"lutaml/model\"\n<%=\n  resolve_required_files(unions)&.map do |file|\n    next if file.nil? || file.empty?\n\n    \"require_relative \\\\\\\"\\\#{file}\\\\\\\"\"\n  end.compact.join(\"\\n\") + \"\\n\"\n-%>\n\nclass <%= klass_name %> < Lutaml::Model::Type::Value\n  def self.cast(value)\n    return nil if value.nil?\n\n    <%= unions.map do |union|\n      base_class = union.base_class.split(':').last\n      if DEFAULT_CLASSES.include?(base_class)\n        \"\\\#{SUPPORTED_DATA_TYPES.dig(base_class.to_sym, :class_name)}.cast(value)\"\n      else\n        \"\\\#{Utils.camel_case(base_class)}.cast(value)\"\n      end\n    end.join(\" || \") %>\n  end\nend\n", trim_mode: "-")
MODEL_TEMPLATE =
ERB.new("# frozen_string_literal: true\nrequire \"lutaml/model\"\n<%= \"require_relative '\\\#{Utils.snake_case(parent_class)}'\\n\" if require_parent -%>\n\nclass <%= klass_name %> < <%= parent_class %>\n<%= \"  VALUES = \\\#{values}.freeze\\n\\n\" if values_exist = values&.any? -%>\n<%= \"  LENGTHS = \\\#{properties[:length]&.map(&:value)}\\n\\n\" if length_exist = properties&.key?(:length) -%>\n  def self.cast(value)\n    return nil if value.nil?\n\n    value = super(value)\n<%= \"    raise_values_error(value) unless VALUES.include?(value)\\n\" if values_exist -%>\n<%= \"    raise_length_error(value) unless LENGTHS.all?(value.length)\\n\" if length_exist -%>\n<%=\n  if pattern_exist = properties.key?(:pattern)\n    \"    pattern = %r{\\\#{properties[:pattern]}}\\n    raise_pattern_error(value, pattern) unless value.match?(pattern)\\n\"\n  end\n-%>\n<%=\n  if min_length_exist = properties&.key_exist?(:min_length)\n    \"    min_length = \\\#{properties.min_length}\\n    raise_min_length_error(value, min_length) unless value.length >= min_length\\n\"\n  end\n-%>\n<%=\n  if max_length_exist = properties&.key_exist?(:max_length)\n    \"    max_length = \\\#{properties.max_length}\\n    raise_max_length_error(value, max_length) unless value.length <= max_length\\n\"\n  end\n-%>\n<%=\n  if min_bound_exist = (properties&.key_exist?(:min_inclusive) || properties&.key_exist?(:min_exclusive))\n    \"    min_bound = \\\#{properties[:min_inclusive] || properties[:min_exclusive]}\\n    raise_min_bound_error(value, min_bound) unless value >\\\#{'=' if properties.key?(:min_inclusive)} min_bound \\n\"\n  end\n-%>\n<%=\n  if max_bound_exist = (properties&.key_exist?(:max_inclusive) || properties&.key_exist?(:max_exclusive))\n    \"    max_bound = \\\#{properties[:max_inclusive] || properties[:max_exclusive]}\\n    raise_max_bound_error(value, max_bound) unless value <\\\#{'=' if properties.key?(:max_inclusive)} max_bound \\n\"\n  end\n-%>\n    <%= \"value\" %>\n  end\n<%= \"\\n  private\\n\" if pattern_exist || values_exist || length_exist || min_length_exist || max_length_exist || min_bound_exist || max_bound_exist -%>\n<%=\n  if pattern_exist\n    \"\\n  def self.raise_pattern_error(value, pattern)\\n    raise Lutaml::Model::Type::InvalidValueError, \\\\\"The value \\\\\\\#{value} does not match the required pattern: \\\\\\\#{pattern}\\\\\"\\n  end\\n\"\n  end\n-%>\n<%=\n  if values_exist\n    \"\\n  def self.raise_values_error(input_value)\\n    raise Lutaml::Model::InvalidValueError.new(self, input_value, VALUES)\\n  end\\n\"\n  end\n-%>\n<%=\n  if length_exist\n    \"\\n  def self.raise_length_error(input_value)\\n    raise Lutaml::Model::Type::InvalidValueError, \\\\\"The provided value \\\\\\\\\\\\\"\\\\\\\#{input_value}\\\\\\\\\\\\\" should match the specified lengths: \\\\\\\#{LENGTHS.join(',')}\\\\\"\\n  end\\n\"\n  end\n-%>\n<%=\n  if min_length_exist\n    \"\\n  def self.raise_min_length_error(input_value, min_length)\\n    raise Lutaml::Model::Type::InvalidValueError, \\\\\"The provided value \\\\\\\\\\\\\"\\\\\\\#{input_value}\\\\\\\\\\\\\" has fewer characters than the minimum allowed \\\\\\\#{min_length}\\\\\"\\n  end\\n\"\n  end\n-%>\n<%=\n  if max_length_exist\n    \"\\n  def self.raise_max_length_error(input_value, max_length)\\n    raise Lutaml::Model::Type::InvalidValueError, \\\\\"The provided value \\\\\\\\\\\\\"\\\\\\\#{input_value}\\\\\\\\\\\\\" exceeds the maximum allowed length of \\\\\\\#{max_length}\\\\\"\\n  end\\n\"\n  end\n-%>\n<%=\n  if min_bound_exist\n    \"\\n  def self.raise_min_bound_error(input_value, min_bound)\\n    raise Lutaml::Model::Type::InvalidValueError, \\\\\"The provided value \\\\\\\\\\\\\"\\\\\\\#{input_value}\\\\\\\\\\\\\" is less than the minimum allowed value of \\\\\\\#{min_bound}\\\\\"\\n  end\\n\"\n  end\n-%>\n<%=\n  if max_bound_exist\n    \"\\n  def self.raise_max_bound_error(input_value, max_bound)\\n    raise Lutaml::Model::Type::InvalidValueError, \\\\\"The provided value \\\\\\\\\\\\\"\\\\\\\#{input_value}\\\\\\\\\\\\\" exceeds the maximum allowed value of \\\\\\\#{max_bound}\\\\\"\\n  end\\n\"\n  end\n-%>\nend\n", trim_mode: "-")

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#simple_typesObject

Returns the value of attribute simple_types.



9
10
11
# File 'lib/lutaml/model/schema/templates/simple_type.rb', line 9

def simple_types
  @simple_types
end

Instance Method Details

#create_simple_types(simple_types) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/lutaml/model/schema/templates/simple_type.rb', line 176

def create_simple_types(simple_types)
  setup_supported_types
  simple_types.each do |name, properties|
    klass_name = Utils.camel_case(name)
    @simple_types[name] = if @simple_types.key?(properties[:base_class]) && properties.one?
                            ref_template(properties, klass_name)
                          elsif properties&.key_exist?(:union)
                            union_template(properties, klass_name)
                          else
                            model_template(properties, klass_name)
                          end
  end
  @simple_types
end