Skip to content

Commit

Permalink
Added new Undefined constant (#1018)
Browse files Browse the repository at this point in the history
* Using a single instance to reference Nothing called Undefined instead of a ton of new instances. Fixes #1017

* Use IGNORE instead of Undefined so the purpose is a bit more clear.
  • Loading branch information
jwoertink authored Apr 8, 2024
1 parent f24ae0e commit 87a1ef9
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 13 deletions.
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

0 comments on commit 87a1ef9

Please sign in to comment.