Class: Erbside::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/erbside/runner.rb

Overview

Runner

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(files, options) ⇒ Runner

Returns a new instance of Runner.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/erbside/runner.rb', line 34

def initialize(files, options)
  files = files || Dir["**/*#{ext_glob}"]
  files = files.map do |file|
    if File.directory?(file)
      collect_usable_files(file)
    else
      file
    end
  end.flatten
  @files  = files

  @force     = options[:force]
  @skip      = options[:skip]
  @output    = options[:output]
  #@delete    = options[:delete]
  @resources = options[:resources]
end

Instance Attribute Details

#filesObject

A T T R I B U T E S



16
17
18
# File 'lib/erbside/runner.rb', line 16

def files
  @files
end

#forceObject

Returns the value of attribute force.



18
19
20
# File 'lib/erbside/runner.rb', line 18

def force
  @force
end

#outputObject

The output can be any object that responds to #<<.



23
24
25
# File 'lib/erbside/runner.rb', line 23

def output
  @output
end

#resourcesObject

Returns the value of attribute resources.



28
29
30
# File 'lib/erbside/runner.rb', line 28

def resources
  @resources
end

#skipObject

Returns the value of attribute skip.



20
21
22
# File 'lib/erbside/runner.rb', line 20

def skip
  @skip
end

Instance Method Details

#collect_usable_files(dir) ⇒ Object



58
59
60
# File 'lib/erbside/runner.rb', line 58

def collect_usable_files(dir)
  Dir[File.join(dir,"**/*#{ext_glob}")]
end

#debug?Boolean

Returns:

  • (Boolean)


66
# File 'lib/erbside/runner.rb', line 66

def debug?  ; $DEBUG  ; end

#force?Boolean

def delete? ; @delete ; end

Returns:

  • (Boolean)


63
# File 'lib/erbside/runner.rb', line 63

def force?  ; @force  ; end

#renderObject



70
71
72
73
74
# File 'lib/erbside/runner.rb', line 70

def render
  files.each do |file|
    render_file(file)
  end
end

#render_file(file) ⇒ Object

Search through a file for inline templates, render and output.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/erbside/runner.rb', line 77

def render_file(file)
  parser = Inline.factory(file)

  if !parser
    puts "  unrecognized #{file}" if $DEBUG || $TRIAL
    return
  end

  template = parser.new(file, *resources)

  if template.exist? && skip?
    puts "  #{template.relative_output} skipped"
  else
    result = template.render
    if output
      output << (result + "\n")
    else
      save(template, result)
    end
  end
end

#save(template, result) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/erbside/runner.rb', line 100

def save(template, result)
  name = template.relative_output
  save = false
  if trial?
    puts "  #{name}"
  else
    if template.exist?
      if !template.changed?
        puts "  unchanged #{name}"
      elsif !force?
        case ask("  overwrite #{name}? ")
        when 'y', 'yes'
          save = true
        end
      else
        save = true
      end
    else
      save = true
    end
  end
  if save
    template.save
    puts "    written #{name}"
  end
end

#skip?Boolean

Returns:

  • (Boolean)


64
# File 'lib/erbside/runner.rb', line 64

def skip?   ; @skip   ; end

#trial?Boolean

Returns:

  • (Boolean)


67
# File 'lib/erbside/runner.rb', line 67

def trial?  ; $TRIAL  ; end