class Pinion::Conversion
A conversion describes how to convert certain types of files and create asset links for them. Conversions.create() provides a tiny DSL for defining new conversions
Attributes
from_type[R]
gem_required[R]
to_type[R]
Public Class Methods
[](from_and_to)
click to toggle source
# File lib/pinion/conversion.rb, line 11 def self.[](from_and_to) @@conversions[from_and_to] end
add_watch_directory(path)
click to toggle source
# File lib/pinion/conversion.rb, line 13 def self.add_watch_directory(path) @@conversions.values.each { |c| c.add_watch_directory(path) } end
conversions_for(to)
click to toggle source
# File lib/pinion/conversion.rb, line 12 def self.conversions_for(to) @@conversions.values.select { |c| c.to_type == to } end
create(from_and_to, &block)
click to toggle source
# File lib/pinion/conversion.rb, line 14 def self.create(from_and_to, &block) unless from_and_to.is_a?(Hash) && from_and_to.size == 1 raise Error, "Unexpected argument to Conversion.create: #{from_and_to.inspect}" end conversion = Conversion.new *from_and_to.to_a[0] conversion.instance_eval &block conversion.verify @@conversions[conversion.signature] = conversion end
new(from_type, to_type)
click to toggle source
# File lib/pinion/conversion.rb, line 26 def initialize(from_type, to_type) @loaded = false @from_type = from_type @to_type = to_type @gem_required = nil @conversion_fn = nil @watch_fn = Proc.new {} # Don't do anything by default @context = {} end
Public Instance Methods
add_watch_directory(path)
click to toggle source
# File lib/pinion/conversion.rb, line 47 def add_watch_directory(path) @watch_fn.call(path, @context) end
convert(file_contents)
click to toggle source
# File lib/pinion/conversion.rb, line 43 def convert(file_contents) require_dependency @conversion_fn.call(file_contents, @context) end
render(&block)
click to toggle source
# File lib/pinion/conversion.rb, line 38 def render(&block) @conversion_fn = block end
require_gem(gem_name)
click to toggle source
DSL methods
# File lib/pinion/conversion.rb, line 37 def require_gem(gem_name) @gem_required = gem_name end
signature()
click to toggle source
Instance methods
# File lib/pinion/conversion.rb, line 42 def signature() { @from_type => @to_type } end
verify()
click to toggle source
# File lib/pinion/conversion.rb, line 49 def verify unless [@from_type, @to_type].all? { |s| s.is_a? Symbol } raise Error, "Expecting symbol key/value but got #{from_and_to.inspect}" end unless @conversion_fn raise Error, "Must provide a conversion function with convert { |file_contents| ... }." end end
watch(&block)
click to toggle source
# File lib/pinion/conversion.rb, line 39 def watch(&block) @watch_fn = block end
Private Instance Methods
require_dependency()
click to toggle source
# File lib/pinion/conversion.rb, line 60 def require_dependency return if @loaded @loaded = true return unless @gem_required begin require @gem_required rescue LoadError => e raise "Tried to load conversion for #{signature.inspect}, but failed to load the #{@gem_required} gem" end end