Class: Paggio::CSS

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

Defined Under Namespace

Classes: Animation, Color, Definition, Font, Rule, Unit

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(defer: false, &block) ⇒ CSS

Returns a new instance of CSS.



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

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

  @selector   = []
  @current    = []
  @rules      = []
  @fonts      = []
  @animations = []
  
  @block      = block

  build! unless defer
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object

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



114
115
116
# File 'lib/paggio/css.rb', line 114

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

Instance Attribute Details

#animationsObject (readonly)

Returns the value of attribute animations.



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

def animations
  @animations
end

#fontsObject (readonly)

Returns the value of attribute fonts.



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

def fonts
  @fonts
end

#media(query, *args, &block) ⇒ Object (readonly)

Returns the value of attribute media.



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

def media
  @media
end

#rulesObject (readonly)

Returns the value of attribute rules.



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

def rules
  @rules
end

Class Method Details

.selector(list) ⇒ Object



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

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

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



102
103
104
105
106
107
108
109
110
# File 'lib/paggio/css.rb', line 102

def animation(name, *args, &block)
  if block
    @current << Animation.new(name)
    block.call
    @animations << @current.pop
  else
    method_missing(:animation, name, *args)
  end
end

#build!(force_call: false) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/paggio/css.rb', line 55

def build!(force_call: false)
  if !force_call && @block.arity == 0
    instance_exec(&@block)
  else
    @block.call(self)
  end
  @block = nil
end

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



92
93
94
95
96
97
98
99
100
# File 'lib/paggio/css.rb', line 92

def font(name, *args, &block)
  if block
    @current << Font.new(name)
    block.call
    @fonts << @current.pop
  else
    method_missing(:font, name, *args)
  end
end

#rule(*names, &block) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/paggio/css.rb', line 64

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), @media)

    block.call

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