diff --git a/src/avram.cr b/src/avram.cr index 9e59b0987..6a79ba4d9 100644 --- a/src/avram.cr +++ b/src/avram.cr @@ -12,6 +12,7 @@ require "cadmium_transliterator" require "./ext/db/*" require "./ext/pg/*" +require "./avram/nothing" require "./avram/object_extensions" require "./avram/criteria" require "./avram/type" diff --git a/src/avram/attribute.cr b/src/avram/attribute.cr index f17fafad3..ae961b5a2 100644 --- a/src/avram/attribute.cr +++ b/src/avram/attribute.cr @@ -76,7 +76,7 @@ class Avram::Attribute(T) errors.empty? end - def changed?(from : T? | Nothing = Nothing.new, to : T? | Nothing = Nothing.new) : Bool + def changed?(from : T? | Nothing = IGNORE, to : T? | Nothing = IGNORE) : Bool from = from.is_a?(Nothing) ? true : from == original_value to = to.is_a?(Nothing) ? true : to == value value != original_value && from && to diff --git a/src/avram/base_query_template.cr b/src/avram/base_query_template.cr index 9c904fff9..ace778178 100644 --- a/src/avram/base_query_template.cr +++ b/src/avram/base_query_template.cr @@ -22,7 +22,7 @@ class Avram::BaseQueryTemplate def update( {% for column in columns %} - {{ column[:name] }} : {{ column[:type] }} | Avram::Nothing{% if column[:nilable] %} | Nil{% end %} = Avram::Nothing.new, + {{ column[:name] }} : {{ column[:type] }} | Avram::Nothing{% if column[:nilable] %} | Nil{% end %} = IGNORE, {% end %} ) : Int64 diff --git a/src/avram/needy_initializer.cr b/src/avram/needy_initializer.cr index 0f2e82d5e..551e76fe4 100644 --- a/src/avram/needy_initializer.cr +++ b/src/avram/needy_initializer.cr @@ -42,8 +42,8 @@ module Avram::NeedyInitializer # # attribute_method_args would look something like: # - # name : String | Avram::Nothing = Avram::Nothing.new, - # email : String | Nil | Avram::Nothing = Avram::Nothing.new + # name : String | Avram::Nothing = IGNORE, + # email : String | Nil | Avram::Nothing = IGNORE # # This can be passed to macros as a string, and then the macro can call .id # on it to output the string as code! @@ -58,7 +58,7 @@ module Avram::NeedyInitializer {% attribute_params = "" %} {% for attribute in ATTRIBUTES %} - {% attribute_method_args = attribute_method_args + "#{attribute.var} : #{attribute.type} | Avram::Nothing = Avram::Nothing.new,\n" %} + {% attribute_method_args = attribute_method_args + "#{attribute.var} : #{attribute.type} | Avram::Nothing = IGNORE,\n" %} {% attribute_params = attribute_params + "#{attribute.var}: #{attribute.var},\n" %} {% end %} diff --git a/src/avram/needy_initializer_and_delete_methods.cr b/src/avram/needy_initializer_and_delete_methods.cr index 9634a7711..b0734829a 100644 --- a/src/avram/needy_initializer_and_delete_methods.cr +++ b/src/avram/needy_initializer_and_delete_methods.cr @@ -44,8 +44,8 @@ module Avram::NeedyInitializerAndDeleteMethods # # attribute_method_args would look something like: # - # name : String | Avram::Nothing = Avram::Nothing.new, - # email : String | Nil | Avram::Nothing = Avram::Nothing.new + # name : String | Avram::Nothing = IGNORE, + # email : String | Nil | Avram::Nothing = IGNORE # # This can be passed to macros as a string, and then the macro can call .id # on it to output the string as code! @@ -63,14 +63,14 @@ module Avram::NeedyInitializerAndDeleteMethods {% for attribute in COLUMN_ATTRIBUTES.uniq %} {% attribute_method_args = attribute_method_args + "#{attribute[:name]} : #{attribute[:type]} | Avram::Nothing" %} {% if attribute[:nilable] %}{% attribute_method_args = attribute_method_args + " | Nil" %}{% end %} - {% attribute_method_args = attribute_method_args + " = Avram::Nothing.new,\n" %} + {% attribute_method_args = attribute_method_args + " = IGNORE,\n" %} {% attribute_params = attribute_params + "#{attribute[:name]}: #{attribute[:name]},\n" %} {% end %} {% end %} {% for attribute in ATTRIBUTES %} - {% attribute_method_args = attribute_method_args + "#{attribute.var} : #{attribute.type} | Avram::Nothing = Avram::Nothing.new,\n" %} + {% attribute_method_args = attribute_method_args + "#{attribute.var} : #{attribute.type} | Avram::Nothing = IGNORE,\n" %} {% attribute_params = attribute_params + "#{attribute.var}: #{attribute.var},\n" %} {% end %} diff --git a/src/avram/needy_initializer_and_save_methods.cr b/src/avram/needy_initializer_and_save_methods.cr index 0cea2eab8..ddbefd1c1 100644 --- a/src/avram/needy_initializer_and_save_methods.cr +++ b/src/avram/needy_initializer_and_save_methods.cr @@ -46,8 +46,8 @@ module Avram::NeedyInitializerAndSaveMethods # # attribute_method_args would look something like: # - # name : String | Avram::Nothing = Avram::Nothing.new, - # email : String | Nil | Avram::Nothing = Avram::Nothing.new + # name : String | Avram::Nothing = IGNORE, + # email : String | Nil | Avram::Nothing = IGNORE # # This can be passed to macros as a string, and then the macro can call .id # on it to output the string as code! @@ -65,14 +65,14 @@ module Avram::NeedyInitializerAndSaveMethods {% for attribute in COLUMN_ATTRIBUTES.uniq %} {% attribute_method_args = attribute_method_args + "#{attribute[:name]} : #{attribute[:type]} | Avram::Nothing" %} {% if attribute[:nilable] %}{% attribute_method_args = attribute_method_args + " | Nil" %}{% end %} - {% attribute_method_args = attribute_method_args + " = Avram::Nothing.new,\n" %} + {% attribute_method_args = attribute_method_args + " = IGNORE,\n" %} {% attribute_params = attribute_params + "#{attribute[:name]}: #{attribute[:name]},\n" %} {% end %} {% end %} {% for attribute in ATTRIBUTES.uniq %} - {% attribute_method_args = attribute_method_args + "#{attribute.var} : #{attribute.type} | Avram::Nothing = Avram::Nothing.new,\n" %} + {% attribute_method_args = attribute_method_args + "#{attribute.var} : #{attribute.type} | Avram::Nothing = IGNORE,\n" %} {% attribute_params = attribute_params + "#{attribute.var}: #{attribute.var},\n" %} {% end %} diff --git a/src/avram/nothing.cr b/src/avram/nothing.cr index dcfeabe0a..3e2a1a9bd 100644 --- a/src/avram/nothing.cr +++ b/src/avram/nothing.cr @@ -4,3 +4,14 @@ # :nodoc: class Avram::Nothing end + +# Use this value when you want to ignore updating a column +# in a SaveOperation instead of setting the column value to `nil`. +# +# ``` +# # Set value to x.value or ignore it if x.value is nil +# SaveThing.create!( +# value: x.value || IGNORE +# ) +# ``` +IGNORE = Avram::Nothing.new