Class: Gisture::Gist
- Inherits:
-
Object
- Object
- Gisture::Gist
- Defined in:
- lib/gisture/gist.rb
Constant Summary collapse
- STRATEGIES =
[:eval, :load, :require]
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#gist_id ⇒ Object
readonly
Returns the value of attribute gist_id.
-
#strategy ⇒ Object
Returns the value of attribute strategy.
Class Method Summary collapse
Instance Method Summary collapse
- #eval!(&block) ⇒ Object
- #gist ⇒ Object
- #gist_file ⇒ Object
- #github ⇒ Object
- #load!(&block) ⇒ Object
- #raw ⇒ Object
- #require!(&block) ⇒ Object
- #run!(&block) ⇒ Object
- #tempfile ⇒ Object
- #to_h ⇒ Object
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
5 6 7 |
# File 'lib/gisture/gist.rb', line 5 def filename @filename end |
#gist_id ⇒ Object (readonly)
Returns the value of attribute gist_id.
5 6 7 |
# File 'lib/gisture/gist.rb', line 5 def gist_id @gist_id end |
#strategy ⇒ Object
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 |
#gist ⇒ Object
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_file ⇒ Object
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 |
#github ⇒ Object
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 |
#raw ⇒ Object
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 |
#tempfile ⇒ Object
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_h ⇒ Object
86 87 88 89 90 |
# File 'lib/gisture/gist.rb', line 86 def to_h { gist_id: gist_id, strategy: strategy, filename: filename } end |