Class: ComponentTemplates

Inherits:
Object show all
Defined in:
lib/volt/server/component_templates.rb

Overview

Initialize with the path to a component and returns all the front-end setup code (for controllers, models, views, and routes)

Instance Method Summary collapse

Constructor Details

#initialize(component_path, component_name, client = true) ⇒ ComponentTemplates

Returns a new instance of ComponentTemplates.



6
7
8
9
10
# File 'lib/volt/server/component_templates.rb', line 6

def initialize(component_path, component_name, client=true)
  @component_path = component_path
  @component_name = component_name
  @client = true
end

Instance Method Details

#codeObject



12
13
14
15
16
17
18
19
20
# File 'lib/volt/server/component_templates.rb', line 12

def code
  code = generate_view_code
  if @client
    # On the backend, we just need the views
    code << generate_controller_code + generate_model_code + generate_routes_code
  end

  return code
end

#generate_controller_codeObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/volt/server/component_templates.rb', line 61

def generate_controller_code
  code = ''
  controllers_path = "#{@component_path}/controllers/"

  Dir["#{controllers_path}*_controller.rb"].sort.each do |controller_path|
    code << File.read(controller_path) + "\n\n"
  end

  return code
end

#generate_model_codeObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/volt/server/component_templates.rb', line 72

def generate_model_code
  code = ''
  models_path = "#{@component_path}/models/"

  Dir["#{models_path}*.rb"].sort.each do |model_path|
    code << File.read(model_path) + "\n\n"

    model_name = model_path.match(/([^\/]+)[.]rb$/)[1]

    code << "#{page_reference}.add_model(#{model_name.inspect})\n\n"
  end

  return code
end

#generate_routes_codeObject



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/volt/server/component_templates.rb', line 87

def generate_routes_code
  code = ''
  routes_path = "#{@component_path}/config/routes.rb"

  if File.exists?(routes_path)
    code << "#{page_reference}.add_routes do\n"
    code << "\n" + File.read(routes_path) + "\n"
    code << "end\n\n"
  end

  return code
end

#generate_view_codeObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/volt/server/component_templates.rb', line 30

def generate_view_code
  code = ''
  views_path = "#{@component_path}/views/"

  # Load all templates in the folder
  Dir["#{views_path}*/*.html"].sort.each do |view_path|
    # Get the path for the template, supports templates in folders
    template_path = view_path[views_path.size..((-1 * ('.html'.size + 1)))]
    template_path = "#{@component_name}/#{template_path}"

    all_templates = ViewParser.new(File.read(view_path), template_path)

    binding_initializers = []
    all_templates.templates.each_pair do |name, template|
      binding_code = []

      if template['bindings']
        template['bindings'].each_pair do |key,value|
          binding_code << "#{key.inspect} => [#{value.join(', ')}]"
        end
      end

      binding_code = "{#{binding_code.join(', ')}}"

      code << "#{page_reference}.add_template(#{name.inspect}, #{template['html'].inspect}, #{binding_code})\n"
    end
  end

  return code
end

#page_referenceObject



22
23
24
25
26
27
28
# File 'lib/volt/server/component_templates.rb', line 22

def page_reference
  if @client
    '$page'
  else
    'page'
  end
end