Class: Pinion::Conversion
- Inherits:
-
Object
- Object
- Pinion::Conversion
- Defined in:
- lib/pinion/conversion.rb
Overview
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
Constant Summary collapse
- @@conversions =
{}
Instance Attribute Summary collapse
-
#from_type ⇒ Object
readonly
Returns the value of attribute from_type.
-
#gem_required ⇒ Object
readonly
Returns the value of attribute gem_required.
-
#to_type ⇒ Object
readonly
Returns the value of attribute to_type.
Class Method Summary collapse
- .[](from_and_to) ⇒ Object
- .add_watch_directory(path) ⇒ Object
- .conversions_for(to) ⇒ Object
- .create(from_and_to, &block) ⇒ Object
Instance Method Summary collapse
- #add_watch_directory(path) ⇒ Object
- #convert(file_contents) ⇒ Object
-
#initialize(from_type, to_type) ⇒ Conversion
constructor
A new instance of Conversion.
- #render(&block) ⇒ Object
-
#require_gem(gem_name) ⇒ Object
DSL methods.
-
#signature ⇒ Object
Instance methods.
- #verify ⇒ Object
- #watch(&block) ⇒ Object
Constructor Details
#initialize(from_type, to_type) ⇒ Conversion
Returns a new instance of Conversion.
26 27 28 29 30 31 32 33 34 |
# 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 |
Instance Attribute Details
#from_type ⇒ Object (readonly)
Returns the value of attribute from_type.
24 25 26 |
# File 'lib/pinion/conversion.rb', line 24 def from_type @from_type end |
#gem_required ⇒ Object (readonly)
Returns the value of attribute gem_required.
24 25 26 |
# File 'lib/pinion/conversion.rb', line 24 def gem_required @gem_required end |
#to_type ⇒ Object (readonly)
Returns the value of attribute to_type.
24 25 26 |
# File 'lib/pinion/conversion.rb', line 24 def to_type @to_type end |
Class Method Details
.[](from_and_to) ⇒ Object
11 |
# File 'lib/pinion/conversion.rb', line 11 def self.[](from_and_to) @@conversions[from_and_to] end |
.add_watch_directory(path) ⇒ Object
13 |
# 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) ⇒ Object
12 |
# 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) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# 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 |
Instance Method Details
#add_watch_directory(path) ⇒ Object
47 |
# File 'lib/pinion/conversion.rb', line 47 def add_watch_directory(path) @watch_fn.call(path, @context) end |
#convert(file_contents) ⇒ Object
43 44 45 46 |
# File 'lib/pinion/conversion.rb', line 43 def convert(file_contents) require_dependency @conversion_fn.call(file_contents, @context) end |
#render(&block) ⇒ Object
38 |
# File 'lib/pinion/conversion.rb', line 38 def render(&block) @conversion_fn = block end |
#require_gem(gem_name) ⇒ Object
DSL methods
37 |
# File 'lib/pinion/conversion.rb', line 37 def require_gem(gem_name) @gem_required = gem_name end |
#signature ⇒ Object
Instance methods
42 |
# File 'lib/pinion/conversion.rb', line 42 def signature() { @from_type => @to_type } end |
#verify ⇒ Object
49 50 51 52 53 54 55 56 |
# 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) ⇒ Object
39 |
# File 'lib/pinion/conversion.rb', line 39 def watch(&block) @watch_fn = block end |