Class: Nucleon::Util::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/core/util/package.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Package


Constructor / Destructor



9
10
11
12
13
14
15
16
# File 'lib/core/util/package.rb', line 9

def initialize(options = {})
  if options.is_a?(String)
    @data = Config.new
    decode(options)  
  else
    @data = Config.ensure(options)
  end
end

Instance Attribute Details

#dataObject (readonly)


Property accessors / modifiers



21
22
23
# File 'lib/core/util/package.rb', line 21

def data
  @data
end

Instance Method Details

#add_file(file, target_path = nil, perm = 0600) ⇒ Object




36
37
38
39
40
41
42
43
44
45
# File 'lib/core/util/package.rb', line 36

def add_file(file, target_path = nil, perm = 0600)    
  target_path = file if target_path.nil?
  file        = File.expand_path(file)
  
  if File.exists?(file)
    content = Disk.read(file)
    data.set([ :files, target_path ], { :perm => perm, :content => content }) if content
  end
  self
end

#add_files(base_path, file_glob, target_path = nil, perm = 0600) ⇒ Object




49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/core/util/package.rb', line 49

def add_files(base_path, file_glob, target_path = nil, perm = 0600)
  target_path = base_path if target_path.nil?
  curr_dir    = Dir.pwd
  
  Dir.chdir(File.expand_path(base_path))
  Dir.glob(file_glob.gsub(/^[\/\\]+/, '')) do |file|
    content = Disk.read(file)
    
    if content
      data.set([ :dir, target_path, file ], { :perm => perm, :content => content })
    end
  end
  Dir.chdir(curr_dir)
  self      
end

#decode(encoded_string) ⇒ Object



30
31
32
# File 'lib/core/util/package.rb', line 30

def decode(encoded_string)
  data.import(Data.symbol_map(Data.parse_json(Base64.decode64(encoded_string))))  
end

#encodeObject


Operations



26
27
28
# File 'lib/core/util/package.rb', line 26

def encode
  Base64.encode64(Data.to_json(data.export, false))
end

#extract(base_path) ⇒ Object




67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/core/util/package.rb', line 67

def extract(base_path)
  success = true
  
  data.get_hash(:files).each do |target_path, info|
    file    = File.join(base_path.to_s, target_path.to_s)
    perm    = info[:perm]
    content = info[:content]
    
    FileUtils.mkdir_p(File.dirname(file))      
    success = false unless Disk.write(file, content) && File.chmod(perm, file)
  end
  
  data.get_hash(:dir).each do |target_path, files|
    files.each do |file, info|
      file    = File.join(base_path.to_s, target_path.to_s, file.to_s)
      perm    = info[:perm]
      content = info[:content]
      
      FileUtils.mkdir_p(File.dirname(file))
      success = false unless Disk.write(file, content) && File.chmod(perm, file)
    end      
  end
  success
end