Class: Writefully::Asset

Inherits:
Object
  • Object
show all
Defined in:
lib/writefully/asset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index) ⇒ Asset

Returns a new instance of Asset.



5
6
7
8
9
# File 'lib/writefully/asset.rb', line 5

def initialize(index)
  base_path = [Writefully.options[:content], index[:site], index[:resource], index[:slug]]
  @path = File.join(base_path, 'assets')
  @endpoint = File.join(index[:site], index[:resource], index[:slug], 'assets')
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



3
4
5
# File 'lib/writefully/asset.rb', line 3

def endpoint
  @endpoint
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/writefully/asset.rb', line 3

def path
  @path
end

Instance Method Details

#convert_for(content) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/writefully/asset.rb', line 23

def convert_for content
  if content.is_a?(String)
    content.gsub(regex, url(Writefully::Storage.endpoint))
  elsif content.is_a?(Hash)
    content.inject({}) do |h, (k, v)| 
      h[k] = v.gsub(regex, url(Writefully::Storage.endpoint)); h 
    end 
  end
end

#namesObject



11
12
13
# File 'lib/writefully/asset.rb', line 11

def names
  Dir.chdir(path) { Dir.glob('*') }
end

#regexObject



15
16
17
# File 'lib/writefully/asset.rb', line 15

def regex
  ::Regexp.new('assets\/')
end

#url(storage_endpoint) ⇒ Object



19
20
21
# File 'lib/writefully/asset.rb', line 19

def url storage_endpoint
  File.join(storage_endpoint, endpoint, '/')
end