class BrwyRails::Middleware

Public Class Methods

new(app) click to toggle source
# File lib/brwy_rails/middleware.rb, line 3
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
# File lib/brwy_rails/middleware.rb, line 39
def call(env)
  req = Rack::Request.new env
  if is_target_js? req.path
    tmp_path = get_tmp_path(req.path)
    loop do
      if file_is_readable? tmp_path
        break
      else
        sleep 1
      end
    end
  end
  @app.call(env)
end
file_is_readable?(path) click to toggle source
# File lib/brwy_rails/middleware.rb, line 31
def file_is_readable?(path)
  File.exists?(path) && File.size(path) > 0
end
get_tmp_path(path) click to toggle source
# File lib/brwy_rails/middleware.rb, line 24
def get_tmp_path path
  target_suffix = Rails.application.config.brwy_rails.target_suffix
  m = /assets\/(.*)#{target_suffix.sub(".", "\.")}/.match path
  name = m[1]
  "#{Rails.root.to_s}/#{Rails.application.config.brwy_rails.tmpdir}/#{name}#{target_suffix}.js"
end
in_targets?(path) click to toggle source
# File lib/brwy_rails/middleware.rb, line 17
def in_targets?(path)
  targets = Rails.application.config.brwy_rails.targets
  canditates = targets.map {|t| to_asset_path(t)}
  puts canditates
  canditates.any? {|c| path.start_with?(c)}
end
is_asset_js?(path) click to toggle source
# File lib/brwy_rails/middleware.rb, line 7
def is_asset_js?(path)
  path.start_with?("/assets/") && File.extname(path) == ".js"
end
is_target_js?(path) click to toggle source
# File lib/brwy_rails/middleware.rb, line 35
def is_target_js?(path)
  is_asset_js?(path) && in_targets?(path)
end
to_asset_path(target) click to toggle source
# File lib/brwy_rails/middleware.rb, line 11
def to_asset_path(target)
  target_suffix = Rails.application.config.brwy_rails.target_suffix
  extname = File.extname(target)
  "/assets/" + File.basename(target).sub(extname, "") + target_suffix
end