class JsShuffle::Shuffler

Shuffles a js-program or RKelly AST RDoc::Markup

Public Class Methods

new( hash={ use: [:variable_renaming, :parameter_renaming] } ) click to toggle source

Instantiate a new Shuffler Parameters:

use

A Symbol, Proc or a Class inheriting from JsShuffle::Methods::Method or an Array mixing any of these types. Symbols are matched to JsShuffle's native Methods and should be lowercased and underscored (like the filenames of the corresponding Method) Procs are called three times and receive the pass (one of :preprocess, :process or postprocess), the AST and the Shuffler instance. In the last pass the js string is passed instead of the AST and the Proc is expected to return the modified string

# File lib/jsshuffle/shuffler.rb, line 12
def initialize( hash={ use: [:variable_renaming, :parameter_renaming] } )
    @methods = hash[:use]
    @methods = [ @methods ] if @methods.is_a? Symbol

    @parser = RKelly::Parser.new

    @methods.collect! do |m|
        next (JsShuffle::Methods.methods[m]).new if m.is_a? Symbol 
        next m.new if m.is_a? JsShuffle::Methods::Method
        m
    end

    @defaults = {}
    @methods.each { |m| @defaults.merge! m.default_config if m.is_a? JsShuffle::Methods::Method }
end

Public Instance Methods

compress( source ) click to toggle source

Shuffle a javascript program string and return the result

Used to make Shuffler available as a Rails JS Preprocessor

Parameters:

source

The js source string

# File lib/jsshuffle/shuffler.rb, line 35
def compress( source )
    shuffle js: source
end
random_new_name() click to toggle source

Generates a random new symbol name

# File lib/jsshuffle/shuffler.rb, line 82
def random_new_name
    begin
        new_name = ('a'..'z').to_a.shuffle[0,8].join
    end while @new_names.include? new_name
    new_name
end
shuffle( hash={} ) click to toggle source

Shuffle a js-string or a RKelly AST

Parameters:

options

an option hash containing either

js

the javascript source code as a string or

ast

the parsed javascript source as RKelly AST

and optionally the following options:

output

the output format, :string or :ast

all other options are passed on to the Methods supplied during initialization

Returns either a String or RKelly AST, matching the input format or the output value

# File lib/jsshuffle/shuffler.rb, line 52
def shuffle( hash={} )
    hash = { js: hash } if hash.is_a? String
    options = @defaults.merge(hash) 
    @new_names = []
    ast = hash[:ast] || @parser.parse( hash[:js] )

    @methods.each { |m| m.configure options if m.is_a? JsShuffle::Methods::Method }

    [:preprocess, :process].each do |pass|
        @methods.each do |m|
            next m.send( pass, ast, self ) if m.is_a? JsShuffle::Methods::Method and m.respond_to? pass
            m.call( pass, ast, self ) if m.respond_to? :call
        end
    end
    js = ast.to_ecma
    @methods.each do |m|
        next (js = m.postprocess( ast, self )) if m.is_a? JsShuffle::Methods::Method and m.respond_to? :postprocess
        js = m.call( :postprocess, js, self ) if m.respond_to? :call
    end

    if hash.has_key? :output
        return js if hash[:output] == :string
        return @parser.parse( js )
    end

    return @parser.parse( js ) if hash[:ast]
    js
end