Class: Nucleon::Util::Package
- Inherits:
-
Object
- Object
- Nucleon::Util::Package
- Defined in:
- lib/core/util/package.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
—————————————————————————– Property accessors / modifiers.
Instance Method Summary collapse
-
#add_file(file, target_path = nil, perm = 0600) ⇒ Object
—.
-
#add_files(base_path, file_glob, target_path = nil, perm = 0600) ⇒ Object
—.
- #decode(encoded_string) ⇒ Object
-
#encode ⇒ Object
—————————————————————————– Operations.
-
#extract(base_path) ⇒ Object
—.
-
#initialize(options = {}) ⇒ Package
constructor
—————————————————————————– Constructor / Destructor.
Constructor Details
Instance Attribute Details
#data ⇒ Object (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.(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.(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 |
#encode ⇒ Object
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 |