Class: SimpleSelector::Segment

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_selector/segment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_namesObject (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

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/simple_selector/segment.rb', line 10

def id
  @id
end

#tag_nameObject (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

#inspectObject



34
35
36
# File 'lib/simple_selector/segment.rb', line 34

def inspect
  "#<#{self.class} #{to_s.inspect}>"
end

#match?(tag) ⇒ Boolean

Returns:

  • (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

#specificityObject



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_sObject



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