Skip to content

Instantly share code, notes, and snippets.

@kwando
Created February 7, 2014 12:46
Show Gist options
  • Save kwando/8862072 to your computer and use it in GitHub Desktop.
Save kwando/8862072 to your computer and use it in GitHub Desktop.
class Element
def initialize(tag_name)
@tag_name = tag_name
@classes = []
end
def tag_name
@tag_name
end
def add_class(css_class)
@classes << css_class
self
end
def remove_class(css_class)
@classes.remove(css_class)
self
end
def has_class?(css_class)
@classes.include?(css_class)
end
def classes
@classes
end
end
# Selector base class
class Selector
def matches?(element)
false
end
end
# # # # # # # # # # # # # # # # # # # #
# Example selectors
# # # # # # # # # # # # # # # # # # # #
class TagSelector
def initialize(tag_name)
@tag_name = tag_name
end
def matches?(element)
element.tag_name == @tag_name
end
end
class ClassSelector
def initialize(css_class)
@css_class = css_class
end
def matches?(element)
element.has_class?(@css_class)
end
end
class CombinedSelector
def initialize
@selectors = []
end
def add_selector(selector)
@selectors.add(selector)
end
def matches?
@selectors.all?{ |selector| selector.matches?(element) }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment