diff --git a/lib/class_kit/class_methods.rb b/lib/class_kit/class_methods.rb index dfedbe4..761277b 100644 --- a/lib/class_kit/class_methods.rb +++ b/lib/class_kit/class_methods.rb @@ -3,6 +3,7 @@ def attr_accessor_type( name, type: nil, collection_type: nil, + one_of: nil, allow_nil: true, default: nil, auto_init: false, @@ -17,6 +18,7 @@ def attr_accessor_type( attributes[name] = { name: name, type: type, + one_of: one_of, collection_type: collection_type, allow_nil: allow_nil, default: default, @@ -54,7 +56,16 @@ def attr_accessor_type( raise ClassKit::Exceptions::InvalidAttributeValueError, "Attribute: #{name}, must not be nil." end - # check if the value being set is not of the specified type and should attempt to parse the value + if !cka[:one_of].nil? && !value.nil? && ([Hash, String].any? { |t| value.is_a?(t) }) + type = cka[:one_of].detect { |t| value.is_a?(t) } + value = ClassKit::ValueHelper.instance.parse(type: type, value: value) + end + + puts '-' * 50 + puts "#{cka}" + puts "Value: #{value}" + puts '-' * 50 + if !cka[:type].nil? && !value.nil? && (cka[:type] == :bool || !value.is_a?(cka[:type])) begin value = ClassKit::ValueHelper.instance.parse(type: cka[:type], value: value)