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



39
40
41
42
43
44
45
# File 'lib/broadway/mixins/assetable.rb', line 39

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



27
28
29
# File 'lib/broadway/mixins/assetable.rb', line 27

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
# File 'lib/broadway/mixins/assetable.rb', line 12

def assets
  unless @assets
    @assets = []
    if self.data.has_key?("asset")
      @assets << ::Broadway::Asset.new(self, "asset", data["asset"])
    elsif self.data.has_key?("assets")
      self.data["assets"].each do |name, asset|
        @assets << ::Broadway::Asset.new(self, name, asset)
      end
    end
  end
  
  @assets
end

#find_asset_by_name(name) ⇒ Object



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

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

#num_assetsObject



31
32
33
# File 'lib/broadway/mixins/assetable.rb', line 31

def num_assets
  assets.length
end