flowplayer-gem/lib/flowplayer/player.rb

56 lines
1.3 KiB
Ruby
Raw Normal View History

2011-01-31 22:22:47 +00:00
require 'json'
module Flowplayer
class Player
2011-02-01 21:03:28 +00:00
attr_accessor :options, :functions, :dom_id, :swf
def initialize(dom_id, swf, &block)
@dom_id, @swf = dom_id, swf
2011-01-31 22:22:47 +00:00
@options = {}
@functions = {}
block.call(self)
self
end
def to_js
json = options_to_javascript
json += functions_to_javascript
"{#{json.join(', ')}}"
end
def script_tags
<<-EOS
<script type='text/javascript'>
//<![CDATA[
2011-02-01 21:03:28 +00:00
flowplayer("#{dom_id}", "#{swf}", #{to_js});
2011-01-31 22:22:47 +00:00
//]]>
</script>
EOS
end
private
def functions_to_javascript
functions.map {|option, function| "\"#{option}\":#{function}"}
end
def options_to_javascript
options.map do |option, value|
if value.is_a?(String)
"\"#{option}\":\"#{value.to_json}\""
else
"\"#{option}\":#{value.to_json}"
end
end
end
def method_missing(method, *args, &block)
raise "Setters are not supported use method('whatever') to set configs" if /\=$/.match(method.to_s)
if block.nil?
options[method] = args.first
else
functions[method] = "function() { #{block.call.gsub(/\;$/, '')}; }"
end
return method
end
end
end