Skip to content
Open
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
33 changes: 11 additions & 22 deletions lib/literal/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,23 @@ def index(name, type, unique: true, &block)
end

def where(**kwargs)
unless kwargs.length == 1
raise ArgumentError.new("You can only specify one index when using `where`.")
end

key, value = kwargs.first

types = @indexes_definitions.fetch(key)
type = types.first
Literal.check(value, type) { |c| raise NotImplementedError }

@indexes.fetch(key)[value]
end
results = []

def find_by(**kwargs)
unless kwargs.length == 1
raise ArgumentError.new("You can only specify one index when using `where`.")
end
kwargs.each do |key, lookup|
matches = []

key, value = kwargs.first
@indexes.fetch(key).each do |value, enums|
matches.concat(enums) if lookup === value
end

unless @indexes_definitions.fetch(key)[1]
raise ArgumentError.new("You can only use `find_by` on unique indexes.")
results << matches
end

type = @indexes_definitions.fetch(key)[0]
Literal.check(value, type)
results.inject(:&)
end

@indexes.fetch(key)[value]&.first
def find_by(**kwargs)
where(**kwargs).first
end

def _load(data)
Expand Down
Loading