Class: Cheepub::AssetStore
- Inherits:
-
Object
- Object
- Cheepub::AssetStore
- Defined in:
- lib/cheepub/asset_store.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, val) ⇒ Object
- #asset_file_path(content, dir, ext) ⇒ Object
- #clear ⇒ Object
- #each_relpath_and_content(&proc) ⇒ Object
-
#initialize(asset_dir, base_dir) ⇒ AssetStore
constructor
A new instance of AssetStore.
- #store(src, dir) ⇒ Object
Constructor Details
#initialize(asset_dir, base_dir) ⇒ AssetStore
Returns a new instance of AssetStore.
10 11 12 13 14 |
# File 'lib/cheepub/asset_store.rb', line 10 def initialize(asset_dir, base_dir) @store = {} @asset_dir = Pathname.new(asset_dir) @base_dir = Pathname.new(base_dir || ".") end |
Instance Method Details
#[](key) ⇒ Object
36 37 38 |
# File 'lib/cheepub/asset_store.rb', line 36 def [](key) @store[key] end |
#[]=(key, val) ⇒ Object
40 41 42 |
# File 'lib/cheepub/asset_store.rb', line 40 def []=(key,val) @store[key] = val end |
#asset_file_path(content, dir, ext) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/cheepub/asset_store.rb', line 52 def asset_file_path(content, dir, ext) hash_val = Digest::SHA256.digest(content) basename = Base64.urlsafe_encode64(hash_val).gsub(/=/,"") path = @asset_dir + dir + "#{basename}#{ext}" path end |
#clear ⇒ Object
32 33 34 |
# File 'lib/cheepub/asset_store.rb', line 32 def clear FileUtils.remove_entry_secure(@asset_dir.to_s) end |
#each_relpath_and_content(&proc) ⇒ Object
44 45 46 47 48 49 50 |
# File 'lib/cheepub/asset_store.rb', line 44 def each_relpath_and_content(&proc) @store.each do |key, val| path = val.relative_path_from(@asset_dir) content = File.read(val) yield path.to_s, content end end |
#store(src, dir) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/cheepub/asset_store.rb', line 16 def store(src, dir) ext = File.extname(src) if src =~ /^(https?:|\/)/ img_content = open(src){|f| f.read} else img_content = open(File.join(@base_dir, src)){|f| f.read} end img_path = asset_file_path(img_content, dir, ext) if !@store[src] @store[src] = img_path FileUtils.mkdir_p(img_path.dirname.to_s) File.write(img_path.to_s, img_content) end img_path.relative_path_from(@asset_dir) end |