Class: Sapling::JavascriptGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/sapling/generators/javascript_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sapling) ⇒ JavascriptGenerator

Returns a new instance of JavascriptGenerator.



6
7
8
# File 'lib/sapling/generators/javascript_generator.rb', line 6

def initialize(sapling)
  @sapling=sapling
end

Instance Attribute Details

#saplingObject

Returns the value of attribute sapling.



4
5
6
# File 'lib/sapling/generators/javascript_generator.rb', line 4

def sapling
  @sapling
end

Instance Method Details

#css_container_class(feature) ⇒ Object

Use these classes on the container elements of your features



15
16
17
# File 'lib/sapling/generators/javascript_generator.rb', line 15

def css_container_class(feature)
  "#{prefix}_#{feature.to_s}"
end

#css_toggle_class(feature, on) ⇒ Object

Put these on the html element to turn on/off features



20
21
22
# File 'lib/sapling/generators/javascript_generator.rb', line 20

def css_toggle_class(feature, on)
  "#{css_container_class(feature)}_#{on ? 'on' : 'off'}"
end

#generate(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sapling/generators/javascript_generator.rb', line 31

def generate(options={})
  features = Set.new @sapling.features
  active_features = Set.new @sapling.active_features options
  inactive_features = features - active_features

  
  "(function() {\nif (window.MooTools) {\n  html = $$('html')[0];\n  \#{inactive_features.map{|f| mootools_for_feature(f,false) }.join}\n  \#{active_features.map{|f| mootools_for_feature(f,true) }.join}    \n}\n})();\n"
end

#mootools_for_feature(feature, on) ⇒ Object

see Sapling::API::Client for options



27
28
29
# File 'lib/sapling/generators/javascript_generator.rb', line 27

def mootools_for_feature(feature, on)
  "html.removeClass('" + css_toggle_class(feature,!on) + "');html.addClass('" + css_toggle_class(feature,on) + "');"
end

#prefixObject



10
11
12
# File 'lib/sapling/generators/javascript_generator.rb', line 10

def prefix
  "sapling_feature"
end