Module: SwfTrain
- Defined in:
- lib/swf_train.rb,
lib/swf_train/version.rb
Constant Summary collapse
- VERSION =
"1.0.0"
Instance Method Summary collapse
-
#filename_to_dom_id(str) ⇒ Object
Convert a string to dom-friendly format.
-
#hash_to_key_value_string(hash) ⇒ Object
Convert a hash to a string of key:value pairs, delimited by commas Wrap in quotes unless numeric or flashvars hash.
-
#swf(swf_file, options = {}) ⇒ Object
Outputs javscript to embed your SWF.
Instance Method Details
#filename_to_dom_id(str) ⇒ Object
Convert a string to dom-friendly format. Adapted from permalink_fu
63 64 65 66 67 68 69 70 |
# File 'lib/swf_train.rb', line 63 def filename_to_dom_id(str) result = str.split("/").last.gsub(".", "_") # e.g. 'foo.swf' -> 'foo_swf' result.gsub!(/[^\x00-\x7F]+/, '') # Remove anything non-ASCII entirely (e.g. diacritics). result.gsub!(/[^\w \-]+/i, '') # Remove unwanted chars. result.gsub!(/[ \-]+/i, '-') # No more than one of the separator in a row. result.gsub!(/^\-|\-$/i, '') # Remove leading/trailing separator. result.downcase end |
#hash_to_key_value_string(hash) ⇒ Object
Convert a hash to a string of key:value pairs, delimited by commas Wrap in quotes unless numeric or flashvars hash
74 75 76 77 78 79 80 81 |
# File 'lib/swf_train.rb', line 74 def hash_to_key_value_string(hash) pairs = [] hash.each_pair do |k,v| v = "\"#{v}\"" unless k.to_s=='flashvars' || !v.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/).nil? pairs << "#{k}:#{v}" end pairs.sort.join(", ").html_safe end |
#swf(swf_file, options = {}) ⇒ Object
Outputs javscript to embed your SWF. The only required argument is the swf_file path, relative to the public directory. swf_file can also of course be a full URL.
swf 'foo.swf'
swf 'foo.swf', :height => 50, :flashvars => {:a => 1, :b => 'two'}
swf 'foo.swf', :use_headjs => true # If you're using head.js (http://headjs.com)
By default, a DOM container is generated, but if you don’t want that just pass in your custom ‘dom_id` and set `create_dom_container` to false
swf 'swf/foo.swf', :dom_id => 'dombo', :create_dom_container => false)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/swf_train.rb', line 14 def swf(swf_file, ={}) = { :swf => swf_file, :dom_id => filename_to_dom_id(swf_file), :name => filename_to_dom_id(swf_file), :width => "100%", :height => "100%", :wmode => "opaque", :allowScriptAccess => "sameDomain", :create_dom_container => true, }.merge!() out = [] # Yank use_headjs boolean out of the options hash # See http://headjs.com/ use_headjs = .delete(:use_headjs) ready_wrapper = use_headjs ? "head" : "$(document)" # Yank dom_id out of the options hash dom_id = .delete(:dom_id) # Yank create_dom_container option out of the hash, if it exists # Create DOM container if option is set to something other than false or nil if .delete(:create_dom_container) # Set the div's dimensions right away so the page doesn't jolt into # the proper dimensions when the swf loads. w = [:width].to_s h = [:height].to_s w += "px" unless w.include? "%" h += "px" unless h.include? "%" style = "width:#{w};height:#{h}" out << content_tag(:div, "", :id => dom_id, :style => style) end # Turn flashvars hash into a flashvars-style formatted string [:flashvars] = "{" + hash_to_key_value_string([:flashvars]) + "}" if [:flashvars] # Format options hash (excluding flashvars) into a key:value string.. = hash_to_key_value_string() # Spit it out! out << content_tag(:script, "#{ready_wrapper}.ready(function(){$('##{dom_id}').flash({#{}});});".html_safe, :type => "text/javascript", :charset => "utf-8") out.reverse.join("\n").html_safe end |