Class: CssCompare::CSS::Component::Property
- Defined in:
- lib/css_compare/css/component/property.rb
Overview
Represents a CSS property tied to a specific selector. It can have several values based on the conditions specified by the @media queries.
Instance Attribute Summary collapse
-
#name ⇒ String
readonly
Name of the property.
-
#values ⇒ Hash{String=>Value}
Key-value pair of property values.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Checks whether two properties are equal.
-
#complex? ⇒ Boolean
Whether the property is a complex one.
-
#deep_copy ⇒ Property
Creates a deep copy of this property.
-
#initialize(node, conditions) ⇒ Property
constructor
A new instance of Property.
-
#merge(property) ⇒ Void
Merges the property with another one.
-
#set_value(val, condition = 'all') ⇒ Void
Replaces the original value of the property under a certain media query condition.
-
#to_json ⇒ Hash
Creates the JSON representation of this property.
-
#value(condition = 'all') ⇒ #to_s
Returns the property’s value taken under a certain circumstance.
Methods inherited from Base
Constructor Details
#initialize(node, conditions) ⇒ Property
An optimization has been done here, as well. If there are several conditions, all should be paired with the same value but not the same object, since the value can be overridden later in the process of the stylesheet evaluation. A clone of the value is paired with each of the conditions.
Returns a new instance of Property.
28 29 30 31 32 33 |
# File 'lib/css_compare/css/component/property.rb', line 28 def initialize(node, conditions) @name = node.resolved_name @values = {} value = Value.new(node) conditions.each { |c| set_value(value.clone, c) } end |
Instance Attribute Details
#name ⇒ String (readonly)
Returns name of the property.
9 10 11 |
# File 'lib/css_compare/css/component/property.rb', line 9 def name @name end |
#values ⇒ Hash{String=>Value}
Key-value pair of property values.
The key represents the condition set by a media query and the value is the actual value of the property.
17 18 19 |
# File 'lib/css_compare/css/component/property.rb', line 17 def values @values end |
Instance Method Details
#==(other) ⇒ Boolean
Checks whether two properties are equal.
Properties are equal only if both contain the same keys and the values under those keys are also equal.
44 45 46 47 |
# File 'lib/css_compare/css/component/property.rb', line 44 def ==(other) return super(@values, other.values) unless font? equals?(@values, other.values) end |
#complex? ⇒ Boolean
Whether the property is a complex one. One property, that can be separated into smaller chunks of elementary properties.
Example:
`border: 1px solid black` => `{
border-width: 1px;
border-style: solid;
border-color: black;
}`
111 112 113 114 |
# File 'lib/css_compare/css/component/property.rb', line 111 def complex? COMPLEX_PROPERTIES.include?(@name) false end |
#deep_copy ⇒ Property
Creates a deep copy of this property.
119 120 121 122 123 124 |
# File 'lib/css_compare/css/component/property.rb', line 119 def deep_copy copy = dup copy.values = {} @values.each { |k, v| copy.values[k] = v.clone } copy end |
#merge(property) ⇒ Void
Merges the property with another one.
53 54 55 |
# File 'lib/css_compare/css/component/property.rb', line 53 def merge(property) property.values.each { |cond, v| set_value(v, cond) } end |
#set_value(val, condition = 'all') ⇒ Void
Replaces the original value of the property under a certain media query condition.
If the value does not exist under the specified condition, it is added into the set of property values. Otherwise, it rewrites the current value if it’s not set as important. However, if the replacing value is also set as important, the current value will be replaced with the new one.
If the condition does not exist but there is an important global value assigned to the property, the replacing value will be used only, if it’s set to important, too. Otherwise, the global value should be cloned in there.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/css_compare/css/component/property.rb', line 75 def set_value(val, condition = 'all') global_value = @values['all'] val_to_replace = value(condition) # Check, whether the condition exists if val_to_replace @values[condition].value = val if val.important? || !val_to_replace.important? else @values[condition] = if global_value && global_value.important? val.important? ? val : global_value.clone else val end end end |
#to_json ⇒ Hash
Creates the JSON representation of this property.
129 130 131 |
# File 'lib/css_compare/css/component/property.rb', line 129 def to_json @values.inject({}) { |result, (k, v)| result.update(k => v.to_s) } end |
#value(condition = 'all') ⇒ #to_s
Returns the property’s value taken under a certain circumstance
94 95 96 |
# File 'lib/css_compare/css/component/property.rb', line 94 def value(condition = 'all') @values[condition] end |