Skip to content
This repository
Browse code

Added docs for lib/asset_hat.rb

  • Loading branch information...
commit 72ee298473db32f5030f2f77f40e629c59125658 1 parent a5b4829
Ron DeVera rondevera authored

Showing 1 changed file with 42 additions and 15 deletions. Show diff stats Hide diff stats

  1. +42 15 lib/asset_hat.rb
57 lib/asset_hat.rb
@@ -12,9 +12,10 @@ module AssetHat
12 12 CONFIG_FILEPATH = File.join(RAILS_ROOT, RELATIVE_CONFIG_FILEPATH)
13 13
14 14 class << self
15   - attr_accessor :config, :asset_exists, :html_cache
  15 + attr_accessor :config, :asset_exists, :html_cache #:nodoc:
16 16 end
17 17
  18 + # Nested-hash version of <tt>config/assets.yml</tt>.
18 19 def self.config
19 20 if !cache? || @config.blank?
20 21 @config = YAML.load(File.open(CONFIG_FILEPATH, 'r'))
@@ -22,6 +23,9 @@ def self.config
22 23 @config
23 24 end
24 25
  26 + # Argument: <tt>:css</tt> or <tt>:js</tt>
  27 + #
  28 + # Returns the path to the directory where CSS or JS files are stored.
25 29 def self.assets_dir(type)
26 30 case type.to_sym
27 31 when :css ; STYLESHEETS_DIR
@@ -30,6 +34,17 @@ def self.assets_dir(type)
30 34 end
31 35 end
32 36
  37 + # Returns true if the specified asset exists in the file system:
  38 + #
  39 + # AssetHat.asset_exists?('application', :css)
  40 + # # => <tt>true</tt> if <tt>/public/stylesheets/application.css</tt>
  41 + # exists
  42 + # AssetHat.asset_exists?('some-plugin', :js)
  43 + # # => <tt>true</tt> if <tt>/public/javascripts/some-plugin.js</tt>
  44 + # exists
  45 + #
  46 + # See also <tt>AssetHat::STYLESHEETS_DIR</tt> and
  47 + # <tt>AssetHat::JAVASCRIPTS_DIR</tt>.
33 48 def self.asset_exists?(filename, type)
34 49 # Process arguments
35 50 type = type.to_sym
@@ -48,20 +63,31 @@ def self.asset_exists?(filename, type)
48 63 @asset_exists[type][filename]
49 64 end
50 65
  66 + # Returns +true+ if bundles should be included as single minified files
  67 + # (e.g., in production), or +false+ if bundles should be included as
  68 + # separate, unminified files (e.g., in development). To modify this value,
  69 + # set <tt>config.action_controller.perform_caching = true</tt> in your
  70 + # environment.
51 71 def self.cache? ; ActionController::Base.perform_caching ; end
52 72
  73 + # Returns the expected path for the minified version of an asset:
  74 + #
  75 + # AssetHat.min_filepath('public/stylesheets/bundles/application.css')
  76 + # # => 'public/stylesheets/bundles/application.min.css'
  77 + #
  78 + # See also <tt>AssetHat::CSS.min_filepath</tt> and
  79 + # <tt>AssetHat::JS.min_filepath</tt>.
53 80 def self.min_filepath(filepath, extension)
54 81 filepath.sub(/([^\.]*).#{extension}$/, "\\1.min.#{extension}")
55 82 end
56 83
  84 + # Returns the extension-less names of files in the given bundle:
  85 + #
  86 + # AssetHat.bundle_filenames('application', :css)
  87 + # # => ['reset', 'application', 'clearfix']
  88 + # AssetHat.bundle_filenames('non-existent-file', :css)
  89 + # # => nil
57 90 def self.bundle_filenames(bundle, type)
58   - # Usage:
59   - #
60   - # AssetHat.bundle_filenames('application', :css)
61   - # # => ['reset', 'application', 'clearfix']
62   - # AssetHat.bundle_filenames('non-existent-file', :css)
63   - # # => nil
64   -
65 91 # Process arguments
66 92 unless TYPES.include?(type.to_sym)
67 93 raise "Unknown type \"#{type}\"; should be one of: #{TYPES.join(', ')}."
@@ -71,14 +97,15 @@ def self.bundle_filenames(bundle, type)
71 97 self.config[type.to_s]['bundles'][bundle] rescue nil
72 98 end
73 99
  100 + # Returns the full paths of files in the given bundle:
  101 + #
  102 + # AssetHat.bundle_filenames('application', :css)
  103 + # # => ['/path/to/app/public/stylesheets/reset.css',
  104 + # '/path/to/app/public/stylesheets/application.css',
  105 + # '/path/to/app/public/stylesheets/clearfix.css']
  106 + # AssetHat.bundle_filenames('non-existent-file', :css)
  107 + # # => nil
74 108 def self.bundle_filepaths(bundle, type)
75   - # Usage:
76   - #
77   - # AssetHat.bundle_filenames('application', :css)
78   - # # => ['reset', 'application', 'clearfix']
79   - # AssetHat.bundle_filenames('non-existent-file', :css)
80   - # # => nil
81   -
82 109 # Process arguments
83 110 unless TYPES.include?(type.to_sym)
84 111 raise "Unknown type \"#{type}\"; should be one of: #{TYPES.join(', ')}."

0 comments on commit 72ee298

Please sign in to comment.
Something went wrong with that request. Please try again.