Class: Cataract::MediaQuery

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

Overview

MediaQuery represents a CSS media query constraint.

Media queries are stored in the Stylesheet and referenced by Rules via media_query_id. This allows efficient tracking of which rules apply to which media contexts.

Examples:

Access media query properties

mq = MediaQuery.new(0, :screen, "(min-width: 768px)")
mq.id #=> 0
mq.type #=> :screen
mq.conditions #=> "(min-width: 768px)"
mq.text #=> "screen and (min-width: 768px)"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#conditionsString?

Additional conditions like "(min-width: 768px)", or nil if none

Returns:

  • (String, nil)

    the current value of conditions



19
20
21
# File 'lib/cataract/media_query.rb', line 19

def conditions
  @conditions
end

#idInteger

Unique identifier for this media query within the stylesheet

Returns:

  • (Integer)

    the current value of id



19
20
21
# File 'lib/cataract/media_query.rb', line 19

def id
  @id
end

#typeSymbol

Primary media type (:screen, :print, :all, etc.)

Returns:

  • (Symbol)

    the current value of type



19
20
21
# File 'lib/cataract/media_query.rb', line 19

def type
  @type
end

Class Method Details

.make(id:, type:, conditions: nil) ⇒ MediaQuery

Create a MediaQuery with keyword arguments for readability.

Examples:

Create a simple media query

MediaQuery.make(id: 0, type: :screen, conditions: nil)

Create a media query with conditions

MediaQuery.make(id: 1, type: :screen, conditions: "(min-width: 768px)")

Parameters:

  • id (Integer)

    Unique ID for this media query

  • type (Symbol)

    Primary media type

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

    Optional conditions

Returns:



32
33
34
# File 'lib/cataract/media_query.rb', line 32

def self.make(id:, type:, conditions: nil)
  new(id, type, conditions)
end

Instance Method Details

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

Compare media queries for equality based on type and conditions.

Two media queries are equal if they have the same type and conditions. IDs are not considered since they're internal identifiers.

Parameters:

  • other (Object)

    Object to compare with

Returns:

  • (Boolean)

    true if media queries match



65
66
67
68
69
70
71
72
# File 'lib/cataract/media_query.rb', line 65

def ==(other)
  case other
  when MediaQuery
    type == other.type && conditions == other.conditions
  else
    false
  end
end

#hashInteger

Generate hash code for this media query.

Hash is based on type and conditions to match equality semantics.

Returns:

  • (Integer)

    hash code



80
81
82
# File 'lib/cataract/media_query.rb', line 80

def hash
  [self.class, type, conditions].hash
end

#inspectString

Get detailed inspection string.

Returns:

  • (String)

    Inspection string



94
95
96
# File 'lib/cataract/media_query.rb', line 94

def inspect
  "#<MediaQuery id=#{id} type=#{type.inspect} conditions=#{conditions.inspect}>"
end

#textString

Get the full media query text.

Reconstructs the media query string from type and conditions.

Examples:

Simple media query

mq = MediaQuery.new(0, :screen, nil)
mq.text #=> "screen"

Media query with conditions

mq = MediaQuery.new(0, :screen, "(min-width: 768px)")
mq.text #=> "screen and (min-width: 768px)"

Returns:

  • (String)

    Full media query text



49
50
51
52
53
54
55
56
# File 'lib/cataract/media_query.rb', line 49

def text
  if conditions
    # If type is :all, just return conditions (don't say "all and ...")
    type == :all ? conditions : "#{type} and #{conditions}"
  else
    type.to_s
  end
end

#to_sString

Get a human-readable representation.

Returns:

  • (String)

    String representation



87
88
89
# File 'lib/cataract/media_query.rb', line 87

def to_s
  text
end