Skip to content

Commit

Permalink
Refactor results a bit (#49)
Browse files Browse the repository at this point in the history
* Replace singleton with a class method

* See what it looks like if we extract this into Result

* Let's use an instance method instead

* We still need access to the result outside Seeds
  • Loading branch information
kaspth authored Sep 13, 2023
1 parent bbf2e42 commit 4b2e203
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions lib/oaken.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def insert(reader = nil, **attributes)
private
def add_reader(name, id)
location = caller_locations(2, 10).find { _1.path.match?(/(db|test)\/seeds/) }
Result.instance.run(location.path).add_reader @key, name, id, location
Seeds.result.run(location.path).add_reader @key, name, id, location
instance_eval "def #{name}; find #{id}; end", location.path, location.lineno
end
end
Expand All @@ -66,36 +66,39 @@ def self.register(type, key = Oaken.inflector.tableize(type.name))
define_method(key) { stored }
end

def self.load_from(directory)
result = Result.instance
singleton_class.attr_reader :result

Pathname.glob("#{directory}{,/**/*}.rb").sort.each do |path|
path = Path.new(self, path)
run = result.run(path)
def self.load_from(directory)
@result = Result.new(directory)
@result.process do |run, path|
if run.processed? path
run.replay self
else
path.process
end

result << path
end

result.write
end
end

require "singleton"
class Result
include Singleton

def initialize
def initialize(directory)
@directory = directory
@path = Pathname.new("./tmp/oaken-result.yml")
@runs = @path.exist? ? YAML.load(@path.read) : {}
@runs.transform_values! { Run.new(**_1) }
@runs.default_proc = ->(h, k) { h[k] = Run.new(path: k) }
end

def process
Pathname.glob("#{@directory}{,/**/*}.rb").sort.each do |path|
path = Oaken::Path.new(Oaken::Seeds, path)
yield run(path), path
self << path
end

write
end

def run(path)
@runs[path.to_s]
end
Expand Down

0 comments on commit 4b2e203

Please sign in to comment.