module RailsBlocks::Path

Constants

ELEMENT_FILE_PREFIX
MOD_FILE_PREFIX

Attributes

tree[W]

Public Class Methods

blocks_dir() click to toggle source
# File lib/rails_blocks/path.rb, line 20
def self.blocks_dir
        Rails.root.join RailsBlocks.config.blocks_dir
end
tree() click to toggle source
# File lib/rails_blocks/path.rb, line 12
def self.tree
        #so slow, переделать на оновление при запросе, а не каждый раз
        #return build_tree if Rails.env.development?
        @tree ||= (
                build_tree
        )
end

Private Class Methods

build_tree() click to toggle source
# File lib/rails_blocks/path.rb, line 58
def self.build_tree
        file_tree
end
file_tree() click to toggle source
# File lib/rails_blocks/path.rb, line 62
def self.file_tree
        t = {}
        files = Dir["#{blocks_dir}/**/*#{RailsBlocks.config.template_engine}"]
        files.each do |file|
                file.sub! blocks_dir.to_s + '/', ''
                parts = file.split('/')
                filename = parts[2].sub RailsBlocks.config.template_engine, ''
                template = {
                        level: parts[0],
                        block: parts[1],
                        file: filename
                }
                t[template[:level]] ||= {}
                t[template[:level]][template[:block]] ||= {elements: {}}
                
                if is_e_file(filename)
                        t[template[:level]][template[:block]][:elements][get_e_name(filename)] ||= {}
                        t[template[:level]][template[:block]][:elements][get_e_name(filename)][get_e_mod(filename)] = file
                else
                        t[template[:level]][template[:block]][get_b_mod(filename)] = file
                end
        end
        t.with_indifferent_access.freeze
end
get_b_mod(file) click to toggle source
# File lib/rails_blocks/path.rb, line 102
def self.get_b_mod(file)
        return '' unless file.start_with? MOD_FILE_PREFIX
        file.match(/^_(.*)/)
        $1
end
get_e_mod(file) click to toggle source
# File lib/rails_blocks/path.rb, line 96
def self.get_e_mod(file)
        return '' unless file.match(/__\w+#{MOD_FILE_PREFIX}\w/)
        file.match(/\A__(?:[^_]*)_(.*)/)
        $1
end
get_e_name(file) click to toggle source
# File lib/rails_blocks/path.rb, line 91
def self.get_e_name(file)
        file.match(/\A__([^_]*)/)
        $1
end
is_e_file(file) click to toggle source
# File lib/rails_blocks/path.rb, line 87
def self.is_e_file(file)
        file.start_with? ELEMENT_FILE_PREFIX
end

Public Instance Methods

block_template(b_name, options = {}) click to toggle source
# File lib/rails_blocks/path.rb, line 24
def block_template(b_name, options = {})
        options[:levels].reverse.each do |level|
                next unless Path.tree[level][b_name]
                return Path.tree[level][b_name][mod(options)] || Path.tree[level][b_name]['']
        end
        nil
end
element_template(b_name, e_name, options = {}) click to toggle source
# File lib/rails_blocks/path.rb, line 32
def element_template(b_name, e_name, options = {})
        options[:levels].reverse.each do |level|
                next unless Path.tree[level][b_name]
                next unless Path.tree[level][b_name][:elements][e_name]
                return Path.tree[level][b_name][:elements][e_name][mod(options)] || Path.tree[level][b_name][:elements][e_name]['']
        end
        nil
end

Private Instance Methods

get_block_dir(b_name, levels) click to toggle source
# File lib/rails_blocks/path.rb, line 48
def get_block_dir(b_name, levels)
        levels.reverse.each do |level|
                block_dir = File.join(self.blocks_dir, level, b_name)
                if Dir.exists? block_dir
                        return block_dir
                end
        end
        nil
end
mod(options) click to toggle source
# File lib/rails_blocks/path.rb, line 43
def mod(options)
        return '' unless options[:mods] && !options[:mods].empty?
        mod_class(*options[:mods].first)
end