Class: AssetDB::Resolver::PackageCollection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/asset_db/resolver.rb

Overview


lightweight immutable union fa

Instance Method Summary collapse

Constructor Details

#initialize(database, pkgs) ⇒ PackageCollection

Returns a new instance of PackageCollection.



89
90
91
92
93
# File 'lib/asset_db/resolver.rb', line 89

def initialize(database, pkgs)
  @database = database
  @packages = Array(pkgs).uniq
  @cache    = {} # {[type, key] ⇒ [Asset]}

end

Instance Method Details

#+(other) ⇒ Object



95
96
97
# File 'lib/asset_db/resolver.rb', line 95

def +(other)
  @database.unify(self, other)
end

#eachObject



119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/asset_db/resolver.rb', line 119

def each
  return to_enum(:each) unless block_given?

  seen = Set.new
  @database.asset_types.each do |type|
    each_asset(type).each do |asset|
      next if seen.include?(asset.id)   # de-dupe across types just in case

      seen << asset.id
      yield asset
    end
  end
  self
end

#each_asset(type = nil, &block) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/asset_db/resolver.rb', line 99

def each_asset(type = nil, &block)
  return @database.asset_types.each { |t| each_asset(t, &block) } if type.nil?

  type = type.to_sym
  key  = [type, @packages.map(&:key?).sort].hash
  @cache[key] ||= begin
    seen = Set.new
    arr  = []
    @packages.each do |pkg|
      pkg.resolved_assets(type).each do |asset|
        next if seen.include?(asset.id)
        seen << asset.id
        arr  << asset
      end
    end
    arr.freeze
  end
  block ? @cache[key].each(&block) : @cache[key]
end