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
-
#asset(name) ⇒ String
The asset method constructs and returns the full path to an asset file.
-
#asset_content(name) ⇒ String
Reads and returns the content of an asset file from the assets directory.
-
#asset_io(name) {|io| ... } ⇒ File?
The asset_io method retrieves an IO object for a specified asset file.
-
#asset_json(name) ⇒ Object
The asset_json method reads and parses a JSON asset file.
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.
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.
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
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.
84 85 86 |
# File 'spec/spec_helper.rb', line 84 def asset_json(name) JSON(JSON(File.read(asset(name)))) end |