Module: Sinatra::SwfDitty
- Defined in:
- lib/sinatra/swf_ditty.rb
Instance Method Summary collapse
- #content_tag(name, content, options = {}) ⇒ Object
-
#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 k:v pairs, delimited by commas Wrap in quotes unless numeric or flashvars hash.
- #render_swf(swf, *args) ⇒ Object
Instance Method Details
#content_tag(name, content, options = {}) ⇒ Object
60 61 62 63 |
# File 'lib/sinatra/swf_ditty.rb', line 60 def content_tag(name,content,={}) = .map{ |k,v| "#{k}='#{v}'" }.join(" ") "<#{name} #{}>#{content}</#{name}>" end |
#filename_to_dom_id(str) ⇒ Object
Convert a string to dom-friendly format. Adapted from permalink_fu
40 41 42 43 44 45 46 47 |
# File 'lib/sinatra/swf_ditty.rb', line 40 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 k:v pairs, delimited by commas Wrap in quotes unless numeric or flashvars hash
51 52 53 54 55 56 |
# File 'lib/sinatra/swf_ditty.rb', line 51 def hash_to_key_value_string(hash) hash.each_pair.map do |k,v| v = "'#{v}'" unless k.to_s=='flashvars' || !v.to_s.match(/^[0-9]*\.[0-9]+|[0-9]+$/).nil? "#{k}:#{v}" end.sort.join(", ") end |
#render_swf(swf, *args) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/sinatra/swf_ditty.rb', line 6 def render_swf(swf, *args) = { :swf => swf, :dom_id => filename_to_dom_id(swf), :name => filename_to_dom_id(swf), :width => "100%", :height => "100%", :wmode => "opaque", :allowScriptAccess => "sameDomain", :create_dom_container => true, }.merge(args.) out = [] # 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 out << content_tag(:div, "", :id => dom_id) if .delete(:create_dom_container) # 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, "$(document).ready(function(){$('##{dom_id}').flash({#{}});});", :type => "text/javascript", :charset => "utf-8") out.reverse.join("\n") end |