Module: Parcel
- Defined in:
- lib/parcel.rb,
lib/parcel/has_parcel.rb,
lib/parcel/zip_file_repository.rb
Defined Under Namespace
Modules: HasParcel
Classes: ZipFileRepository
Class Method Summary
collapse
Class Method Details
.create_repository(owner, options, source = nil) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/parcel.rb', line 61
def self.create_repository(owner, options, source = nil)
repo = if options[:class] == :zip
ZipFileRepository.new(owner, options)
else
nil
end
raise ArgumentError, "Repository not supported: #{options[:class]}" if repo.nil?
if source.is_a?(String) && source.starts_with?("/")
repo.import(File.open(source, "r"))
elsif source.is_a?(String)
repo.import_data(source)
elsif source.respond_to?(:to_file)
repo.import(source.to_file)
elsif source.respond_to?(:read)
repo.import(source)
elsif File.exist?(repo.commit_path)
repo.import(File.open(repo.commit_path, "r"))
end
repo
end
|
.interpolate_path(path, options, reference) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/parcel.rb', line 39
def self.interpolate_path(path, options, reference)
new_path = path.to_s
framework_opts = Hash.new
if defined?(Rails)
framework_opts[:rails_root] = Rails.root
framework_opts[:id] = reference.id if reference.is_a?(ActiveRecord::Base)
end
framework_opts[:id] ||= reference.object_id
merge_opts = framework_opts.merge({ :class_name => underscore(reference.class.name) }).merge(options)
merge_opts[:id] = ("%08d" % merge_opts[:id].to_s.to_i).scan(/..../).join("/")
merge_opts.to_a.sort_by { |(k,v)| -k.to_s.length }.each do |(k,v)|
new_path = new_path.gsub(":#{k}", v.to_s)
end
new_path
end
|
.storage_root ⇒ Object
8
9
10
|
# File 'lib/parcel.rb', line 8
def self.storage_root
@options[:storage_root] || "./:class_name/:id"
end
|
.storage_root=(value) ⇒ Object
12
13
14
|
# File 'lib/parcel.rb', line 12
def self.storage_root=(value)
@options[:storage_root] = value
end
|
.temp_path(filename) ⇒ Object
16
17
18
19
20
21
|
# File 'lib/parcel.rb', line 16
def self.temp_path(filename)
date_stamp = "%04d_%02d_%02d" % [Time.now.year, Time.now.month, Time.now.day]
path = File.join(temp_root, date_stamp, "#{Time.now.to_i}_#{rand(999999)}_#{filename}")
FileUtils.mkdir_p( File.dirname(path) )
path
end
|
.temp_root ⇒ Object
27
28
29
|
# File 'lib/parcel.rb', line 27
def self.temp_root
@options[:temp_root] || "/tmp/parcel"
end
|
.temp_root=(value) ⇒ Object
23
24
25
|
# File 'lib/parcel.rb', line 23
def self.temp_root=(value)
@options[:temp_root] = value
end
|
.underscore(camel_cased_word) ⇒ Object
31
32
33
34
35
36
37
|
# File 'lib/parcel.rb', line 31
def self.underscore(camel_cased_word)
camel_cased_word.to_s.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
|