Class: Embulk::PackageData

Inherits:
Object
  • Object
show all
Defined in:
lib/embulk/data/package_data.rb

Instance Method Summary collapse

Constructor Details

#initialize(base_name, dest_dir, erb_binding = nil) ⇒ PackageData

Returns a new instance of PackageData.



4
5
6
7
8
9
# File 'lib/embulk/data/package_data.rb', line 4

def initialize(base_name, dest_dir, erb_binding=nil)
  require 'fileutils'
  @base_name = base_name
  @dest_dir = dest_dir
  @erb_binding = erb_binding
end

Instance Method Details

#bincontent(src) ⇒ Object



19
20
21
# File 'lib/embulk/data/package_data.rb', line 19

def bincontent(src)
  File.binread(path(src))
end

#content(src) ⇒ Object



15
16
17
# File 'lib/embulk/data/package_data.rb', line 15

def content(src)
  File.read(path(src))
end

#cp(src, dest_name) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/embulk/data/package_data.rb', line 28

def cp(src, dest_name)
  dest = dest_path_message(dest_name)
  File.open(dest, "wb") do |dst_io|
    File.open(path(src), "rb") do |src_io|
      FileUtils.copy_stream src_io, dst_io
    end
  end
end

#cp_erb(src, dest_name) ⇒ Object



37
38
39
40
# File 'lib/embulk/data/package_data.rb', line 37

def cp_erb(src, dest_name)
  dest = dest_path_message(dest_name)
  File.open(dest, "wb") {|f| f.write erb(src) }
end

#dest_path(dest_name) ⇒ Object



42
43
44
# File 'lib/embulk/data/package_data.rb', line 42

def dest_path(dest_name)
  File.join(@dest_dir, *dest_name.split('/'))
end

#dest_path_message(dest_name) ⇒ Object



46
47
48
49
50
51
# File 'lib/embulk/data/package_data.rb', line 46

def dest_path_message(dest_name)
  path = dest_path(dest_name)
  puts "  Creating #{path}"
  FileUtils.mkdir_p File.dirname(path)
  path
end

#erb(src) ⇒ Object



23
24
25
26
# File 'lib/embulk/data/package_data.rb', line 23

def erb(src)
  require 'erb'
  ERB.new(content(src), nil, '%').result(@erb_binding)
end

#path(src) ⇒ Object



11
12
13
# File 'lib/embulk/data/package_data.rb', line 11

def path(src)
  Embulk.lib_path("embulk/data/#{@base_name}/#{src}")
end

#set_executable(dest_name) ⇒ Object



53
54
55
56
# File 'lib/embulk/data/package_data.rb', line 53

def set_executable(dest_name)
  dest = dest_path(dest_name)
  File.chmod(File.stat(dest).mode | 0111, dest)
end