Class: Jzip::Engine::Template

Inherits:
Struct
  • Object
show all
Includes:
Support::Notifier
Defined in:
lib/jzip/engine/template.rb

Constant Summary collapse

@@instances =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Support::Notifier

#notify

Constructor Details

#initialize(*args) ⇒ Template

Returns a new instance of Template.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/jzip/engine/template.rb', line 20

def initialize(*args)
  super

  @file_name   = File.basename(self.template, ".jz") + ".js"
  @target_dir  = File.dirname File.join(self.target, self.template.gsub(self.source, ""))
  if partial?
    @target_dir.gsub! Engine.root_dir, Engine.tmp_dir
  end
  @target_file = File.join @target_dir, @file_name

  scan_template
end

Instance Attribute Details

#sourceObject

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



5
6
7
# File 'lib/jzip/engine/template.rb', line 5

def source
  @source
end

#targetObject

Returns the value of attribute target

Returns:

  • (Object)

    the current value of target



5
6
7
# File 'lib/jzip/engine/template.rb', line 5

def target
  @target
end

#target_fileObject (readonly)

Returns the value of attribute target_file.



10
11
12
# File 'lib/jzip/engine/template.rb', line 10

def target_file
  @target_file
end

#templateObject

Returns the value of attribute template

Returns:

  • (Object)

    the current value of template



5
6
7
# File 'lib/jzip/engine/template.rb', line 5

def template
  @template
end

Class Method Details

.build(*args) ⇒ Object



16
17
18
# File 'lib/jzip/engine/template.rb', line 16

def self.build(*args)
  @@instances[args.first] ||= self.new(*args)
end

.clear_instancesObject



12
13
14
# File 'lib/jzip/engine/template.rb', line 12

def self.clear_instances
  @@instances.clear
end

Instance Method Details

#codeObject



53
54
55
# File 'lib/jzip/engine/template.rb', line 53

def code
  parse
end

#outdated?Boolean

Returns:

  • (Boolean)


57
58
59
60
61
# File 'lib/jzip/engine/template.rb', line 57

def outdated?
  return true if missing?
  notify "Outdated  '#{self.template}'" if result = File.mtime(@target_file) < File.mtime(self.template)
  result
end

#parseObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/jzip/engine/template.rb', line 33

def parse
  unless requires_parsing?
    notify "Skipping  '#{self.template}'"
  else
    notify "Parsing   '#{self.template}'"

    @code = @segments.collect do |segment|
              segment.is_a?(Requirement) ?
                segment.code :
                segment.join("")
            end.
            join("\n\n").
            gsub(/^\s*$\n{2,}/, "\n").
            strip.insert(0, "\n").insert(-1, "\n")

    write_file
  end
  @code ||= File.read(@target_file)
end

#partial?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/jzip/engine/template.rb', line 63

def partial?
  !!@file_name.match(REG_EXPS[:partial])
end