Class: AppScrollsScrolls::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/appscrolls/template.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scrolls) ⇒ Template

Returns a new instance of Template.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/appscrolls/template.rb', line 5

def initialize(scrolls)
  @unknown_scroll_names = []
  @scrolls = scrolls.inject([]) do |list, name|
    scroll = AppScrollsScrolls::Scroll.from_mongo(name)
    if scroll
      list << scroll
    else
      @unknown_scroll_names << name
      $stderr.puts "Unknown scroll '#{name}'. Skipping."
    end
    list
  end
end

Instance Attribute Details

#scrollsObject (readonly)

Returns the value of attribute scrolls.



3
4
5
# File 'lib/appscrolls/template.rb', line 3

def scrolls
  @scrolls
end

#unknown_scroll_namesObject (readonly)

Returns the value of attribute unknown_scroll_names.



3
4
5
# File 'lib/appscrolls/template.rb', line 3

def unknown_scroll_names
  @unknown_scroll_names
end

Class Method Details

.render(template_name, binding = nil) ⇒ Object



23
24
25
26
# File 'lib/appscrolls/template.rb', line 23

def self.render(template_name, binding = nil)
  erb = ERB.new(File.open(template_root + '/' + template_name + '.erb').read)
  erb.result(binding)
end

.template_rootObject



19
20
21
# File 'lib/appscrolls/template.rb', line 19

def self.template_root
  File.dirname(__FILE__) + '/../../templates'
end

Instance Method Details

#argsObject



60
61
62
# File 'lib/appscrolls/template.rb', line 60

def args
  scrolls.map(&:args).uniq
end

#compileObject



56
57
58
# File 'lib/appscrolls/template.rb', line 56

def compile
  render 'layout', binding
end

#custom_codeObject



65
# File 'lib/appscrolls/template.rb', line 65

def custom_code; nil end

#custom_code?Boolean

Returns:

  • (Boolean)


64
# File 'lib/appscrolls/template.rb', line 64

def custom_code?; false end

#render(template_name, binding = nil) ⇒ Object



27
# File 'lib/appscrolls/template.rb', line 27

def render(template_name, binding = nil); self.class.render(template_name, binding) end

#resolve_scrollsObject



30
31
32
# File 'lib/appscrolls/template.rb', line 30

def resolve_scrolls
  @resolve_scrolls ||= scrolls_with_dependencies.sort.sort
end

#scroll_classesObject



34
35
36
# File 'lib/appscrolls/template.rb', line 34

def scroll_classes
  @scroll_classes ||= scrolls.map { |name| AppScrollsScrolls::Scroll.from_mongo(name) }
end

#scrolls_with_dependenciesObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/appscrolls/template.rb', line 38

def scrolls_with_dependencies
  @scrolls_with_dependencies ||= scroll_classes
  
  added_more = false
  for scroll in scroll_classes
    scroll.requires.each do |requirement|
      requirement = AppScrollsScrolls::Scroll.from_mongo(requirement)
      count = @scrolls_with_dependencies.size
      (@scrolls_with_dependencies << requirement).uniq!
      unless @scrolls_with_dependencies.size == count
        added_more = true
      end
    end
  end

  added_more ? scrolls_with_dependencies : @scrolls_with_dependencies
end