Class: ChunkyCSS::MediaQuery

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/chunky_css.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(querystr) ⇒ MediaQuery

Returns a new instance of MediaQuery.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/chunky_css.rb', line 86

def initialize(querystr)
  @features = {}
  @type = "all"
  @css_rules = ""

  scanner = StringScanner.new(querystr)

  while !scanner.eos? do
    if scanner.scan RegEx::TYPE
      @type = scanner[1]
    elsif scanner.scan RegEx::FEATURE_WITH_LENGTH
      (key, value) = scanner[1].split(/\s*:\s*/)
      @features[key] = value.gsub(/\s/, '')
    else
      scanner.getch
    end
  end
end

Instance Attribute Details

#css_rulesObject

Returns the value of attribute css_rules.



84
85
86
# File 'lib/chunky_css.rb', line 84

def css_rules
  @css_rules
end

#featuresObject (readonly)

Returns the value of attribute features.



83
84
85
# File 'lib/chunky_css.rb', line 83

def features
  @features
end

#typeObject (readonly)

Returns the value of attribute type.



82
83
84
# File 'lib/chunky_css.rb', line 82

def type
  @type
end

Instance Method Details

#<=>(other) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/chunky_css.rb', line 105

def <=>(other)
  d = @features["max-width"].to_i <=> other.features["max-width"].to_i

  if (d == 0)
    my_min_width, other_min_width = [@features, other.features].map{|f| f["min-width"].to_i}

    d = other_min_width <=> my_min_width
    d *= -1  if [my_min_width, other_min_width].include?(0)
  end

  d
end

#media_descriptionObject



118
119
120
121
122
# File 'lib/chunky_css.rb', line 118

def media_description
  [ @type ].concat( @features.keys.map {|key|
    "(%s:%s)"%[key, @features[key]]
  } ).join(" and ")
end

#to_cssObject



124
125
126
127
128
129
130
# File 'lib/chunky_css.rb', line 124

def to_css
  if @type == "all" && @features.empty?
    @css_rules
  else
    "@media %s{%s}"%[media_description, @css_rules]
  end
end