Module: AssetHelpers

Defined in:
spec/spec_helper.rb

Overview

A module that provides helper methods for asset management within the application.

The AssetHelpers module encapsulates functionality related to handling and processing application assets, such as CSS, JavaScript, and image files. It offers utilities for managing asset paths, generating URLs, and performing operations on assets during the application’s runtime.

Instance Method Summary collapse

Instance Method Details

#asset(name) ⇒ String

The asset method constructs and returns the full path to an asset file.

This method takes a filename argument and combines it with the assets directory located within the same directory as the calling file, returning the complete path to that asset.

Parameters:

  • name (String)

    the name of the asset file

Returns:

  • (String)

    the full path to the asset file



36
37
38
# File 'spec/spec_helper.rb', line 36

def asset(name)
  File.join(__dir__, 'assets', name)
end

#asset_content(name) ⇒ String

Reads and returns the content of an asset file from the assets directory.

Parameters:

  • name (String)

    the name of the asset file to read

Returns:

  • (String)

    the content of the asset file as a string



45
46
47
# File 'spec/spec_helper.rb', line 45

def asset_content(name)
  File.read(File.join(__dir__, 'assets', name))
end

#asset_io(name) {|io| ... } ⇒ File?

The asset_io method retrieves an IO object for a specified asset file.

This method constructs the path to an asset file within the assets directory and returns an IO object representing that file. If a block is provided, it yields the IO object to the block and ensures the file is properly closed after the block executes.

block is provided and the block does not return a value

Parameters:

  • name (String)

    the name of the asset file to retrieve

Yields:

  • (io)

    yields the IO object for the asset file to the provided block

Returns:

  • (File, nil)

    returns the IO object for the asset file, or nil if a



62
63
64
65
66
67
68
69
70
71
72
73
# File 'spec/spec_helper.rb', line 62

def asset_io(name, &block)
  io = File.new(File.join(__dir__, 'assets', name))
  if block
    begin
      block.call(io)
    ensure
      io.close
    end
  else
    io
  end
end

#asset_json(name) ⇒ Object

The asset_json method reads and parses a JSON asset file.

This method retrieves an asset by name, reads its contents from the filesystem, and then parses the resulting string as JSON, returning the parsed data structure.

Parameters:

  • name (String)

    the name of the asset to retrieve and parse

Returns:

  • (Object)

    the parsed JSON data structure, typically a Hash or Array



84
85
86
# File 'spec/spec_helper.rb', line 84

def asset_json(name)
  JSON(JSON(File.read(asset(name))))
end