Class: Hyraft::Compiler::JoinProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/hyraft/compiler/join_processor.rb

Constant Summary collapse

ROOT =
Dir.pwd.freeze

Class Method Summary collapse

Class Method Details

.load_partial(join_path, current_file) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hyraft/compiler/join_processor.rb', line 18

def self.load_partial(join_path, current_file)
  full_path = resolve_partial_path(join_path, current_file)
  return nil unless full_path && File.exist?(full_path)
  
  content = File.read(full_path)
  content = process_component_joins(content, full_path)
  
  if content =~ /<hyr>(.*?)<\/hyr>/m
    ruby_code = $1
    ctx = Object.new
    ctx.instance_eval(ruby_code)
    ctx.instance_variables.each do |ivar|
      name = ivar.to_s[1..-1]
      value = ctx.instance_variable_get(ivar)
      content = content.gsub(/:\[#{name}\]/, value.to_s)
    end
    content = content.gsub(/<hyr>.*?<\/hyr>/m, '')
  end
  
  content
end

.process_component_joins(content, current_file) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/hyraft/compiler/join_processor.rb', line 6

def self.process_component_joins(content, current_file)
  content.gsub(/<join\s+"([^"]+)"\s*\/>/) do
    join_path = $1
    next if join_path.end_with?('.css', '.js')
    
    partial_content = load_partial(join_path, current_file)
    partial_content || "<!-- Component not found: #{join_path} -->"
  end
end

.resolve_partial_path(join_path, current_file) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/hyraft/compiler/join_processor.rb', line 40

def self.resolve_partial_path(join_path, current_file)
  return join_path if File.exist?(join_path)
  return "#{join_path}.hyr" if File.exist?("#{join_path}.hyr")
  
  base_dir = File.dirname(current_file)
  local_path = File.join(base_dir, join_path)
  return local_path if File.exist?(local_path)
  return "#{local_path}.hyr" if File.exist?("#{local_path}.hyr")
  
  patterns = [
    File.join(ROOT, 'app', 'web', 'display', 'shared', "#{join_path}.hyr"),
    File.join(ROOT, 'app', 'web', 'display', 'components', "#{join_path}.hyr"),
    File.join(ROOT, 'app', 'web', 'display', 'partials', "#{join_path}.hyr"),
    File.join(ROOT, 'app', 'web', 'display', 'segments', "#{join_path}.hyr"),
    File.join(ROOT, 'app', 'web', 'display', 'layouts', "#{join_path}.hyr"),
    File.join(ROOT, 'app', '*', 'display', '**', "#{join_path}.hyr")
  ]
  
  patterns.each do |pattern|
    matches = Dir.glob(pattern)
    return matches.first if matches.any?
  end
  
  nil
end