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
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ro/asset.rb', line 7

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

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

  # T029: Updated to split on node ID instead of /assets/ segment
  @node = options.fetch(:node) do
    # Try to find node by splitting path
    # In new structure: no /assets/ segment
    # In old structure: /assets/ segment exists
    if @path.to_s.include?('/assets/')
      # Old structure
      Node.for(@path.split('/assets/').first)
    else
      # New structure: find node directory by looking at parent paths
      # Asset path like: /collection/node-id/file.jpg
      # Node path should be: /collection/node-id
      found_node = nil
      node_path = @path.parent
      while node_path && !node_path.basename.to_s.match(/\.(yml|yaml|json|toml)$/)
        # Check if there's a metadata file for this directory
        collection_path = node_path.parent
        node_id = node_path.basename.to_s

        %w[yml yaml json toml].each do |ext|
           = collection_path.join("#{node_id}.#{ext}")
          if .exist?
            root = Root.for(collection_path.parent)
            collection = root.collection_for(collection_path)
            found_node = Node.new(collection, )
            break
          end
        end

        break if found_node
        node_path = node_path.parent
      end

      # Fallback: old behavior
      found_node || Node.for(@path.split('/assets/').first)
    end
  end

  # T030: Updated relative_path calculation for new structure
  # In new structure, path is already relative to node.asset_dir
  # In old structure, need to account for assets/ prefix
  @relative_path = @path.relative_to(@node.asset_dir)

  @name = @relative_path

  # URL needs to include 'assets/' prefix since assets live in assets/ subdirectory
  @url = @node.url_for(Path.relative('assets', @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



69
70
71
72
# File 'lib/ro/asset.rb', line 69

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

#is_img?Boolean Also known as: is_img

Returns:



63
64
65
# File 'lib/ro/asset.rb', line 63

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

#is_src?Boolean Also known as: is_src

Returns:



74
75
76
77
78
79
80
# File 'lib/ro/asset.rb', line 74

def is_src?
  key = relative_path.parts
  # Check if the first directory in the path is 'src'
  # e.g., src/file.js or src/subdir/file.js
  first_dir = key.size >= 2 ? key[0] : nil
  !!(first_dir == 'src')
end

#sizeObject



93
94
95
# File 'lib/ro/asset.rb', line 93

def size
  stat.size
end

#srcObject



84
85
86
87
# File 'lib/ro/asset.rb', line 84

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

#statObject



89
90
91
# File 'lib/ro/asset.rb', line 89

def stat
  @path.stat.size
end