Class: Massimo::Resource

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Memoizable
Defined in:
lib/massimo/resource.rb

Direct Known Subclasses

Javascript, Page, Stylesheet, View

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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).expand_path
  read_source
end

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content.



54
55
56
# File 'lib/massimo/resource.rb', line 54

def content
  @content
end

#source_pathObject (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

.allObject

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

.pathObject

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.

Returns:

  • (Boolean)


42
43
44
# File 'lib/massimo/resource.rb', line 42

def processable?
  true
end

.resource_nameObject

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

.urlObject

The base url for the resource.



24
25
26
# File 'lib/massimo/resource.rb', line 24

def url
  Massimo.config.url_for resource_name
end

Instance Method Details

#extensionObject

The extension to output with.



69
70
71
# File 'lib/massimo/resource.rb', line 69

def extension
  extensions.first
end

#extensionsObject

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

#filenameObject

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_pathObject

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

#processObject

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

#renderObject

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]
      template_options = Massimo.config.options_for(ext[1..-1])
      template         = template_type.new(source_path.to_s, @line, template_options) { output }
      template.render(template_scope, template_locals)
    else
      output
    end
  end
end

#urlObject

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