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 = nil) ⇒ 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) (defaults to: nil)

    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 = nil)
  File.join(*[__dir__, 'assets', name ].compact)
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



54
55
56
# File 'spec/spec_helper.rb', line 54

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.

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 block is provided and the block does not return a value



71
72
73
74
75
76
77
78
79
80
81
82
# File 'spec/spec_helper.rb', line 71

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



93
94
95
# File 'spec/spec_helper.rb', line 93

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

#asset_pathname(name = nil) ⇒ Pathname

The asset_pathname method returns a Pathname object for the given asset name.

Parameters:

  • name (String) (defaults to: nil)

    the name of the asset file, optional

Returns:

  • (Pathname)

    the Pathname object for the asset file



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

def asset_pathname(name = nil)
  Pathname.new(asset(name))
end