Class: Lutaml::Model::Schema::XmlCompiler::SimpleType
- Inherits:
-
Object
- Object
- Lutaml::Model::Schema::XmlCompiler::SimpleType
- Defined in:
- lib/lutaml/model/schema/xml_compiler/simple_type.rb
Constant Summary collapse
- LUTAML_VALUE_CLASS_NAME =
"Lutaml::Model::Type::Value"- SUPPORTED_DATA_TYPES =
{ nonNegativeInteger: { skippable: false, class_name: "Lutaml::Model::Type::String", validations: { pattern: /\+?[0-9]+/ } }, normalizedString: { skippable: false, class_name: "Lutaml::Model::Type::String", validations: { transform: "value.gsub(/[\\r\\n\\t]/, ' ')" } }, positiveInteger: { skippable: false, class_name: "Lutaml::Model::Type::Integer", validations: { min_inclusive: 0 } }, unsignedShort: { skippable: false, class_name: "Lutaml::Model::Type::Integer", validations: { min_inclusive: 0, max_inclusive: 65535 } }, base64Binary: { skippable: false, class_name: "Lutaml::Model::Type::String", validations: { pattern: /\A([A-Za-z0-9+\/]+={0,2}|\s)*\z/ } }, unsignedLong: { skippable: false, class_name: "Lutaml::Model::Type::Integer", validations: { min_inclusive: 0, max_inclusive: 18446744073709551615 } }, unsignedByte: { skippable: false, class_name: "Lutaml::Model::Type::Integer", validations: { min_inclusive: 0, max_inclusive: 255 } }, unsignedInt: { skippable: false, class_name: "Lutaml::Model::Type::Integer", validations: { min_inclusive: 0, max_inclusive: 4294967295 } }, hexBinary: { skippable: false, class_name: "Lutaml::Model::Type::String", validations: { pattern: /([0-9a-fA-F]{2})*/ } }, language: { skippable: false, class_name: "Lutaml::Model::Type::String", validations: { pattern: /\A[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*\z/ } }, 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" }, decimal: { skippable: true, class_name: "Lutaml::Model::Type::Decimal" }, string: { skippable: true, class_name: "Lutaml::Model::Type::String" }, double: { skippable: true, class_name: "Lutaml::Model::Type::Float" }, NCName: { skippable: false, class_name: "Lutaml::Model::Type::String", validations: { pattern: /\A[a-zA-Z_][\w.-]*\z/ } }, anyURI: { skippable: false, class_name: "Lutaml::Model::Type::String", validations: { pattern: "\\A\#{URI::DEFAULT_PARSER.make_regexp(%w[http https ftp])}\\z" } }, token: { skippable: false, class_name: "Lutaml::Model::Type::String", validations: { pattern: /\A[^\t\n\f\r ]+(?: [^\t\n\f\r ]+)*\z/ } }, byte: { skippable: false, class_name: "Lutaml::Model::Type::Integer", validations: { min_inclusive: -128, max_inclusive: 127 } }, long: { skippable: false, class_name: "Lutaml::Model::Type::Decimal" }, int: { skippable: true, class_name: "Lutaml::Model::Type::Integer" }, id: { skippable: false, class_name: "Lutaml::Model::Type::String", validations: { pattern: /\A[a-zA-Z_][\w.-]*\z/ } }, }.freeze
- INSTANCE_MODEL_TEMPLATE =
ERB.new("# frozen_string_literal: true\nrequire \"lutaml/model\"\n\n<%= \"\\\#{required_files}\\n\" -%>\nclass <%= klass_name %><%= \" < \\\#{parent_class}\" if parent_class %>\n<%= @indent %>def self.cast(value, options = {})\n<%= extended_indent %>return if value.nil?\n\n<%= instance&.to_method_body(extended_indent) %>\n value = super(value, options)\n value\n end\n\n<%= @indent %>def self.register\n<%= extended_indent %>@register ||= Lutaml::Model::GlobalRegister.lookup(Lutaml::Model::Config.default_register)\n<%= @indent %>end\n\n<%= @indent %>def self.register_class_with_id\n<%= extended_indent %>register.register_model(self, id: :<%= Utils.snake_case(class_name) %>)\n<%= @indent %>end\nend\n\n<%= klass_name %>.register_class_with_id\n", trim_mode: "-")
- UNION_MODEL_TEMPLATE =
ERB.new("# frozen_string_literal: true\nrequire \"lutaml/model\"\n<%= union_required_files %>\n\nclass <%= klass_name %> < <%= LUTAML_VALUE_CLASS_NAME %>\n<%= @indent %>def self.cast(value, options = {})\n<%= extended_indent %>return if value.nil?\n\n<%= union_class_method_body %>\n end\n\n<%= @indent %>def self.register\n<%= extended_indent %>@register ||= Lutaml::Model::GlobalRegister.lookup(Lutaml::Model::Config.default_register)\n<%= @indent %>end\n\n<%= @indent %>def self.register_class_with_id\n<%= extended_indent %>register.register_model(self, id: :<%= Utils.snake_case(class_name) %>)\n<%= @indent %>end\nend\n\n<%= klass_name %>.register_class_with_id\n", trim_mode: "-")
Instance Attribute Summary collapse
-
#base_class ⇒ Object
Returns the value of attribute base_class.
-
#class_name ⇒ Object
Returns the value of attribute class_name.
-
#instance ⇒ Object
Returns the value of attribute instance.
-
#unions ⇒ Object
Returns the value of attribute unions.
Class Method Summary collapse
- .setup_restriction(base_class, validations) ⇒ Object
- .setup_supported_types ⇒ Object
- .skippable?(type) ⇒ Boolean
Instance Method Summary collapse
-
#initialize(name, unions = []) ⇒ SimpleType
constructor
A new instance of SimpleType.
- #required_files ⇒ Object
- #to_class(options: {}) ⇒ Object
Constructor Details
#initialize(name, unions = []) ⇒ SimpleType
Returns a new instance of SimpleType.
88 89 90 91 |
# File 'lib/lutaml/model/schema/xml_compiler/simple_type.rb', line 88 def initialize(name, unions = []) @class_name = name @unions = unions end |
Instance Attribute Details
#base_class ⇒ Object
Returns the value of attribute base_class.
8 9 10 |
# File 'lib/lutaml/model/schema/xml_compiler/simple_type.rb', line 8 def base_class @base_class end |
#class_name ⇒ Object
Returns the value of attribute class_name.
8 9 10 |
# File 'lib/lutaml/model/schema/xml_compiler/simple_type.rb', line 8 def class_name @class_name end |
#instance ⇒ Object
Returns the value of attribute instance.
8 9 10 |
# File 'lib/lutaml/model/schema/xml_compiler/simple_type.rb', line 8 def instance @instance end |
#unions ⇒ Object
Returns the value of attribute unions.
8 9 10 |
# File 'lib/lutaml/model/schema/xml_compiler/simple_type.rb', line 8 def unions @unions end |
Class Method Details
.setup_restriction(base_class, validations) ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/lutaml/model/schema/xml_compiler/simple_type.rb', line 169 def setup_restriction(base_class, validations) return unless validations Restriction.new.tap do |restriction| restriction.base_class = base_class restriction.min_inclusive = validations[:min_inclusive] restriction.max_inclusive = validations[:max_inclusive] restriction.pattern = validations[:pattern] restriction.transform = validations[:transform] end end |
.setup_supported_types ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/lutaml/model/schema/xml_compiler/simple_type.rb', line 156 def setup_supported_types SUPPORTED_DATA_TYPES .reject { |_, simple_type| simple_type[:skippable] } .each_with_object({}) do |(name, simple_type), hash| str_name = name.to_s new(str_name).tap do |instance| instance.base_class = Utils.base_class_snake_case(simple_type[:class_name]) instance.instance = setup_restriction(instance.base_class, simple_type[:validations]) hash[str_name] = instance end end end |
.skippable?(type) ⇒ Boolean
181 182 183 |
# File 'lib/lutaml/model/schema/xml_compiler/simple_type.rb', line 181 def skippable?(type) SUPPORTED_DATA_TYPES.dig(type&.to_sym, :skippable) end |
Instance Method Details
#required_files ⇒ Object
99 100 101 102 103 |
# File 'lib/lutaml/model/schema/xml_compiler/simple_type.rb', line 99 def required_files files = Array(instance&.required_files) files << "require_relative \"#{Utils.snake_case(parent_class)}\"" if require_parent? files.join("\n") end |
#to_class(options: {}) ⇒ Object
93 94 95 96 97 |
# File 'lib/lutaml/model/schema/xml_compiler/simple_type.rb', line 93 def to_class(options: {}) () template = unions&.any? ? UNION_MODEL_TEMPLATE : INSTANCE_MODEL_TEMPLATE template.result(binding) end |