Class: Gisture::File

Inherits:
Object
  • Object
show all
Defined in:
lib/gisture/file.rb

Constant Summary collapse

STRATEGIES =
[:eval, :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



50
51
52
53
# File 'lib/gisture/file.rb', line 50

def method_missing(meth, *args, &block)
  return file.send(meth, *args, &block) if file.respond_to?(meth)
  super
end

Instance Attribute Details

#basenameObject (readonly)

Returns the value of attribute basename.



5
6
7
# File 'lib/gisture/file.rb', line 5

def basename
  @basename
end

#fileObject (readonly)

Returns the value of attribute file.



5
6
7
# File 'lib/gisture/file.rb', line 5

def file
  @file
end

#strategyObject

Returns the value of attribute strategy.



5
6
7
# File 'lib/gisture/file.rb', line 5

def strategy
  @strategy
end

Instance Method Details

#eval!(&block) ⇒ Object



25
26
27
28
29
# File 'lib/gisture/file.rb', line 25

def eval!(&block)
  clean_room = Evaluator.new(file.content)
  clean_room.instance_eval &block if block_given?
  clean_room
end

#load!(&block) ⇒ Object



19
20
21
22
23
# File 'lib/gisture/file.rb', line 19

def load!(&block)
  loaded = load tempfile.path
  unlink_tempfile
  block_given? ? yield : loaded
end

#require!(&block) ⇒ Object



13
14
15
16
17
# File 'lib/gisture/file.rb', line 13

def require!(&block)
  required = require tempfile.path
  unlink_tempfile
  block_given? ? yield : required
end

#respond_to_missing?(meth, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/gisture/file.rb', line 55

def respond_to_missing?(meth, include_private=false)
  file.respond_to?(meth, include_private)
end

#run!(&block) ⇒ Object



9
10
11
# File 'lib/gisture/file.rb', line 9

def run!(&block)
  send "#{strategy}!".to_sym, &block
end

#tempfileObject



36
37
38
39
40
41
42
43
# File 'lib/gisture/file.rb', line 36

def tempfile
  @tempfile ||= begin
    tmpfile = Tempfile.new([basename, file.filename, ::File.extname(file.filename)].compact, Gisture.configuration.tmpdir)
    tmpfile.write(file.content)
    tmpfile.close
    tmpfile
  end
end


45
46
47
48
# File 'lib/gisture/file.rb', line 45

def unlink_tempfile
  tempfile.unlink
  @tempfile = nil
end