Class: Gluey::Material
- Inherits:
-
Object
- Object
- Gluey::Material
- Defined in:
- lib/gluey/workshop/material.rb
Instance Attribute Summary collapse
-
#asset_extension ⇒ Object
Returns the value of attribute asset_extension.
-
#file_extension ⇒ Object
Returns the value of attribute file_extension.
-
#glue ⇒ Object
readonly
Returns the value of attribute glue.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#paths ⇒ Object
readonly
Returns the value of attribute paths.
Instance Method Summary collapse
- #find_base_file(path) ⇒ Object
-
#initialize(name, glue, context) {|_self| ... } ⇒ Material
constructor
A new instance of Material.
- #is_listed?(path, file) ⇒ Boolean
- #list_all_items ⇒ Object
- #set(**opts) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(name, glue, context) {|_self| ... } ⇒ Material
Returns a new instance of Material.
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_extension ⇒ Object
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_extension ⇒ Object
Returns the value of attribute file_extension.
5 6 7 |
# File 'lib/gluey/workshop/material.rb', line 5 def file_extension @file_extension end |
#glue ⇒ Object (readonly)
Returns the value of attribute glue.
4 5 6 |
# File 'lib/gluey/workshop/material.rb', line 4 def glue @glue end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
4 5 6 |
# File 'lib/gluey/workshop/material.rb', line 4 def items @items end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
4 5 6 |
# File 'lib/gluey/workshop/material.rb', line 4 def name @name end |
#paths ⇒ Object (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
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_items ⇒ Object
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) = %i(paths items asset_extension file_extension) opts.each do |k, value| next unless .include? k instance_variable_set "@#{k}", value end end |
#to_s ⇒ Object
41 42 43 |
# File 'lib/gluey/workshop/material.rb', line 41 def to_s "Material #{@name}" end |