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



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

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



27
28
29
30
31
32
# File 'lib/gisture/file.rb', line 27

def eval!(&block)
  Gisture.logger.info "[gisture] Running #{basename}/#{file.path || file.filename} via the :eval strategy"
  clean_room = Evaluator.new(file.content)
  clean_room.instance_eval &block if block_given?
  clean_room
end

#load!(&block) ⇒ Object



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

def load!(&block)
  Gisture.logger.info "[gisture] Running #{basename}/#{file.path || file.filename} via the :load strategy"
  loaded = load tempfile.path
  unlink_tempfile
  block_given? ? yield : loaded
end

#require!(&block) ⇒ Object



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

def require!(&block)
  Gisture.logger.info "[gisture] Running #{basename}/#{file.path || file.filename} via the :require strategy"
  required = require tempfile.path
  unlink_tempfile
  block_given? ? yield : required
end

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

Returns:

  • (Boolean)


58
59
60
# File 'lib/gisture/file.rb', line 58

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



39
40
41
42
43
44
45
46
# File 'lib/gisture/file.rb', line 39

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


48
49
50
51
# File 'lib/gisture/file.rb', line 48

def unlink_tempfile
  tempfile.unlink
  @tempfile = nil
end