lib/lutaml/model/attribute.rb in lutaml-model-0.3.1 vs lib/lutaml/model/attribute.rb in lutaml-model-0.3.2

- old
+ new

@@ -1,18 +1,31 @@ -# lib/lutaml/model/attribute.rb module Lutaml module Model class Attribute attr_reader :name, :type, :options def initialize(name, type, options = {}) @name = name - @type = type + @type = cast_type(type) + @options = options if collection? && !options[:default] @options[:default] = -> { [] } end + end + + def cast_type(type) + case type + when Class + type + when String + Type.const_get(type) + when Symbol + Type.const_get(type.to_s.split("_").collect(&:capitalize).join) + end + rescue NameError + raise ArgumentError, "Unknown Lutaml::Model::Type: #{type}" end def collection? options[:collection] || false end