Class: Parse::File

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ File

Returns a new instance of File.



6
7
8
9
10
# File 'lib/parse/file.rb', line 6

def initialize hash
  @raw_hash = hash
  @name = hash['name']
  @url = hash['url']
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/parse/file.rb', line 4

def name
  @name
end

#urlObject

Returns the value of attribute url.



4
5
6
# File 'lib/parse/file.rb', line 4

def url
  @url
end

Instance Method Details

#inspectObject



29
30
31
32
33
34
# File 'lib/parse/file.rb', line 29

def inspect
  data, @data = @data, '..snip..'
  ret = super
  @data = data
  ret
end

#load(&block) ⇒ Object



12
13
14
15
# File 'lib/parse/file.rb', line 12

def load &block
  open @url do |data| @data = data.read end unless @data
  block.call @data
end

#store(filepath = nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/parse/file.rb', line 17

def store filepath=nil
  filepath ||= @name
  raise 'filepath is mandatory' unless filepath

  FileUtils.mkdir_p ::File.dirname(filepath)
  load do |data|
    open filepath, 'wb' do |file|
      file.write data
    end
  end
end