Class: Gluey::Workshop

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root, tmp_dir = 'tmp/gluey') ⇒ Workshop

Returns a new instance of Workshop.



12
13
14
15
16
17
18
# File 'lib/gluey/workshop.rb', line 12

def initialize(root, tmp_dir='tmp/gluey')
  @root_path = root
  @tmp_path = "#{root}/#{tmp_dir}"
  Dir.mkdir @tmp_path unless Dir.exists? @tmp_path
  @materials = {}
  @cache = {}
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



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

def cache
  @cache
end

#materialsObject (readonly)

Returns the value of attribute materials.



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

def materials
  @materials
end

#root_pathObject (readonly)

Returns the value of attribute root_path.



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

def root_path
  @root_path
end

#tmp_pathObject (readonly)

Returns the value of attribute tmp_path.



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

def tmp_path
  @tmp_path
end

Instance Method Details

#fetch_file(material, path) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gluey/workshop.rb', line 26

def fetch_file(material, path)
  # check cache
  cache_key = "lump:#{material}:#{path}"
  file, dependencies = @cache[cache_key]
  if file && File.exists?(file) && !dependencies.any?{|d| d.changed?}
    return file
  end

  # prepare for processing
  material = @materials[material.to_sym]
  base_file = material.find_base_file path
  file = "#{@tmp_path}/#{path}.#{material.asset}"
  dependencies = [::Gluey::Dependencies::SingleFile.new(base_file).actualize]
  # process
  glue = material.glue.new self, material
  FileUtils.mkdir_p file[0..(file.rindex('/')-1)]
  File.write file, glue.process(base_file, dependencies)

  # save and return
  @cache[cache_key] = [file, dependencies]
  file
end

#real_path(material, path) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/gluey/workshop.rb', line 49

def real_path(material, path)
  material = @materials[material.to_sym]
  file = material.find_base_file path
  unless material.is_listed? path, file
    raise ::Gluey::ItemNotListed.new("#{material.to_s} doesn't have enlisted item #{path} (file=#{file}).")
  end
  "#{path}.#{File.mtime(file).to_i}.#{material.asset}"
end

#register_material(name, glue = ::Gluey::Glues::Base, &block) ⇒ Object



20
21
22
23
24
# File 'lib/gluey/workshop.rb', line 20

def register_material(name, glue=::Gluey::Glues::Base, &block)
  name = name.to_sym
  material = ::Gluey::Material.new name, glue, self, &block
  @materials[name] = material
end

#try_real_path(path) ⇒ Object



58
59
60
61
62
# File 'lib/gluey/workshop.rb', line 58

def try_real_path(path)
  path.match /^(.+)\.\d+\.(\w+)$/ do |m|
    yield m[1], m[2]
  end
end