lib/lutaml/model/attribute.rb in lutaml-model-0.3.24 vs lib/lutaml/model/attribute.rb in lutaml-model-0.3.25

- old
+ new

@@ -32,19 +32,27 @@ @options[:delegate] end def cast_type!(type) case type + when Symbol + begin + Type.lookup(type) + rescue UnknownTypeError + raise ArgumentError, "Unknown Lutaml::Model::Type: #{type}" + end + when String + begin + Type.const_get(type) + rescue NameError + raise ArgumentError, "Unknown Lutaml::Model::Type: #{type}" + end when Class type - when String - Type.const_get(type) - when Symbol - Type.const_get(type.to_s.split("_").collect(&:capitalize).join) + else + raise ArgumentError, "Unknown Lutaml::Model::Type: #{type}" end - rescue NameError - raise ArgumentError, "Unknown Lutaml::Model::Type: #{type}" end def cast_value(value) return type.cast(value) unless value.is_a?(Array) @@ -192,11 +200,13 @@ serialize(v, format, options) end elsif type <= Serialize type.hash_representation(value, format, options) else - type.serialize(value) + # Convert to Value instance if not already + value = type.new(value) unless value.is_a?(Type::Value) + value.send(:"to_#{format}") end end def cast(value, format, options = {}) value ||= [] if collection? @@ -206,10 +216,10 @@ cast(v, format, options) end elsif type <= Serialize && value.is_a?(Hash) type.apply_mappings(value, format, options) else - Lutaml::Model::Type.cast(value, type) + type.cast(value) end end private