Class: Gisture::File

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

Direct Known Subclasses

ClonedFile, RepoFile

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

#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!(*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"

  # map nils to file path in args to allow easily inserting the filepath wherever
  # makes sense in your executable arguments (i.e. 'ruby', '-v', nil, '--script-arg')
  args.map! { |arg| arg.nil? ? tempfile.path : arg }

  # attempt to apply a default interpreter if nothing was provided
  # TODO create a legit map of default interpreter args and apply it
  args = ['ruby'] if args.empty? && extname == '.rb'
  args = ['node'] if args.empty? && extname == '.js'

  # append the filepath if it was not inserted into the args already
  args << tempfile.path unless args.include?(tempfile.path)

  # make file executable if we're just invoking it directly
  ::File.chmod(0744, tempfile.path) if args.length == 1

  executed = `#{args.join(' ')}`.strip
  block_given? ? yield : executed
end

#extnameObject 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

Returns:

  • (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

#tempfileObject



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


100
101
102
103
# File 'lib/gisture/file.rb', line 100

def unlink_tempfile
  tempfile.unlink
  @tempfile = nil
end