Class: Paggio::CSS

Inherits:
BasicObject
Defined in:
lib/paggio/css.rb,
lib/paggio/css/unit.rb,
lib/paggio/css/color.rb,
lib/paggio/css/definition.rb

Defined Under Namespace

Classes: Color, Definition, Rule, Unit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ CSS

Returns a new instance of CSS.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/paggio/css.rb', line 40

def initialize(&block)
  ::Kernel.raise ::ArgumentError, 'no block given' unless block

  @selector = []
  @current  = []
  @rules    = []

  if block.arity == 0
    instance_exec(&block)
  else
    block.call(self)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

this is needed because the methods inside the rule blocks are actually called on the CSS object



74
75
76
# File 'lib/paggio/css.rb', line 74

def method_missing(name, *args, &block)
  @current.last.definition.__send__(name, *args, &block)
end

Instance Attribute Details

#rulesObject (readonly)

Returns the value of attribute rules.



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

def rules
  @rules
end

Class Method Details

.selector(list) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/paggio/css.rb', line 20

def self.selector(list)
  result = ''

  list.each {|part|
    if part.start_with?('&')
      result += part[1 .. -1]
    else
      result += " " + part
    end
  }

  if result[0] == " "
    result[1 .. -1]
  else
    result
  end
end

Instance Method Details

#rule(*names, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/paggio/css.rb', line 54

def rule(*names, &block)
  return unless block

  if names.any? { |n| n.include? ',' }
    ::Kernel.raise ::ArgumentError, 'selectors cannot contain commas'
  end

  names.each {|name|
    @selector << name
    @current  << Rule.new(CSS.selector(@selector), Definition.new)

    block.call(self)

    @selector.pop
    @rules << @current.pop
  }
end