Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added new IGNORE constant #1018

Merged
merged 2 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/avram.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion src/avram/attribute.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/avram/base_query_template.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions src/avram/needy_initializer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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 %}

Expand Down
8 changes: 4 additions & 4 deletions src/avram/needy_initializer_and_delete_methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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 %}

Expand Down
8 changes: 4 additions & 4 deletions src/avram/needy_initializer_and_save_methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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 %}

Expand Down
11 changes: 11 additions & 0 deletions src/avram/nothing.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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