Class: Gluey::Material

Inherits:
Object
  • Object
show all
Defined in:
lib/gluey/workshop/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:



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

def initialize(name, glue, context)
  @name = name.to_sym
  @glue = glue
  @context = context

  set({asset_extension: name.to_s, paths: [], items: []})
  yield self if block_given?
  @file_extension ||= @asset_extension.dup
end

Instance Attribute Details

#asset_extensionObject

Returns the value of attribute asset_extension.



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

def asset_extension
  @asset_extension
end

#file_extensionObject

Returns the value of attribute file_extension.



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

def file_extension
  @file_extension
end

#glueObject (readonly)

Returns the value of attribute glue.



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

def glue
  @glue
end

#itemsObject (readonly)

Returns the value of attribute items.



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

def items
  @items
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#pathsObject (readonly)

Returns the value of attribute paths.



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

def paths
  @paths
end

Instance Method Details

#find_base_file(path) ⇒ Object



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

def find_base_file(path)
  full_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) ⇒ Boolean

Returns:

  • (Boolean)


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

def is_listed?(path, file)
  file[/\.(\w+)(?:\.erb)?$/, 1]==@file_extension &&
      @items.any? do |items_declaration|
        case items_declaration
          when :all, :any
            true
          when String
            path == items_declaration
          when Regexp
            path =~ items_declaration
          when Proc
            items_declaration[path, file]
        end
      end
end

#list_all_itemsObject



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

def list_all_items
  list = []
  full_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

#set(**opts) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/gluey/workshop/material.rb', line 17

def set(**opts)
  allowed_options = %i(paths items asset_extension file_extension)
  opts.each do |k, value|
    next unless allowed_options.include? k
    instance_variable_set "@#{k}", value
  end
end

#to_sObject



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

def to_s
  "Material #{@name}"
end