Class: Ro::Asset

Inherits:
String show all
Includes:
Klass
Defined in:
lib/ro/asset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Klass

included

Methods inherited from String

#html_safe

Constructor Details

#initialize(arg, *args) ⇒ Asset

Returns a new instance of Asset.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ro/asset.rb', line 7

def initialize(arg, *args)
  options = args.last.is_a?(Hash) ? args.pop : {}

  @path = Path.for(arg, *args)

  @node = options.fetch(:node) { Node.for(@path.split('/assets/').first) }

  @relative_path = @path.relative_to(@node.path)

  @name = @relative_path

  @url = @node.url_for(@relative_path)

  super(@path)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/ro/asset.rb', line 5

def name
  @name
end

#nodeObject (readonly)

Returns the value of attribute node.



5
6
7
# File 'lib/ro/asset.rb', line 5

def node
  @node
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/ro/asset.rb', line 5

def path
  @path
end

#relative_pathObject (readonly)

Returns the value of attribute relative_path.



5
6
7
# File 'lib/ro/asset.rb', line 5

def relative_path
  @relative_path
end

#urlObject (readonly)

Returns the value of attribute url.



5
6
7
# File 'lib/ro/asset.rb', line 5

def url
  @url
end

Instance Method Details

#imgObject



29
30
31
32
# File 'lib/ro/asset.rb', line 29

def img
  return unless is_img?
  Ro.image_info(path.to_s)
end

#is_img?Boolean Also known as: is_img

Returns:

  • (Boolean)


23
24
25
# File 'lib/ro/asset.rb', line 23

def is_img?
  @path.file? && Ro.is_image?(@path.basename)
end

#is_src?Boolean Also known as: is_src

Returns:

  • (Boolean)


34
35
36
37
38
# File 'lib/ro/asset.rb', line 34

def is_src?
  key = relative_path.parts
  subdir = key.size > 2 ? key[1] : nil
  !!(subdir == 'src')
end

#sizeObject



51
52
53
# File 'lib/ro/asset.rb', line 51

def size
  stat.size
end

#srcObject



42
43
44
45
# File 'lib/ro/asset.rb', line 42

def src
  return unless is_src?
  Ro.render_src(path, node)
end

#statObject



47
48
49
# File 'lib/ro/asset.rb', line 47

def stat
  @path.stat.size
end