Module: Blade::Assets

Extended by:
Assets
Included in:
Assets
Defined in:
lib/blade/assets.rb

Defined Under Namespace

Classes: Builder

Instance Method Summary collapse

Instance Method Details

#adapter_load_pathsObject



68
69
70
71
# File 'lib/blade/assets.rb', line 68

def adapter_load_paths
  gem_name = "blade-#{Blade.config.framework}_adapter"
  [ gem_pathname(gem_name).join("assets") ]
end

#blade_load_pathsObject



52
53
54
# File 'lib/blade/assets.rb', line 52

def blade_load_paths
  [ Blade.root_path.join("assets") ]
end

#build(name = :user) ⇒ Object



40
41
42
43
44
# File 'lib/blade/assets.rb', line 40

def build(name = :user)
  if Blade.config.build
    Builder.new(environment(name)).build
  end
end

#environment(name = :user, context_name = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/blade/assets.rb', line 10

def environment(name = :user, context_name = nil)
  cache_name = [name, context_name].compact.map(&:to_s).uniq.join("-")

  @environments[cache_name] ||= Sprockets::Environment.new do |env|
    env.cache = Sprockets::Cache::FileStore.new(Blade.tmp_path.join(cache_name))

    send("#{name}_load_paths").each do |path|
      env.append_path(path)
    end

    env.context_class.class_eval do
      delegate :logical_paths, to: Blade::Assets

      define_method(:environment) { env }
      define_method(:context_name) { name }

      def with_asset(path, env_name)
        if asset = Blade::Assets.environment(env_name, context_name)[path]
          depend_on(asset.pathname)
          yield(asset)
        end
      end

      def render_asset(path, env_name = context_name)
        with_asset(path, env_name) { |asset| asset.to_s }
      end
    end
  end
end

#logical_paths(type = nil) ⇒ Object



46
47
48
49
50
# File 'lib/blade/assets.rb', line 46

def logical_paths(type = nil)
  paths = Blade.config.logical_paths
  paths.select! { |path| File.extname(path) == ".#{type}" } if type
  paths
end

#user_load_pathsObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/blade/assets.rb', line 56

def user_load_paths
  Blade.config.load_paths.flat_map do |load_path|
    if load_path.is_a?(Hash)
      load_path.flat_map do |gem_name, paths|
        Array(paths).map{ |path| gem_pathname(gem_name).join(path) }
      end
    else
      Pathname.new(load_path)
    end
  end
end

#watch_logical_pathsObject



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/blade/assets.rb', line 73

def watch_logical_paths
  @mtimes = get_mtimes

  EM.add_periodic_timer(1) do
    mtimes = get_mtimes
    unless mtimes == @mtimes
      @mtimes = mtimes
      Blade.publish("/assets", changed: @mtimes)
    end
  end
end