Class: SwfFu::Generator

Inherits:
Object
  • Object
show all
Defined in:
app/models/swf_fu/generator.rb

Overview

:nodoc:

Constant Summary collapse

VALID_MODES =
[:static, :dynamic]

Instance Method Summary collapse

Constructor Details

#initialize(source, options, view) ⇒ Generator

Returns a new instance of Generator.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/swf_fu/generator.rb', line 17

def initialize(source, options, view)
  @view = view
  @source = view.swf_path(source)
  options = ActionView::Base.swf_default_options.merge(options)
  options.each do |key, value|
    options[key] = value.call(source) if value.respond_to?(:call)
  end
  [:html_options, :parameters, :flashvars].each do |k|
    options[k] = convert_to_hash(options[k]).reverse_merge convert_to_hash(ActionView::Base.swf_default_options[k])
  end
  options.reverse_merge!(DEFAULTS)
  options[:id] ||= source.gsub(/^.*\//, '').gsub(/\.swf$/,'')
  options[:id] = force_to_valid_id(options[:id])
  options[:div_id] ||= options[:id]+"_div"
  options[:div_id] = force_to_valid_id(options[:div_id])
  options[:width], options[:height] = options[:size].scan(/^(\d*%?)x(\d*%?)$/).first if options[:size]
  options[:auto_install] &&= @view.swf_path(options[:auto_install])
  options[:flashvars][:id] ||= options[:id]
  @mode = options.delete(:mode)
  @options = options
  unless VALID_MODES.include? @mode
    raise ArgumentError, "options[:mode] should be either #{VALID_MODES.join(' or ')}"
  end
end

Instance Method Details

#force_to_valid_id(id) ⇒ Object



42
43
44
45
46
# File 'app/models/swf_fu/generator.rb', line 42

def force_to_valid_id(id)
  id = id.gsub /[^A-Za-z0-9\-_]/, "_" # HTML id can only contain these characters
  id = "swf_" + id unless id =~ /^[A-Z]/i # HTML id must start with alpha
  id
end

#generate(&block) ⇒ Object



48
49
50
51
# File 'app/models/swf_fu/generator.rb', line 48

def generate(&block)
  @options[:alt] = @view.capture(&block) if block_given?
  send(@mode)
end