specs passing

This commit is contained in:
Scott Davis 2011-01-31 17:22:47 -05:00
parent 5385986f88
commit 7c1f0e6cf6
10 changed files with 148 additions and 0 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@ -6,6 +6,7 @@ PATH
GEM GEM
specs: specs:
diff-lcs (1.1.2) diff-lcs (1.1.2)
json (1.4.6)
mocha (0.9.10) mocha (0.9.10)
rake rake
nokogiri (1.4.4) nokogiri (1.4.4)
@ -27,6 +28,7 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
bundler (>= 1.0.0) bundler (>= 1.0.0)
flowplayer! flowplayer!
json
mocha mocha
nokogiri nokogiri
rspec (~> 2.0.0) rspec (~> 2.0.0)

View File

@ -23,5 +23,6 @@ Gem::Specification.new do |s|
s.add_development_dependency "rspec", "~> 2.0.0" s.add_development_dependency "rspec", "~> 2.0.0"
s.add_development_dependency "nokogiri" s.add_development_dependency "nokogiri"
s.add_development_dependency "mocha" s.add_development_dependency "mocha"
s.add_development_dependency "json"
end end

BIN
lib/.DS_Store vendored Normal file

Binary file not shown.

1
lib/flowplayer.rb Normal file
View File

@ -0,0 +1 @@
require 'flowplayer/player'

56
lib/flowplayer/player.rb Normal file
View File

@ -0,0 +1,56 @@
require 'json'
module Flowplayer
class Player
attr_accessor :options, :functions, :dom_id
def initialize(dom_id, &block)
@dom_id = dom_id
@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[
flowplayer("#{dom_id}", #{to_js})
//]]>
</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

View File

@ -0,0 +1,19 @@
module Flowplayer
module Helpers
# flowplayer_for :hubble do |f|
# f.option 'foo'
# f.onLoad do
# 'this.unmute();'
# end
def flowplayer_for(id, swf, &block)
FlowPlayer::Player.new(id, swf, &block).script_tags
end
end
end

View File

@ -0,0 +1,8 @@
module Flowplayer
require 'rails'
class Railtie < ::Rails::Railtie
generators do
require File.join(File.expand_path('../', __FILE__), 'railties', 'generators', 'compass_theme', 'compass_theme_generator.rb')
end
end
end

60
spec/flowplayer_spec.rb Normal file
View File

@ -0,0 +1,60 @@
require 'spec_helper'
describe Flowplayer::Player do
it "should set options from block" do
flow_player = Flowplayer::Player.new('my_video') do |player|
player.fullscreen true
player.logo(:url => nil, :opacity => 0, :fullscreenOnly => true)
player.onLoad do
'this.unmute();'
end
end
flow_player.options[:fullscreen].should == true
flow_player.functions[:onLoad].should == 'function() { this.unmute(); }'
flow_player.options[:logo].should be_an(Hash)
end
it "should set the dom_id to 'my_video'" do
flow_player = Flowplayer::Player.new('my_video') do |player|
player.fullscreen true
end
flow_player.dom_id.should == 'my_video'
end
it "should generate valid options" do
flow_player = Flowplayer::Player.new('my_video') do |player|
player.fullscreen true
player.logo({:opacity => 0, :fullscreenOnly => true})
player.onLoad do
'this.unmute();'
end
end
flow_player.to_js.should include 'fullscreen'
flow_player.to_js.should match /^\{.+\}$/
flow_player.to_js.should include({:opacity => 0, :fullscreenOnly => true}.to_json)
end
it "should create script tags with options" do
flow_player = Flowplayer::Player.new('my_video') do |player|
player.fullscreen true
player.logo(:url => nil, :opacity => 0, :fullscreenOnly => true)
player.onLoad do
'this.unmute();'
end
end
["<script", "</script>", "flowplayer(\"my_video\", #{flow_player.to_js}"].each do |part|
flow_player.script_tags.should include part
end
end
it "should raise exception if passed a setter" do
lambda do
flow_player = Flowplayer::Player.new('my_video') do |player|
player.fullscreen = true
end
end.should raise_error
end
end

1
spec/spec_helper.rb Normal file
View File

@ -0,0 +1 @@
require File.expand_path('../../lib/flowplayer', __FILE__)