Module: Broadway::Assetable::InstanceMethods

Defined in:
lib/broadway/mixins/assetable.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/broadway/mixins/assetable.rb', line 48

def method_missing(meth, *args, &block)
  if asset_types.detect { |type| meth.to_s =~ /(\w+)_#{type}/ }
    find_asset_by_name($1.to_s)
  else
    super(meth, *args, &block)
  end
end

Instance Method Details

#assetObject



36
37
38
# File 'lib/broadway/mixins/assetable.rb', line 36

def asset
  assets.first
end

#asset_typesObject



8
9
10
# File 'lib/broadway/mixins/assetable.rb', line 8

def asset_types
  %w(image video audio asset)
end

#assetsObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/broadway/mixins/assetable.rb', line 12

def assets
  unless @assets
    @assets = []
    if self.data.has_key?("asset")
      options = data["asset"].is_a?(Hash) ? data["asset"] : {:src => data["asset"]}            
      @assets << ::Broadway::Asset.new(self, "asset", options.symbolize_keys)
    elsif self.data.has_key?("assets")
      if self.data["assets"].is_a?(Array)
        self.data["assets"].each do |asset|
          options = asset.is_a?(Hash) ? asset : {:src => asset}
          @assets << ::Broadway::Asset.new(self, "asset", options.symbolize_keys)
        end
      elsif self.data["assets"].is_a?(Hash)
        self.data["assets"].each do |name, asset|
          options = asset.is_a?(Hash) ? asset : {:src => asset}            
          @assets << ::Broadway::Asset.new(self, name, options.symbolize_keys)
        end
      end
    end
  end
  
  @assets
end

#find_asset_by_name(name) ⇒ Object



44
45
46
# File 'lib/broadway/mixins/assetable.rb', line 44

def find_asset_by_name(name)
  assets.detect { |asset| asset.name == name }
end

#num_assetsObject



40
41
42
# File 'lib/broadway/mixins/assetable.rb', line 40

def num_assets
  assets.length
end