Class: CSSNative::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/css-native/rule.rb,
lib/css-native/rule/constants.rb,
lib/css-native/rule/stylesheet.rb

Defined Under Namespace

Classes: Stylesheet

Instance Method Summary collapse

Constructor Details

#initialize(parent, name = "", previous: nil) ⇒ Rule

Returns a new instance of Rule.



5
6
7
8
9
10
# File 'lib/css-native/rule.rb', line 5

def initialize(parent, name = "", previous: nil)
  @previous = previous
  @parent = parent
  @selector = name.to_s
  @stylesheet = Stylesheet.new(self)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/css-native/rule.rb', line 113

def method_missing(m, *args, &block)
  if COMBINATORS.key?(m)
    combinator(m)
  elsif PSEUDO_CLASSES.key?(m)
    pseudo_class(m, *args, &block)
  elsif PSEUDO_ELEMENTS.key?(m)
    pseudo_element(m, *args, &block)
  else
    super(m, *args, &block)
  end
end

Instance Method Details

#all(&block) ⇒ Object



54
55
56
57
58
# File 'lib/css-native/rule.rb', line 54

def all(&block)
  selector_error("*") if previous_selector?
  append_selector("*", :all)
  chain(&block)
end

#combinator(c) ⇒ Object

Combinators



78
79
80
81
82
83
# File 'lib/css-native/rule.rb', line 78

def combinator(c)
  m = c.to_sym
  selector_error(COMBINATORS[m].strip) if previous_combinator?
  append_selector(COMBINATORS[m], :combinator)
  self
end

#join(rule = nil, &block) ⇒ Object

Grouping selectors

Raises:



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/css-native/rule.rb', line 61

def join(rule = nil, &block)
  raise JoinError unless rule.kind_of?(Rule) || rule.nil?
  selector_error(",") if previous_combinator?

  if rule.kind_of? Rule
    selector = rule.instance_variable_get(:@selector)
    previous = rule.instance_variable_get(:@previous)

    append_selector("," + selector, previous)
    previous_combinator? ? self : chain(&block)
  else
    append_selector(",", :join)
    self
  end
end

#pseudo_class(name, *args, &block) ⇒ Object

Raises:



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/css-native/rule.rb', line 85

def pseudo_class(name, *args, &block)
  pc = name.to_s.gsub("_", "-")
  m = name.to_s.gsub("-", "_").to_sym
  raise PseudoClassError.new(method: pc) unless PSEUDO_CLASSES.key?(m)

  unless args.all? {|arg| valid_arg?(PSEUDO_CLASSES[m], arg.to_s)}
    raise PseudoClassError.new(argument: arg, method: pc) 
  end
  
  selector = ":#{pc}" + (args.empty? ? "" : "(#{args.join(" ")})")
  append_selector(selector, :pseudo_class)
  chain(&block)
end

#pseudo_element(name, *args, &block) ⇒ Object

Raises:



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/css-native/rule.rb', line 99

def pseudo_element(name, *args, &block)
  pe = name.to_s.gsub("_", "-")
  m = name.to_s.gsub("-", "_").to_sym
  raise PseudoElementError.new(method: pe) unless PSEUDO_ELEMENTS.key?(m)
 
  unless args.all? {|arg| valid_arg?(PSEUDO_ELEMENTS[m], arg.to_s)}
    raise PseudoElementError.new(argument: arg, method: pe)
  end

  selector = "::#{pe}" + (args.empty? ? "" : "(#{args.join(" ")})")
  append_selector(selector, :pseudo_element)
  chain(&block)
end

#respond_to_missing?(m, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
130
# File 'lib/css-native/rule.rb', line 125

def respond_to_missing?(m, include_all = false)
  COMBINATORS.key?(m) ||
  PSEUDO_CLASSES.key?(m) ||
  PSEUDO_ELEMENTS.key?(m) ||
  super(m, include_all)
end

#to_sObject



132
133
134
# File 'lib/css-native/rule.rb', line 132

def to_s
  "#{@selector} #{@stylesheet}"
end

#with(name, *args, type: :element, &block) ⇒ Object Also known as: select



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/css-native/rule.rb', line 34

def with(name, *args, type: :element, &block)
  case type
  when :element
    if name == :all
      all(&block)
    else
      with_element(name, &block)
    end
  when :class
    with_class(name, &block)
  when :id
    with_id(name, &block)
  when :attribute
    with_attribute(name, *args, &block)
  else
    raise RuleError.new("undefined rule type '#{type}' for css selector")
  end
end

#with_attribute(name, op = :none, value = nil, case_sensitive: true, &block) ⇒ Object



29
30
31
32
# File 'lib/css-native/rule.rb', line 29

def with_attribute(name, op = :none, value = nil, case_sensitive: true, &block)
  append_selector(format_attribute(name, op, value, case_sensitive: case_sensitive), :attribute)
  chain(&block)
end

#with_class(name, &block) ⇒ Object



19
20
21
22
# File 'lib/css-native/rule.rb', line 19

def with_class(name, &block)
  append_selector(format_class(name), :class)
  chain(&block)
end

#with_element(name, &block) ⇒ Object

basic selectors



13
14
15
16
17
# File 'lib/css-native/rule.rb', line 13

def with_element(name, &block)
  selector_error(name) if previous_selector?
  append_selector(format_element(name), :element)
  chain(&block)
end

#with_id(name, &block) ⇒ Object



24
25
26
27
# File 'lib/css-native/rule.rb', line 24

def with_id(name, &block)
  append_selector(format_id(name), :id)
  chain(&block)
end