Skip to content

Commit

Permalink
refactor (Mapping): move checking enumerables to #initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
vladfaust committed Feb 27, 2019
1 parent a129775 commit 94e7939
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
8 changes: 8 additions & 0 deletions src/onyx-sql/model.cr
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ module Onyx::SQL::Model
unless ivar.type.union_types.size == 2
raise "Only T | Nil unions can be an Onyx::SQL::Model's variables (got #{ivar.type} type for #{@type}@#{ivar.name})"
end

type = ivar.type.union_types.find { |t| t != Nil }

if type <= Enumerable
if (type.type_vars.size != 1 || type.type_vars.first.union?)
raise "If an Onyx::SQL::Model variable is a Enumerable, it must have a single non-union type var (got #{type} type for #{@type}@#{ivar.name})"
end
end
%}
{% end %}

Expand Down
22 changes: 0 additions & 22 deletions src/onyx-sql/model/mappable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,6 @@ module Onyx::SQL::Model::Mappable
def self.db_values(**values : **U) : Tuple forall U
{% verbatim do %}
{% begin %}
{% for ivar in @type.instance_vars %}
{%
a = 42

unless ivar.type.nilable?
raise "#{@type}@#{ivar.name} must be nilable, as it's an Onyx::SQL::Serializable variable"
end

unless ivar.type.union_types.size == 2
raise "Only T | Nil unions can be an Onyx::SQL::Serializable's variables (got #{ivar.type} type for #{@type}@#{ivar.name})"
end

type = ivar.type.union_types.find { |t| t != Nil }

if type <= Enumerable
if (type.type_vars.size != 1 && type.type_vars.first.union?)
raise "If an Onyx::SQL::Serializable variable is a Enumerable, it must have a single non-union type var (got #{type} type for #{@type}@#{ivar.name})"
end
end
%}
{% end %}

return {
{% for key, value in U %}
{% found = false %}
Expand Down

0 comments on commit 94e7939

Please sign in to comment.