Class: Gluey::Material

Inherits:
Object
  • Object
show all
Defined in:
lib/gluey/material.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, glue, context) {|_self| ... } ⇒ Material

Returns a new instance of Material.

Yields:

  • (_self)

Yield Parameters:



8
9
10
11
12
13
14
15
16
17
# File 'lib/gluey/material.rb', line 8

def initialize(name, glue, context)
  @name = name.to_sym
  @glue = glue
  @context = context
  @asset = name.to_s
  @paths = []
  @items = []
  yield self if block_given?
  @file_extension ||= @asset.dup
end

Instance Attribute Details

#assetObject

Returns the value of attribute asset.



6
7
8
# File 'lib/gluey/material.rb', line 6

def asset
  @asset
end

#file_extensionObject

Returns the value of attribute file_extension.



6
7
8
# File 'lib/gluey/material.rb', line 6

def file_extension
  @file_extension
end

#glueObject (readonly)

Returns the value of attribute glue.



5
6
7
# File 'lib/gluey/material.rb', line 5

def glue
  @glue
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/gluey/material.rb', line 5

def name
  @name
end

#public_dirObject

Returns the value of attribute public_dir.



6
7
8
# File 'lib/gluey/material.rb', line 6

def public_dir
  @public_dir
end

Instance Method Details

#add_item(declaration) ⇒ Object



23
24
25
# File 'lib/gluey/material.rb', line 23

def add_item(declaration)
  @items << declaration
end

#add_path(path) ⇒ Object



19
20
21
# File 'lib/gluey/material.rb', line 19

def add_path(path)
  @paths << path
end

#find_base_file(path) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/gluey/material.rb', line 44

def find_base_file(path)
  paths.each do |base_path|
    p = "#{base_path}/#{path}.#{@file_extension}"; return p if File.exists? p
    p = "#{p}.erb"; return p if File.exists? p
    p = "#{base_path}/#{path}/index.#{@file_extension}"; return p if File.exists? p
    p = "#{p}.erb"; return p if File.exists? p
    end
  raise(::Gluey::FileNotFound.new "#{to_s} cannot find base file for #{path}")
end

#is_listed?(path, file = nil) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gluey/material.rb', line 27

def is_listed?(path, file=nil)
  @items.any? do |items_declaration|
    case items_declaration
      when String
        path == items_declaration
      when Regexp
        path =~ items_declaration
      when Proc
        items_declaration[path, file]
    end
  end
end

#list_all_itemsObject



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gluey/material.rb', line 54

def list_all_items
  list = []
  paths.map do |base_path|
    glob_path = "#{base_path}/**/*.#{@file_extension}"
    files = Dir[glob_path] + Dir["#{glob_path}.erb"]
    files.select do |file|
      path = file[/^#{base_path}\/(.+)\.#{@file_extension}(?:\.erb)?$/, 1]
      path.gsub! /\/index$/, ''
      list << path if is_listed? path, file
    end
  end
  list.uniq
end

#to_sObject



40
41
42
# File 'lib/gluey/material.rb', line 40

def to_s
  "Material #{@name}"
end