Class: CSS::Rule

Inherits:
Object
  • Object
show all
Includes:
Colors, Normalize
Defined in:
lib/css/rule.rb

Constant Summary

Constants included from Colors

Colors::NAMES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Normalize

#normalize_property_name

Constructor Details

#initialize(selector, rule_text) ⇒ Rule

Returns a new instance of Rule.



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

def initialize(selector, rule_text)
  @selector = selector
  @properties = ::Set.new
  @rules = {}

  parse_rules(@properties, @rules, rule_text)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/css/rule.rb', line 46

def method_missing(method_name, *args)
  property_name = normalize_property_name(method_name)
  if property_name =~ /-/
    property_name_parts = property_name.split('-')
    pname = property_name_parts.shift
    property = nil
    while property_name_parts.size > 0
      property = get(pname)
      break unless property.nil?
      pname = [pname, property_name_parts.shift].join('-')
    end
    property[property_name_parts.shift]
  else
    get(property_name) || super
  end
end

Instance Attribute Details

#propertiesObject (readonly)

Returns the value of attribute properties.



9
10
11
# File 'lib/css/rule.rb', line 9

def properties
  @properties
end

#selectorObject (readonly)

Returns the value of attribute selector.



9
10
11
# File 'lib/css/rule.rb', line 9

def selector
  @selector
end

Instance Method Details

#<<(rule) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/css/rule.rb', line 19

def <<(rule)
  rule.properties.each do |property|
    if @rules[property]
      @rules[property] << rule[property]
    else
      @properties << property
      @rules[property] = rule[property]
    end
  end
end

#[](property_name) ⇒ Object



34
35
36
# File 'lib/css/rule.rb', line 34

def [](property_name)
  get property_name
end

#get(property_name) ⇒ Object



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

def get(property_name)
  @rules[normalize_property_name(property_name)]
end

#to_sObject



38
39
40
# File 'lib/css/rule.rb', line 38

def to_s
  properties.map { |prop| get(prop).to_style }.join ';'
end

#to_styleObject



42
43
44
# File 'lib/css/rule.rb', line 42

def to_style
  "#{selector}{#{to_s}}"
end