Class: SimpleSelector::Segment
- Inherits:
-
Object
- Object
- SimpleSelector::Segment
- Defined in:
- lib/simple_selector/segment.rb
Instance Attribute Summary collapse
-
#class_names ⇒ Object
readonly
Returns the value of attribute class_names.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#tag_name ⇒ Object
readonly
Returns the value of attribute tag_name.
Instance Method Summary collapse
-
#initialize(string) ⇒ Segment
constructor
A new instance of Segment.
- #inspect ⇒ Object
- #match?(tag) ⇒ Boolean
- #specificity ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(string) ⇒ Segment
Returns a new instance of Segment.
4 5 6 7 8 |
# File 'lib/simple_selector/segment.rb', line 4 def initialize(string) @tag_name = string.lstrip.slice(/^\w+/) @id = string.slice(/(?<=\#)\w+/) @class_names = string.scan(/(?<=\.)\w+/) end |
Instance Attribute Details
#class_names ⇒ Object (readonly)
Returns the value of attribute class_names.
10 11 12 |
# File 'lib/simple_selector/segment.rb', line 10 def class_names @class_names end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
10 11 12 |
# File 'lib/simple_selector/segment.rb', line 10 def id @id end |
#tag_name ⇒ Object (readonly)
Returns the value of attribute tag_name.
10 11 12 |
# File 'lib/simple_selector/segment.rb', line 10 def tag_name @tag_name end |
Instance Method Details
#inspect ⇒ Object
34 35 36 |
# File 'lib/simple_selector/segment.rb', line 34 def inspect "#<#{self.class} #{to_s.inspect}>" end |
#match?(tag) ⇒ Boolean
21 22 23 24 25 26 |
# File 'lib/simple_selector/segment.rb', line 21 def match?(tag) return false if tag_name && tag_name != tag.name return false if id && id != tag.id return false if (class_names - tag.class_names).any? true end |
#specificity ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/simple_selector/segment.rb', line 12 def specificity @specificity ||= Specificity.new( 0, id ? 1 : 0, class_names.count, tag_name ? 1 : 0 ) end |
#to_s ⇒ Object
28 29 30 31 32 |
# File 'lib/simple_selector/segment.rb', line 28 def to_s "#{tag_name}" + (id ? "##{id}" : "") + class_names.map { |class_name| ".#{class_name}" }.join end |