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

Lineno refactor #56

Merged
merged 2 commits into from
Oct 18, 2023
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
11 changes: 3 additions & 8 deletions lib/oaken/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def load_onto(seeds)
transaction do
if replay?
puts "Replaying #{@file}…"
readers.each do |key, name, id, lineno|
seeds.send(key).instance_eval "def #{name}; find #{id}; end", @file, lineno
readers.each do |key, *args|
define_reader(seeds.send(key), *args)
end
else
reset
Expand All @@ -43,13 +43,8 @@ def reset
self.readers = Set.new
end

def define_reader(stored, name, id)
lineno = self.lineno
def define_reader(stored, name, id, lineno)
stored.instance_eval "def #{name}; find #{id}; end", @file, lineno
readers << [stored.key, name, id, lineno]
end

def lineno
caller_locations(3, 10).find { _1.path == @file }.lineno
end
end
12 changes: 8 additions & 4 deletions lib/oaken/stored/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ def initialize(type, key = nil)
delegate :find, :insert_all, to: :type

def create(reader = nil, **attributes)
lineno = caller_locations(1, 1).first.lineno

type.create!(**attributes).tap do |record|
define_reader reader, record.id if reader
define_reader reader, record.id, lineno if reader
end
end

def insert(reader = nil, **attributes)
lineno = caller_locations(1, 1).first.lineno

type.new(attributes).validate!
type.insert(attributes).tap do
define_reader reader, type.where(attributes).pick(:id) if reader
define_reader reader, type.where(attributes).pick(:id), lineno if reader
end
end

def define_reader(name, id)
Oaken::Seeds.entry.define_reader(self, name, id)
def define_reader(...)
Oaken::Seeds.entry.define_reader(self, ...)
end
end