Class: Stylish::Background
- Inherits:
-
Declaration
- Object
- Declaration
- Stylish::Background
- Defined in:
- lib/stylish/background.rb
Overview
The Background class is a specialised kind of Declaration, geared towards dealing with the oddities of the background family of declarations, which can exist in both long- and shorthand forms.
For example, these longhand background declarations
background-color: #999;
background-image: url('bg.png');
background-repeat: repeat-x;
could be compressed into a single shorthand declaration
background: #999 url('bg.png') repeat-x;
The Background class allows for easy conversion between these forms. It defaults to the longhand versions, allowing rules with stronger selector weighting to only override specific parts of other rules’ background declarations.
Constant Summary collapse
- PROPERTIES =
[ [:color, "background-color"], [:image, "background-image"], [:repeat, "background-repeat"], [:position, "background-position"], [:attachment, "background-attachment"], [:origin, "background-origin"], [:break, "background-break"], [:compressed]]
- REPEAT_VALUES =
["repeat-x", "repeat-y", "repeat", "space", "round", "no-repeat"]
- ATTACHMENT_VALUES =
["scroll", "fixed", "local"]
- HORIZONTAL_POSITIONS =
["left", "center", "right"]
- VERTICAL_POSITIONS =
["top", "center", "bottom"]
- ORIGIN_VALUES =
["border-box", "padding-box", "content-box"]
- BREAK_VALUES =
["bounding-box", "each-box", "continuous"]
Instance Attribute Summary collapse
-
#attachment ⇒ Object
Returns the value of attribute attachment.
-
#break ⇒ Object
Returns the value of attribute break.
-
#color ⇒ Object
Returns the value of attribute color.
-
#compressed ⇒ Object
Returns the value of attribute compressed.
-
#image ⇒ Object
Returns the value of attribute image.
-
#origin ⇒ Object
Returns the value of attribute origin.
-
#position ⇒ Object
Returns the value of attribute position.
-
#repeat ⇒ Object
Returns the value of attribute repeat.
Attributes included from Formattable
Instance Method Summary collapse
-
#initialize(options) ⇒ Background
constructor
Create a new Background object with the specified properties.
-
#name ⇒ Object
Override Declaration#name, since it’s not compatible with the internals of this class.
-
#name=(val) ⇒ Object
Override Declaration#name=, since it’s not compatible with the internals of this class.
-
#to_s(symbols = {}) ⇒ Object
Generate a string representation of a Background instance.
-
#value(name_and_value = false) ⇒ Object
Override Declaration#value, since it’s not compatible with the internals of this class.
-
#value=(options) ⇒ Object
Override Declaration#value=, since it’s not compatible with the internals of this class.
Constructor Details
#initialize(options) ⇒ Background
Create a new Background object with the specified properties.
50 51 52 53 |
# File 'lib/stylish/background.rb', line 50 def initialize() accept_format(/^\s*%s\s*:\s*%s;\s*$/m, "%s:%s;") self.value = end |
Instance Attribute Details
#attachment ⇒ Object
Returns the value of attribute attachment.
22 23 24 |
# File 'lib/stylish/background.rb', line 22 def end |
#break ⇒ Object
Returns the value of attribute break.
22 23 24 |
# File 'lib/stylish/background.rb', line 22 def break @break end |
#color ⇒ Object
Returns the value of attribute color.
22 23 24 |
# File 'lib/stylish/background.rb', line 22 def color @color end |
#compressed ⇒ Object
Returns the value of attribute compressed.
22 23 24 |
# File 'lib/stylish/background.rb', line 22 def compressed @compressed end |
#image ⇒ Object
Returns the value of attribute image.
22 23 24 |
# File 'lib/stylish/background.rb', line 22 def image @image end |
#origin ⇒ Object
Returns the value of attribute origin.
22 23 24 |
# File 'lib/stylish/background.rb', line 22 def origin @origin end |
#position ⇒ Object
Returns the value of attribute position.
22 23 24 |
# File 'lib/stylish/background.rb', line 22 def position @position end |
#repeat ⇒ Object
Returns the value of attribute repeat.
22 23 24 |
# File 'lib/stylish/background.rb', line 22 def repeat @repeat end |
Instance Method Details
#name ⇒ Object
Override Declaration#name, since it’s not compatible with the internals of this class.
172 173 174 175 176 177 |
# File 'lib/stylish/background.rb', line 172 def name PROPERTIES.reject {|n, p| p.nil? }.map {|n, p| value = self.send(n) p.to_s unless value.nil? }.compact end |
#name=(val) ⇒ Object
Override Declaration#name=, since it’s not compatible with the internals of this class.
181 182 183 |
# File 'lib/stylish/background.rb', line 181 def name=(val) raise NoMethodError, "name= is not defined for Background." end |
#to_s(symbols = {}) ⇒ Object
Generate a string representation of a Background instance.
There are two kinds of representation, each of which have slightly different CSS semantics. If compressed is set to true, this method will produce a shorthand CSS declaration such as the following:
background: #fff url('bg.png') no-repeat 50% 0;
Otherwise it will produce an unordered list of individual background declarations.
217 218 219 220 221 222 223 |
# File 'lib/stylish/background.rb', line 217 def to_s(symbols = {}) if @compressed "background:#{self.value(true).map {|p, v| v }.compact.join(" ")};" else self.value(true).map {|p, v| sprintf(@format, p, v.to_s) }.join(" ") end end |
#value(name_and_value = false) ⇒ Object
Override Declaration#value, since it’s not compatible with the internals of this class.
187 188 189 190 191 192 193 |
# File 'lib/stylish/background.rb', line 187 def value(name_and_value = false) PROPERTIES.reject {|n, p| p.nil? }.map {|n, p| value = self.send(n) next if value.nil? name_and_value ? [p.to_s, value] : value }.compact end |
#value=(options) ⇒ Object
Override Declaration#value=, since it’s not compatible with the internals of this class.
197 198 199 200 201 202 203 204 205 |
# File 'lib/stylish/background.rb', line 197 def value=() unless .is_a? Hash raise ArgumentError, "Argument must be a hash of background properties" end PROPERTIES.each do |name, property| self.send(:"#{name.to_s}=", [name]) if [name] end end |