Class: JsShuffle::Shuffler

Inherits:
Object
  • Object
show all
Defined in:
lib/jsshuffle/shuffler.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(hash = { use: [:variable_renaming, :parameter_renaming] }) ⇒ Shuffler

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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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

Instance Method Details

#compress(source) ⇒ Object

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



35
36
37
# File 'lib/jsshuffle/shuffler.rb', line 35

def compress( source )
    shuffle js: source
end

#random_new_nameObject

Generates a random new symbol name



82
83
84
85
86
87
# 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 = {}) ⇒ Object

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# 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