Class: Paggio::CSS

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

Defined Under Namespace

Classes: Definition, Rule, Unit

Constant Summary collapse

Format =
::Paggio::Format

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ CSS

Returns a new instance of CSS.



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

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



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

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.



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

def rules
  @rules
end

Class Method Details

.selector(list) ⇒ Object



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

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

#format(io = ::StringIO.new, options = { indent: 0 }) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/paggio/format.rb', line 96

def format(io = ::StringIO.new, options = { indent: 0 })
  rules.reverse.each {|rule|
    next if rule.definition.empty?

    Format.puts io, "#{rule.selector} {", options[:indent]

    rule.definition.each {|style|
      Format.puts io, "#{style.name}: #{style.value}#{' !important' if style.important?};",
        options[:indent] + 1
    }

    Format.puts io, "}", options[:indent]
    io.puts
  }

  io.seek(-1, ::IO::SEEK_CUR)

  io
end

#rule(*names, &block) ⇒ Object



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

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