Skip to content

Commit

Permalink
Add a criteria method to the type adapter (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmcgarvey authored Jan 6, 2021
1 parent efe1d8a commit 6620951
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 16 deletions.
4 changes: 4 additions & 0 deletions spec/support/custom_email.cr
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ class CustomEmail
alias ColumnType = String
include Avram::Type

def self.criteria(query : T, column) forall T
Criteria(T, CustomEmail).new(query, column)
end

def parse(value : CustomEmail)
SuccessfulCast(CustomEmail).new(value)
end
Expand Down
22 changes: 6 additions & 16 deletions src/avram/base_query_template.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ class Avram::BaseQueryTemplate
include Avram::PrimaryKeyQueryable({{ type }})
{% end %}

macro generate_criteria_method(query_class, name, type)
macro generate_criteria_method(name, type)
def \{{ name }}
column_name = "#{table_name}.\{{ name }}"
\{{ type }}::Lucky::Criteria(\{{ query_class }}, \{{ type }}).new(self, column_name)
\{{ type }}.adapter.criteria(self, "#{table_name}.\{{ name }}")
end
end

Expand Down Expand Up @@ -48,20 +47,11 @@ class Avram::BaseQueryTemplate
{{ column[:name] }}.eq(value)
end

{% if column[:type].is_a?(Generic) %}
# Checking Array type
generate_criteria_method(BaseQuery, {{ column[:name] }}, {{ column[:type].type_vars.first }})
generate_criteria_method({{ column[:name] }}, {{ column[:type] }})

macro inherited
generate_criteria_method(\{{ @type.name }}, {{ column[:name] }}, {{ column[:type].type_vars.first }})
end
{% else %}
generate_criteria_method(BaseQuery, {{ column[:name] }}, {{ column[:type] }})

macro inherited
generate_criteria_method(\{{ @type.name }}, {{ column[:name] }}, {{ column[:type] }})
end
{% end %}
macro inherited
generate_criteria_method({{ column[:name] }}, {{ column[:type] }})
end
{% end %}

{% for assoc in associations %}
Expand Down
4 changes: 4 additions & 0 deletions src/avram/charms/bool_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ struct Bool
alias ColumnType = Bool
include Avram::Type

def self.criteria(query : T, column) forall T
Criteria(T, Bool).new(query, column)
end

def parse(value : String)
if %w(true 1).includes? value
SuccessfulCast(Bool).new true
Expand Down
4 changes: 4 additions & 0 deletions src/avram/charms/enum_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ macro avram_enum(enum_name, &block)
alias ColumnType = Int32
include Avram::Type

def self.criteria(query : T, column) forall T
Criteria(T, Int32).new(query, column)
end

def from_db!(value : Int32)
{{ enum_name }}.new(value)
end
Expand Down
4 changes: 4 additions & 0 deletions src/avram/charms/float64_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ struct Float64
alias ColumnType = Float64
include Avram::Type

def self.criteria(query : T, column) forall T
Criteria(T, Float64).new(query, column)
end

def from_db!(value : Float64)
value
end
Expand Down
4 changes: 4 additions & 0 deletions src/avram/charms/int16_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ struct Int16
alias ColumnType = Int16
include Avram::Type

def self.criteria(query : T, column) forall T
Criteria(T, Int16).new(query, column)
end

def from_db!(value : Int16)
value
end
Expand Down
4 changes: 4 additions & 0 deletions src/avram/charms/int32_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ struct Int32
alias ColumnType = Int32
include Avram::Type

def self.criteria(query : T, column) forall T
Criteria(T, Int32).new(query, column)
end

def from_db!(value : Int32)
value
end
Expand Down
4 changes: 4 additions & 0 deletions src/avram/charms/int64_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ struct Int64
alias ColumnType = Int64
include Avram::Type

def self.criteria(query : T, column) forall T
Criteria(T, Int64).new(query, column)
end

def from_db!(value : Int64)
value
end
Expand Down
4 changes: 4 additions & 0 deletions src/avram/charms/json_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ struct JSON::Any
alias ColumnType = JSON::Any
include Avram::Type

def self.criteria(query : T, column) forall T
Criteria(T, JSON::Any).new(query, column)
end

def from_db!(value : JSON::Any)
value
end
Expand Down
4 changes: 4 additions & 0 deletions src/avram/charms/string_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class String
alias ColumnType = String
include Avram::Type

def self.criteria(query : T, column) forall T
Criteria(T, String).new(query, column)
end

def parse(value : String)
SuccessfulCast(String).new(value)
end
Expand Down
4 changes: 4 additions & 0 deletions src/avram/charms/time_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ struct Time
Time::Format::ISO_8601_TIME,
]

def self.criteria(query : T, column) forall T
Criteria(T, Time).new(query, column)
end

def from_db!(value : Time)
value
end
Expand Down
4 changes: 4 additions & 0 deletions src/avram/charms/uuid_extensions.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ struct UUID
alias ColumnType = UUID
include Avram::Type

def self.criteria(query : T, column) forall T
Criteria(T, UUID).new(query, column)
end

def parse(value : UUID)
SuccessfulCast(UUID).new(value)
end
Expand Down

0 comments on commit 6620951

Please sign in to comment.