Class: Gluey::Warehouse

Inherits:
Object
  • Object
show all
Defined in:
lib/gluey/warehouse.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, listing_file = 'assets/gluey_listing.json') ⇒ Warehouse

Returns a new instance of Warehouse.



9
10
11
12
# File 'lib/gluey/warehouse.rb', line 9

def initialize(root, listing_file='assets/gluey_listing.json')
  @listing_file = "#{root}/#{listing_file}"
  read_listing
end

Instance Attribute Details

#assetsObject (readonly)

Returns the value of attribute assets.



7
8
9
# File 'lib/gluey/warehouse.rb', line 7

def assets
  @assets
end

Instance Method Details

#read_listingObject



28
29
30
31
32
33
34
35
36
# File 'lib/gluey/warehouse.rb', line 28

def read_listing
  assets = if File.exists? @listing_file
             JSON.parse File.read(@listing_file) rescue {}
           else
             {}
           end
  raise "corrupted listing file at #{@listing_file}" unless assets.is_a? Hash
  @assets = assets.keys.inject({}){|h, asset_type| h[asset_type.to_sym] = assets[asset_type]; h }
end

#real_path(asset_type, path) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/gluey/warehouse.rb', line 14

def real_path(asset_type, path)
  listing = @assets[asset_type]
  unless listing
    raise ::Gluey::ItemNotListed.new("Asset type #{asset_type} is not defined! (listing file problem?)")
  end

  real_path = listing[path]
  unless real_path
    raise ::Gluey::ItemNotListed.new("Unknown asset: #{path}, type=#{asset_type}! (listing file problem?)")
  end

  real_path
end

#write_listing(workshop) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gluey/warehouse.rb', line 38

def write_listing(workshop)
  @assets = workshop.materials.values.inject({}) do |listing, material|
    list = material.list_all_items.inject({}) do |h, path|
      h[path] = workshop.real_path material.name, path
      h
    end
    listing[material.name.to_sym] = list
    listing
  end
  File.write @listing_file, JSON.pretty_generate(@assets)
end