Class: Massimo::Resource
- Inherits:
-
Object
- Object
- Massimo::Resource
- Extended by:
- ActiveSupport::Memoizable
- Defined in:
- lib/massimo/resource.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#source_path ⇒ Object
readonly
Returns the value of attribute source_path.
Class Method Summary collapse
-
.all ⇒ Object
Finds all the Resources in the resource’s directory.
-
.find(name) ⇒ Object
Finds a Resource by the given name.
-
.path ⇒ Object
The path to the resource’s directory.
-
.processable? ⇒ Boolean
Whether or not massimo should process the resource’s files.
-
.resource_name ⇒ Object
The underscored, pluralized name of the resource.
-
.url ⇒ Object
The base url for the resource.
Instance Method Summary collapse
-
#extension ⇒ Object
The extension to output with.
-
#extensions ⇒ Object
A list of all the extensions appended to the filename.
-
#filename ⇒ Object
The basename of the source file.
-
#initialize(source) ⇒ Resource
constructor
Creates a new resource for the given source file.
-
#output_path ⇒ Object
The path to the output file.
-
#process ⇒ Object
Writes the rendered content to the output file.
-
#render ⇒ Object
Runs the content through any necessary filters, templates, etc.
-
#url ⇒ Object
The url to the resource.
Constructor Details
#initialize(source) ⇒ Resource
Creates a new resource for the given source file. The contents of the file will automatically be read.
58 59 60 61 |
# File 'lib/massimo/resource.rb', line 58 def initialize(source) @source_path = Pathname.new(source). read_source end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
54 55 56 |
# File 'lib/massimo/resource.rb', line 54 def content @content end |
#source_path ⇒ Object (readonly)
Returns the value of attribute source_path.
54 55 56 |
# File 'lib/massimo/resource.rb', line 54 def source_path @source_path end |
Class Method Details
.all ⇒ Object
Finds all the Resources in the resource’s directory.
35 36 37 38 39 |
# File 'lib/massimo/resource.rb', line 35 def all files = Massimo.config.files_in resource_name files.reject! { |file| File.basename(file).starts_with?('_') } files.map { |file| self.new(file) } end |
.find(name) ⇒ Object
Finds a Resource by the given name.
29 30 31 32 |
# File 'lib/massimo/resource.rb', line 29 def find(name) resource_path = Dir.glob(File.join(path, "#{name}.*")).first resource_path && self.new(resource_path) end |
.path ⇒ Object
The path to the resource’s directory.
19 20 21 |
# File 'lib/massimo/resource.rb', line 19 def path Massimo.config.path_for resource_name end |
.processable? ⇒ Boolean
Whether or not massimo should process the resource’s files.
42 43 44 |
# File 'lib/massimo/resource.rb', line 42 def processable? true end |
.resource_name ⇒ Object
The underscored, pluralized name of the resource.
14 15 16 |
# File 'lib/massimo/resource.rb', line 14 def resource_name self.name.underscore.sub(/.*\//, '').pluralize end |
Instance Method Details
#extension ⇒ Object
The extension to output with.
69 70 71 |
# File 'lib/massimo/resource.rb', line 69 def extension extensions.first end |
#extensions ⇒ Object
A list of all the extensions appended to the filename.
74 75 76 |
# File 'lib/massimo/resource.rb', line 74 def extensions filename.scan /\.[^.]+/ end |
#filename ⇒ Object
The basename of the source file.
64 65 66 |
# File 'lib/massimo/resource.rb', line 64 def filename source_path.basename.to_s end |
#output_path ⇒ Object
The path to the output file.
89 90 91 |
# File 'lib/massimo/resource.rb', line 89 def output_path Pathname.new File.join(Massimo.config.output_path, url.sub(/^#{Regexp.escape(Massimo.config.base_url)}/, '')) end |
#process ⇒ Object
Writes the rendered content to the output file.
107 108 109 110 111 112 |
# File 'lib/massimo/resource.rb', line 107 def process FileUtils.mkdir_p(output_path.dirname) output_path.open('w') do |f| f.write render end end |
#render ⇒ Object
Runs the content through any necessary filters, templates, etc.
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/massimo/resource.rb', line 94 def render extensions.reverse.inject(content) do |output, ext| if template_type = Tilt[ext] = Massimo.config.(ext[1..-1]) template = template_type.new(source_path.to_s, @line, ) { output } template.render(template_scope, template_locals) else output end end end |
#url ⇒ Object
The url to the resource. This is created by swiching the base path of the source file with the base url.
80 81 82 83 84 85 86 |
# File 'lib/massimo/resource.rb', line 80 def url url = source_path.to_s.sub(/^#{Regexp.escape(self.class.path)}/, '') url = url.sub(/\..+$/, extension) url = File.join(self.class.url, url) unless url.starts_with? self.class.url url = url.dasherize url end |