lib/gamefic/query/external.rb in gamefic-2.2.3 vs lib/gamefic/query/external.rb in gamefic-2.3.0
- old
+ new
@@ -1,18 +1,39 @@
module Gamefic
module Query
class External < Base
- def initialize objects, *args
+ # @param container [Plot, Subplot, Array]
+ def initialize container, *args
super(*args)
- @objects = objects
+ @container = container
end
def context_from subject
- @objects
+ Set.new
+ .merge(container_entities)
+ .merge(container_subplots_for(@container, subject))
+ .to_a
end
- def accept?(entity)
- @objects.include?(entity) && super(entity)
+ private
+
+ # @return [Array<Entity>]
+ def container_entities
+ if @container.is_a?(World::Entities)
+ @container.entities
+ elsif @container.is_a?(Enumerable)
+ @container
+ else
+ raise ArgumentError, "Unable to derive entities from #{@container}"
+ end
+ end
+
+ # @return [Array<Entity>]
+ def container_subplots_for container, subject
+ return [] unless container.is_a?(Plot::Host)
+ container.subplots_featuring(subject).flat_map do |subplot|
+ subplot.entities + container_subplots_for(subplot, subject)
+ end
end
end
end
end