Skip to content

Commit

Permalink
Try recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
gmcgibbon committed Nov 8, 2024
1 parent f83489d commit 7478b1c
Showing 1 changed file with 25 additions and 35 deletions.
60 changes: 25 additions & 35 deletions railties/lib/rails/initializable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ def initialize(name, context, options, &block)
@name, @context, @options, @block = name, context, options, block
end

def ==(other)
@name == other.name
end

def before
@options[:before]
end
Expand All @@ -28,6 +24,10 @@ def after
@options[:after]
end

def whenever
!@options[:before] && !@options[:after]
end

def belongs_to?(group)
@options[:group] == group || @options[:group] == :all
end
Expand All @@ -38,6 +38,7 @@ def run(*args)

def bind(context)
return self if @context

Initializer.new(@name, context, @options, &block)
end

Expand All @@ -50,41 +51,34 @@ class Collection
include Enumerable

def initialize(initializers = [])
@sorted = nil
@last = nil
@whenever = []
@before = Hash.new { |hash, key| hash[key] = [] }
@after = Hash.new { |hash, key| hash[key] = [] }
@before = Hash.new { |hash, key| hash[key] = Set.new }
@after = Hash.new { |hash, key| hash[key] = Set.new }
@initializers = Hash.new { |hash, key| hash[key] = [] }
initializers.each(&method(:<<))
end

def each(&block)
return to_enum unless block

yielder = proc do |initializer|
@before[initializer.name].each { |initializer| yielder.(initializer) }
puts initializer.name
block.call(initializer)
end
def expand(name)
[*@before[name].map(&method(:expand)), name, *@after[name].map(&method(:expand))].uniq
end

initializers = @whenever.dup
while (initializer = initializers.pop)
yielder.(initializer)
initializers.concat(@after[initializer.name])
end
def each(&block)
@sorted ||= @whenever.map(&method(:expand)).flatten.uniq.flat_map { |name| @initializers[name] }
@sorted.each(&block)
end

def <<(initializer)
@before[initializer.before].prepend(initializer) if initializer.before
@after[initializer.after].prepend(initializer) if initializer.after
@whenever.prepend(initializer) if !initializer.before && !initializer.after
@before[initializer.before] << initializer.name if initializer.before
@after[initializer.after] << initializer.name if initializer.after
@whenever << initializer.name if initializer.whenever
@initializers[initializer.name] << initializer
@last = initializer
end

def +(other)
dup.tap do |new_collection|
new_collection.whenever.prepend(*other.whenever)
other.before.each { |name, initializers| new_collection.before[name].concat(initializers) }
other.after.each { |name, initializers| new_collection.after[name].concat(initializers) }
end
Collection.new(values + other.values)
end

def before?(name)
Expand All @@ -93,20 +87,16 @@ def before?(name)


def with_binding(context)
dup.tap do |new_collection|
new_collection.whenever.map! { |initializer| initializer.bind(context) }
new_collection.before.each_value { |initializers| initializers.map! { |initializer| initializer.bind(context) } }
new_collection.after.each_value { |initializers| initializers.map! { |initializer| initializer.bind(context) } }
end
Collection.new(values.map! { |initializer| initializer.bind(context) })
end

attr_reader(:last)

protected

attr_reader(:whenever)
attr_reader(:before)
attr_reader(:after)
def values
@initializers.values.flatten
end
end

def run_initializers(group = :default, *args)
Expand Down

0 comments on commit 7478b1c

Please sign in to comment.