Class: Gakubuchi::Template

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/gakubuchi/template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_path) ⇒ Template

Returns a new instance of Template.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gakubuchi/template.rb', line 22

def initialize(source_path)
  path = ::Pathname.new(source_path)
  root = self.class.root

  @extname = extract_extname(path)
  @source_path = path.absolute? ? path : root.join(path)

  case
  when !@extname.include?('html')
    fail Error::InvalidTemplate, 'source path must refer to a template file'
  when !@source_path.fnmatch?(root.join('*').to_s)
    fail Error::InvalidTemplate, "template must exist in #{root}"
  end
end

Instance Attribute Details

#extnameObject (readonly)

Returns the value of attribute extname.



5
6
7
# File 'lib/gakubuchi/template.rb', line 5

def extname
  @extname
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



5
6
7
# File 'lib/gakubuchi/template.rb', line 5

def source_path
  @source_path
end

Class Method Details

.allObject



14
15
16
# File 'lib/gakubuchi/template.rb', line 14

def self.all
  ::Dir.glob(root.join('**/*.html*')).map { |source_path| new(source_path) }
end

.rootObject



18
19
20
# File 'lib/gakubuchi/template.rb', line 18

def self.root
  ::Rails.root.join('app/assets', ::Gakubuchi.configuration.template_directory)
end

Instance Method Details

#destination_pathObject



37
38
39
# File 'lib/gakubuchi/template.rb', line 37

def destination_path
  ::Rails.public_path.join(logical_path)
end

#digest_pathObject



41
42
43
44
45
46
# File 'lib/gakubuchi/template.rb', line 41

def digest_path
  resolved_path = view_context.asset_digest_path(logical_path.to_s)
  return if resolved_path.nil?

  ::Pathname.new(::File.join(::Rails.public_path, view_context.assets_prefix, resolved_path))
end

#logical_pathObject



48
49
50
51
# File 'lib/gakubuchi/template.rb', line 48

def logical_path
  dirname = source_path.relative_path_from(self.class.root).dirname
  ::Pathname.new(dirname).join("#{basename(extname)}.html")
end