Class: Cataract::ImportStatement

Inherits:
Struct
  • Object
show all
Defined in:
lib/cataract/import_statement.rb,
lib/cataract/import_statement.rb

Overview

Represents a CSS @import statement

They can later be resolved by the ImportResolver to fetch and inline the imported CSS.

Per CSS spec, @import must appear before all rules except @charset and @layer. Any @import that appears after a style rule is invalid and will be ignored with a warning.

Examples:

Basic import

@import "styles.css";
# => ImportStatement(url: "styles.css", media: nil)

Import with media query

@import "mobile.css" screen and (max-width: 768px);
# => ImportStatement(url: "mobile.css", media_query_id: 0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#idInteger

The import's position in the source (0-indexed)

Returns:

  • (Integer)

    the current value of id



25
26
27
# File 'lib/cataract/import_statement.rb', line 25

def id
  @id
end

#mediaString?

The media query string (e.g., "print", "screen and (max-width: 768px)"), or nil

Returns:

  • (String, nil)

    the current value of media



25
26
27
# File 'lib/cataract/import_statement.rb', line 25

def media
  @media
end

#media_query_idInteger?

The MediaQuery ID, or nil if no media query

Returns:

  • (Integer, nil)

    the current value of media_query_id



25
26
27
# File 'lib/cataract/import_statement.rb', line 25

def media_query_id
  @media_query_id
end

#resolvedBoolean

Whether this import has been resolved/processed

Returns:

  • (Boolean)

    the current value of resolved



25
26
27
# File 'lib/cataract/import_statement.rb', line 25

def resolved
  @resolved
end

#urlString

The URL to import (without quotes or url() wrapper)

Returns:

  • (String)

    the current value of url



25
26
27
# File 'lib/cataract/import_statement.rb', line 25

def url
  @url
end

Class Method Details

.make(id:, url:, media: nil, media_query_id: nil, resolved: false) ⇒ ImportStatement

Factory method for creating ImportStatement in tests. Uses keyword arguments to avoid positional parameter confusion.

Examples:

Create an import with keyword arguments

ImportStatement.make(
  id: 0,
  url: 'styles.css',
  media: nil,
  media_query_id: nil,
  resolved: false
)

Parameters:

  • id (Integer)

    Import ID (position in source)

  • url (String)

    URL to import

  • media (String, nil) (defaults to: nil)

    Media query string (e.g., "print", "screen and (max-width: 768px)")

  • media_query_id (Integer, nil) (defaults to: nil)

    MediaQuery ID

  • resolved (Boolean) (defaults to: false)

    Whether import has been resolved

Returns:



46
47
48
# File 'lib/cataract/import_statement.rb', line 46

def self.make(id:, url:, media: nil, media_query_id: nil, resolved: false)
  new(id, url, media, media_query_id, resolved)
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Compare two ImportStatement objects for equality. Two imports are equal if they have the same URL and media query. The import ID is ignored as it's an implementation detail.

Parameters:

  • other (Object)

    Object to compare with

Returns:

  • (Boolean)

    true if equal, false otherwise



56
57
58
59
60
61
# File 'lib/cataract/import_statement.rb', line 56

def ==(other)
  return false unless other.is_a?(ImportStatement)

  # Compare by media string (for unparsed imports) or media_query_id (for resolved imports)
  url == other.url && media == other.media
end

#hashInteger

Generate hash code for ImportStatement. Uses URL and media string (ignores import ID position).

Returns:

  • (Integer)

    Hash code



69
70
71
# File 'lib/cataract/import_statement.rb', line 69

def hash
  [url, media].hash
end