Class: Gisture::Gist

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

Constant Summary collapse

STRATEGIES =
[:eval, :load, :require]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#gist_idObject (readonly)

Returns the value of attribute gist_id.



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

def gist_id
  @gist_id
end

#strategyObject

Returns the value of attribute strategy.



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

def strategy
  @strategy
end

Class Method Details

.run!(gist_id: nil, strategy: nil, filename: nil, version: nil, &block) ⇒ Object



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

def self.run!(gist_id: nil, strategy: nil, filename: nil, version: nil, &block)
  new(gist_id: gist_id, strategy: strategy, filename: filename, version: version).run!(&block)
end

Instance Method Details

#eval!(&block) ⇒ Object



29
30
31
32
33
# File 'lib/gisture/gist.rb', line 29

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

#gistObject



42
43
44
45
46
47
48
49
50
# File 'lib/gisture/gist.rb', line 42

def gist
  @gist ||= begin
    if @version.nil?
      github.gists.get(gist_id)
    else
      github.gists.version(gist_id, @version)
    end
  end
end

#gist_fileObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gisture/gist.rb', line 52

def gist_file
  return @gist_file unless @gist_file.nil?

  if gist.files.count > 1
    raise ArgumentError, "You must specify a filename if your gist contains more than one file" if filename.nil?
    gist.files.each do |file|
      @gist_file = file if file[0] == filename
    end
    raise ArgumentError, "The filename '#{filename}' was not found in the list of files for the gist '#{gist_id}'" if @gist_file.nil?
  else
    @gist_file = gist.files.first
  end

  @gist_file
end

#githubObject



35
36
37
38
39
40
# File 'lib/gisture/gist.rb', line 35

def github
  @github ||= begin
    github_config = Gisture::GITHUB_CONFIG_OPTS.map { |key| [key, Gisture.configuration.send(key)] }.to_h
    Github.new(github_config)
  end
end

#load!(&block) ⇒ Object



23
24
25
26
27
# File 'lib/gisture/gist.rb', line 23

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

#rawObject



68
69
70
# File 'lib/gisture/gist.rb', line 68

def raw
  gist_file[1].content
end

#require!(&block) ⇒ Object



17
18
19
20
21
# File 'lib/gisture/gist.rb', line 17

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

#run!(&block) ⇒ Object



13
14
15
# File 'lib/gisture/gist.rb', line 13

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

#tempfileObject



77
78
79
80
81
82
83
84
# File 'lib/gisture/gist.rb', line 77

def tempfile
  @tempfile ||= begin
    file = Tempfile.new([gist_id, File.extname(gist_file[0])], Gisture.configuration.tmpdir)
    file.write(raw)
    file.close
    file
  end
end

#to_hObject



86
87
88
89
90
# File 'lib/gisture/gist.rb', line 86

def to_h
  { gist_id: gist_id,
    strategy: strategy,
    filename: filename }
end