Module: Sinatra::JS

Defined in:
lib/sinatra/js.rb

Overview

Sinatra::JS

A Sinatra Extension that makes working with JS easy.

Installation

#  Add RubyGems.org (former Gemcutter) to your RubyGems sources 
$  gem sources -a http://rubygems.org

$  (sudo)? gem install sinatra-js

Dependencies

This Gem depends upon the following:

Runtime:

  • sinatra ( >= 1.0.a )
  • sinatra-tags ( >= 0.1.0 )
  • sinatra-outputbuffer ( >= 0.1.0 )
  • sinatra-basics ( >= 0.5.0 )

Optionals:

  • sinatra-settings (>= 0.1.1) # to view default settings in a browser display.
  • sinatra-logger (>= 0.1.0) # to capture error messages in the log file.

Development & Tests:

  • sinatra-tests (>= 0.1.6)
  • rspec (>= 1.3.0 )
  • rack-test (>= 0.5.3)
  • rspec_hpricot_matchers (>= 0.1.0)

Getting Started

To start using Sinatra::JS, just require and register the extension in your App...

require 'sinatra/js'

class YourApp < Sinatra::Base
register(Sinatra::JS)

<snip...>

end

You then get access to 6 useful helper methods:

js() / javascript()

This method serves two purposes:

a) Return a stylesheet tag with the path given.

js('jquery') # => 

<script src="/jquery.js" type="text/javascript" charset="utf-8"></script>

js('/js/jquery-tools.js') # => 

<script src="/js/jquery-tools.js" type="text/javascript" charset="utf-8"></script>

You can even pass an array of files to the method, which then outputs the

Registers these Extensions:

  • Sinatra::Tags
  • Sinatra::Basics::Assets

Default Settings:

  • :js_shared_source_files_dir => set the path to the Shared directory with compliled JS templates. Default is: 'ENV/.alt/js'


540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/sinatra/js.rb', line 540

def self.registered(app)
  app.register(Sinatra::Tags)
  app.register(Sinatra::Assets)
  
  app.helpers Sinatra::JS::Helpers
  
  # set the path to the Shared directory with compliled JS templates
  app.set :js_shared_source_files_dir, File.join(File.expand_path(ENV['HOME']) ,'.alt', 'js')
  
  
  ## add the extension specific options to those inspectable by :settings_inspect method
  #  provided by the Sinatra::Settings extension
  if app.respond_to?(:sinatra_settings_for_inspection)
    %w( js_shared_source_files_dir ).each do |m|
      app.sinatra_settings_for_inspection << m
    end
  end
  
end

.versionObject



273
# File 'lib/sinatra/js.rb', line 273

def self.version; "Sinatra::JS v#{VERSION}"; end

Instance Method Details

#get_all_js_requests(path = '/js', options = {}) ⇒ Object

get_all_js_requests('/javascripts') => expects a app/views/js dir with the .rjs files

Examples

get_all_js_requests()                => catches 'site.com/js/app.js
get_all_js_requests('/javascripts')  => catches 'site.com/javascripts/app.js


512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/sinatra/js.rb', line 512

def get_all_js_requests(path='/js', options = {}) 
  path, options = '/js', path if path.is_a?(Hash)
  
  get("#{path}/*.js",  {} ) do
    begin
      options = { :cache => false, :layout => false }.merge(options)
      content_type 'text/javascript', :charset => 'utf-8'
      rjs("js/#{params[:splat].first}".to_sym, options ).gsub(/\n\r?$/,"")
    rescue Errno::ENOENT
      content_type 'text/html'  # reset the content_type
      pass
    end
  end
end