Class: RBlade::ComponentStore

Inherits:
Object
  • Object
show all
Defined in:
lib/rblade/component_store.rb

Constant Summary collapse

FILE_EXTENSIONS =
[".rblade", ".html.rblade"]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeComponentStore

Returns a new instance of ComponentStore.



7
8
9
10
11
# File 'lib/rblade/component_store.rb', line 7

def initialize
  @component_definitions = +""
  @component_name_stack = []
  @component_method_names = {}
end

Class Method Details

.add_path(path, namespace = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/rblade/component_store.rb', line 33

def self.add_path(path, namespace = nil)
  path = path.to_s
  unless path.end_with? "/"
    path << "/"
  end

  template_paths[namespace] ||= []
  template_paths[namespace] << path
end

.find_component_file(name, name_stack = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rblade/component_store.rb', line 55

def self.find_component_file(name, name_stack = nil)
  if name.match? "::"
    namespace, name = name.split("::", 2)
  end

  file_path = name.tr ".", "/"

  template_paths[namespace]&.each do |base_path|
    FILE_EXTENSIONS.each do |extension|
      if File.exist? base_path + file_path + extension
        return "#{base_path}#{file_path}#{extension}"
      end
      if File.exist? base_path + file_path + "/index" + extension
        # Account for index files for relative components
        name_stack << name_stack.pop + ".index" if name_stack
        return "#{base_path}#{file_path}/index#{extension}"
      end
    end
  end

  raise RBladeTemplateError.new "Unknown component #{namespace}::#{name}"
end

Instance Method Details

#component(full_name) ⇒ Object

Retrieve the method name for a component, and compile it if it hasn’t already been compiled



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rblade/component_store.rb', line 14

def component(full_name)
  # If this is a relative path, prepend with the previous component name's base
  if full_name.start_with? "."
    full_name = @component_name_stack.last.sub(/[^.]++\z/, "") + full_name.delete_prefix(".")
  end

  # Ensure each component is only compiled once
  unless @component_method_names[full_name].nil?
    return @component_method_names[full_name]
  end

  @component_name_stack << full_name

  method_name = compile_component full_name, File.read(find_component_file(full_name, @component_name_stack))
  @component_name_stack.pop

  method_name
end

#current_view_nameObject



47
48
49
# File 'lib/rblade/component_store.rb', line 47

def current_view_name
  @component_name_stack.last
end

#getObject



51
52
53
# File 'lib/rblade/component_store.rb', line 51

def get
  @component_definitions
end

#view_name(view_name) ⇒ Object



43
44
45
# File 'lib/rblade/component_store.rb', line 43

def view_name(view_name)
  @component_name_stack.push view_name
end