Class: Bunto::Assets::Env

Inherits:
Sprockets::Environment
  • Object
show all
Defined in:
lib/bunto/assets/env.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, bunto = nil) ⇒ Env




47
48
49
50
51
52
53
54
55
56
# File 'lib/bunto/assets/env.rb', line 47

def initialize(path, bunto = nil)
  bunto, path = path, nil if path.is_a?(Bunto::Site)
  @bunto = bunto
  @used = Set.new

  path ? super(path) : super()
  Hook.trigger :env, :init do |hook|
    hook.arity > 0 || 0 > hook.arity ? hook.call(self) : instance_eval(&hook)
  end
end

Class Attribute Details

.pastObject

Returns the value of attribute past.



13
14
15
# File 'lib/bunto/assets/env.rb', line 13

def past
  @past
end

Instance Attribute Details

#buntoObject (readonly)

Returns the value of attribute bunto.



10
11
12
# File 'lib/bunto/assets/env.rb', line 10

def bunto
  @bunto
end

#usedObject (readonly)

Returns the value of attribute used.



10
11
12
# File 'lib/bunto/assets/env.rb', line 10

def used
  @used
end

Class Method Details

.context_patchesObject



191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/bunto/assets/env.rb', line 191

def context_patches
  Proc.new do
    alias _old_asset_path asset_path
    def asset_path(asset, _ = {})
      rtn = _old_asset_path asset

      return unless rtn
      path = environment.find_asset(resolve(asset))
      environment.parent.used.add(path)
      rtn
    end
  end
end

.liquid_proxiesObject



14
15
16
# File 'lib/bunto/assets/env.rb', line 14

def liquid_proxies
  Liquid::Tag::Proxies
end

Instance Method Details

#all_assetsObject


Merged form of ‘#extra_assets`




77
78
79
# File 'lib/bunto/assets/env.rb', line 77

def all_assets
  Set.new(@used).merge extra_assets
end

#all_unparsed_assetsObject




29
30
31
32
33
# File 'lib/bunto/assets/env.rb', line 29

def all_unparsed_assets
  @unparsed_assets ||= logical_paths.select do |(_, val)|
    val.start_with?(bunto.in_source_dir)
  end
end

#asset_configObject




128
129
130
# File 'lib/bunto/assets/env.rb', line 128

def asset_config
  bunto.config["assets"] ||= {}
end

#baseurlObject




102
103
104
105
106
107
108
109
110
111
# File 'lib/bunto/assets/env.rb', line 102

def baseurl
  ary = []

  ary << bunto.config["baseurl"] unless cdn? && asset_config["skip_baseurl_with_cdn"]
  ary <<  asset_config[ "prefix"] unless cdn? && asset_config[ "skip_prefix_with_cdn"]

  File.join(*ary.delete_if do |val|
    val.nil? || val.empty?
  end)
end

#cachedObject




158
159
160
# File 'lib/bunto/assets/env.rb', line 158

def cached
  Cached.new(self)
end

#cdn?Boolean


Returns:

  • (Boolean)


95
96
97
98
# File 'lib/bunto/assets/env.rb', line 95

def cdn?
  !dev? && asset_config.key?("cdn") && \
    asset_config["cdn"]
end

#compress?(what) ⇒ Boolean


Returns:

  • (Boolean)


121
122
123
124
# File 'lib/bunto/assets/env.rb', line 121

def compress?(what)
  !!asset_config["compress"] \
    .fetch(what, false)
end

#dev?Boolean


Returns:

  • (Boolean)


115
116
117
# File 'lib/bunto/assets/env.rb', line 115

def dev?
  %W(development test).include?(Bunto.env)
end

#digest?Boolean


Returns:

  • (Boolean)


134
135
136
# File 'lib/bunto/assets/env.rb', line 134

def digest?
  !!asset_config["digest"]
end

#excludesObject




21
22
23
24
25
# File 'lib/bunto/assets/env.rb', line 21

def excludes
  excludes = Set.new
  excludes << strip_path(in_cache_dir)
  excludes
end

#extra_assetsObject


Assets you tell us you want to always compile, even if you do not use them. Just like Rails this is probably normally used.




86
87
88
89
90
91
# File 'lib/bunto/assets/env.rb', line 86

def extra_assets
  assets = asset_config["assets"] ||= []
  each_logical_path(*assets).map do |v|
    find_asset v
  end
end

#in_cache_dir(*paths) ⇒ Object


Make sure a path falls withint our cache dir.




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

def in_cache_dir(*paths)
  cache_dir = asset_config["cache"] || ".asset-cache"
  bunto.in_source_dir(cache_dir, *paths)
end

#liquid_proxiesObject




60
61
62
# File 'lib/bunto/assets/env.rb', line 60

def liquid_proxies
  self.class.liquid_proxies
end

#prefix_path(path = nil) ⇒ Object


Prefix path prefixes the path with the baseurl and the cdn if it exists and is in the right mode to use it. Otherwise it will only use the baseurl and asset prefix. All of these can be adjusted.




144
145
146
147
148
149
150
151
152
153
154
# File 'lib/bunto/assets/env.rb', line 144

def prefix_path(path = nil)
  cdn = asset_config["cdn"]
  base_url = baseurl

  path_ = []
  path_ << base_url unless base_url.empty?
  path_ << path unless path.nil?

  url = cdn && cdn?? File.join(cdn, *path_) : File.join(*path_)
  url.chomp("/")
end

#to_liquid_payloadObject




37
38
39
40
41
42
43
# File 'lib/bunto/assets/env.rb', line 37

def to_liquid_payload
  bunto.sprockets.all_unparsed_assets.each_with_object({}) do |(key, val), hash|
    hash[key] = Bunto::Assets::Liquid::Drop.new(
      val, bunto
    )
  end
end

#write_allObject




164
165
166
167
168
169
170
171
# File 'lib/bunto/assets/env.rb', line 164

def write_all
  past = self.class.past ||= Set.new
  (@used.any?? all_assets : past).each do |obj|
    if past.add(obj) && path = as_path(obj)
      obj.write_to path
    end
  end
end