Class: CssCompare::CSS::Component::Selector
- Defined in:
- lib/css_compare/css/component/selector.rb
Overview
Represents one simple selector sequence, like .a.b.c > div.f:first-child.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#name ⇒ String
Selector’s name.
-
#properties ⇒ Hash{String => Component::Property}
Hash of the selector’s properties.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Checks, whether two selector are equal.
-
#add_property(prop, deep_copy = false) ⇒ Void
Adds a property to the existing set of properties of this selector.
-
#deep_copy(name = @name) ⇒ Selector
Creates a deep copy of this selector.
-
#initialize(name, properties, conditions) ⇒ Selector
constructor
A new instance of Selector.
-
#merge(other) ⇒ Void
Combines the selector’s properties with the properties of another selector.
-
#to_json ⇒ Hash
Creates the JSON representation of this selector.
Methods inherited from Base
Constructor Details
#initialize(name, properties, conditions) ⇒ Selector
Returns a new instance of Selector.
22 23 24 25 26 |
# File 'lib/css_compare/css/component/selector.rb', line 22 def initialize(name, properties, conditions) @name = name.strip @properties = {} process_properties(properties, conditions) end |
Instance Attribute Details
#name ⇒ String
Returns selector’s name.
10 11 12 |
# File 'lib/css_compare/css/component/selector.rb', line 10 def name @name end |
#properties ⇒ Hash{String => Component::Property}
Hash of the selector’s properties. Could have been an array but this structure has been chosen, so the properties’ lookup gets optimized.
17 18 19 |
# File 'lib/css_compare/css/component/selector.rb', line 17 def properties @properties end |
Instance Method Details
#==(other) ⇒ Boolean
Checks, whether two selector are equal.
Two selectors are equal only if they both have declared the same properties and they are also equal.
35 36 37 |
# File 'lib/css_compare/css/component/selector.rb', line 35 def ==(other) super(@properties, other.properties) end |
#add_property(prop, deep_copy = false) ⇒ Void
Adds a property to the existing set of properties of this selector.
If the property does not exist, it will be added. Otherwise the values of the properties will be merged.
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/css_compare/css/component/selector.rb', line 63 def add_property(prop, deep_copy = false) name = prop.name if @properties[name] @properties[name].merge(prop) else @properties[name] = if deep_copy prop.deep_copy else prop end end end |
#deep_copy(name = @name) ⇒ Selector
Creates a deep copy of this selector.
81 82 83 84 85 86 87 |
# File 'lib/css_compare/css/component/selector.rb', line 81 def deep_copy(name = @name) copy = dup copy.name = name.strip copy.properties = {} @properties.each { |k, v| copy.properties[k] = v.deep_copy } copy end |
#merge(other) ⇒ Void
Combines the selector’s properties with the properties of another selector.
45 46 47 48 49 |
# File 'lib/css_compare/css/component/selector.rb', line 45 def merge(other) other.properties.each do |_, prop| add_property(prop, true) end end |
#to_json ⇒ Hash
Creates the JSON representation of this selector.
92 93 94 95 96 97 98 99 |
# File 'lib/css_compare/css/component/selector.rb', line 92 def to_json key = @name.to_sym json = { key => {} } @properties.inject(json[key]) do |result, (k, v)| result.update(k => v.to_json) end json end |