Class: Gisture::File
- Inherits:
-
Object
show all
- Defined in:
- lib/gisture/file.rb
Constant Summary
collapse
- STRATEGIES =
[:eval, :exec, :load, :require]
Instance Attribute Summary collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
105
106
107
108
|
# File 'lib/gisture/file.rb', line 105
def method_missing(meth, *args, &block)
return file.send(meth, *args, &block) if file.respond_to?(meth)
super
end
|
Instance Attribute Details
#basename ⇒ Object
Returns the value of attribute basename.
5
6
7
|
# File 'lib/gisture/file.rb', line 5
def basename
@basename
end
|
#file ⇒ Object
Returns the value of attribute file.
5
6
7
|
# File 'lib/gisture/file.rb', line 5
def file
@file
end
|
#strategy ⇒ Object
Returns the value of attribute strategy.
5
6
7
|
# File 'lib/gisture/file.rb', line 5
def strategy
@strategy
end
|
Instance Method Details
#eval!(*args, &block) ⇒ Object
37
38
39
40
41
42
43
44
|
# File 'lib/gisture/file.rb', line 37
def eval!(*args, &block)
Gisture.logger.info "[gisture] Running #{::File.join(basename, (file.filename || file.path))} via the :eval strategy"
args << Gisture::Evaluator
klass = args.first
evaluator = klass.new(file.content)
evaluator.instance_eval &block if block_given?
evaluator
end
|
#exec!(*args, &block) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/gisture/file.rb', line 46
def exec!(*args, &block)
Gisture.logger.info "[gisture] Running #{::File.join(basename, (file.filename || file.path))} via the :exec strategy"
args.map! { |arg| arg.nil? ? tempfile.path : arg }
args = ['ruby'] if args.empty? && extname == '.rb'
args = ['node'] if args.empty? && extname == '.js'
args << tempfile.path unless args.include?(tempfile.path)
::File.chmod(0744, tempfile.path) if args.length == 1
executed = `#{args.join(' ')}`.strip
block_given? ? yield : executed
end
|
#extname ⇒ Object
Also known as:
extension
95
96
97
|
# File 'lib/gisture/file.rb', line 95
def extname
@extname ||= ::File.extname(file.filename)
end
|
#load!(*args, &block) ⇒ Object
30
31
32
33
34
35
|
# File 'lib/gisture/file.rb', line 30
def load!(*args, &block)
Gisture.logger.info "[gisture] Running #{::File.join(basename, (file.filename || file.path))} via the :load strategy"
loaded = load tempfile.path
unlink_tempfile
block_given? ? yield : loaded
end
|
#localize!(root) ⇒ Object
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/gisture/file.rb', line 84
def localize!(root)
@tempfile = begin
fname = ::File.join(root, (file.path || file.filename))
FileUtils.mkdir_p ::File.dirname(fname)
local_file = ::File.open(fname, 'w')
local_file.write(file.content)
local_file.close
local_file
end
end
|
#require!(*args, &block) ⇒ Object
23
24
25
26
27
28
|
# File 'lib/gisture/file.rb', line 23
def require!(*args, &block)
Gisture.logger.info "[gisture] Running #{::File.join(basename, (file.filename || file.path))} via the :require strategy"
required = require tempfile.path
unlink_tempfile
block_given? ? yield : required
end
|
#respond_to_missing?(meth, include_private = false) ⇒ Boolean
110
111
112
|
# File 'lib/gisture/file.rb', line 110
def respond_to_missing?(meth, include_private=false)
file.respond_to?(meth, include_private)
end
|
#run!(*args, &block) ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# File 'lib/gisture/file.rb', line 9
def run!(*args, &block)
strat_key = strategy
if strategy.respond_to?(:keys)
strat_key = strategy.keys.first
if strategy[strat_key].is_a?(Array)
args = args.concat(strategy[strat_key])
else
args << strategy[strat_key]
end
end
send "#{strat_key}!".to_sym, *args, &block
end
|
#tempfile ⇒ Object
75
76
77
78
79
80
81
82
|
# File 'lib/gisture/file.rb', line 75
def tempfile
@tempfile ||= begin
tmpfile = Tempfile.new([basename.to_s.gsub(/\//, '-'), file.filename, extname].compact, Gisture.configuration.tmpdir)
tmpfile.write(file.content)
tmpfile.close
tmpfile
end
end
|
#unlink_tempfile ⇒ Object
100
101
102
103
|
# File 'lib/gisture/file.rb', line 100
def unlink_tempfile
tempfile.unlink
@tempfile = nil
end
|