Class: Mack::Assets::Manager

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/mack/assets/assets_mgr.rb

Overview

Like the name suggests, this object will manage assets (i.e. scripts and stylesheets) used in a mack application. Developer will be able to use the manager to create assets groups, and refer them later in the erb file.
There’s a convenient method available, called assets_mgr. Application should use that method to create new bundle, and to access defined bundles.
Example:

# This will create new bundle called foo
assets_mgr.foo do |a|
  a.add_js "foo"
  a.add_css "bar"
end

Note that nested group is not supported.

Instance Method Summary collapse

Constructor Details

#initializeManager

:nodoc:



87
88
89
# File 'lib/mack/assets/assets_mgr.rb', line 87

def initialize # :nodoc:
  @bundles = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object

:nodoc:



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/mack/assets/assets_mgr.rb', line 98

def method_missing(sym, *args, &block) # :nodoc:
  if @bundles.has_key?(sym.to_s)
    bundle = @bundles[sym.to_s]
  else
    bundle = Mack::Assets::Bundle.new(sym.to_s)
    @bundles[sym.to_s] = bundle
  end
  if block_given?
    yield bundle
  end
  return bundle
end

Instance Method Details

#assets(asset_type, group) ⇒ Object



139
140
141
142
# File 'lib/mack/assets/assets_mgr.rb', line 139

def assets(asset_type, group)
  return nil if !@bundles.has_key?(group.to_s)
  return @bundles[group.to_s].assets(asset_type)
end

#groupsObject

return all groups defined for both javascript and stylesheets



114
115
116
# File 'lib/mack/assets/assets_mgr.rb', line 114

def groups
  @bundles.keys
end

#groups_by_asset_type(type) ⇒ Object

Return all groups defined by specified asset type

Params:

type -- asset type (can be either :javascript or :stylesheet)


123
124
125
126
127
128
129
# File 'lib/mack/assets/assets_mgr.rb', line 123

def groups_by_asset_type(type)
  arr = []
  groups.each do |group|
    arr << group if !@bundles[group.to_s].assets(type.to_sym).empty?
  end
  return arr
end

#has_group?(group, asset_type = nil) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
# File 'lib/mack/assets/assets_mgr.rb', line 131

def has_group?(group, asset_type = nil)
  if asset_type
    return groups_by_asset_type(asset_type).include?(group.to_s)
  else
    return groups.include?(group)
  end
end

#javascripts(group) ⇒ Object



144
145
146
# File 'lib/mack/assets/assets_mgr.rb', line 144

def javascripts(group)
  return assets(:javascripts, group)
end

#reset!Object

Clear the defined assets



94
95
96
# File 'lib/mack/assets/assets_mgr.rb', line 94

def reset!
  @bundles.clear
end

#stylesheets(group) ⇒ Object



148
149
150
# File 'lib/mack/assets/assets_mgr.rb', line 148

def stylesheets(group)
  return assets(:stylesheets, group)
end