Class: Wunderbar::CssProxy

Inherits:
BasicObject
Defined in:
lib/wunderbar/cssproxy.rb

Overview

Class inspired by Markaby to store element options. Methods called against the CssProxy object are added as element classes or IDs.

See the README for examples.

Instance Method Summary collapse

Constructor Details

#initialize(builder, node) ⇒ CssProxy

Returns a new instance of CssProxy.



7
8
9
10
# File 'lib/wunderbar/cssproxy.rb', line 7

def initialize(builder, node)
  @builder = builder
  @node = node
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id_or_class, *args, &block) ⇒ Object (private)

Adds attributes to an element. Bang methods set the :id attribute. Other methods add to the :class attribute.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/wunderbar/cssproxy.rb', line 20

def method_missing(id_or_class, *args, &block)
  empty = args.empty?
  attrs = @node.attrs
  id_or_class = id_or_class.to_s.gsub('_', '-')

  if id_or_class =~ /(.*)!$/
    attrs[:id] = $1
  elsif attrs[:class]
    attrs[:class] = "#{attrs[:class]} #{id_or_class}"
  else
    attrs[:class] = id_or_class
  end

  iterable = block && block.arity != 0
  if args.last.respond_to? :to_hash and not iterable
    hash = args.pop.to_hash 
    if attrs[:class] and hash[:class]
      hash[:class] = "#{attrs[:class]} #{hash[:class]}"
   end
    attrs.merge! hash
  end
  args.push(attrs)

  @node.parent.children.delete(@node)

  if empty and not block
    proxy = @builder.proxiable_tag! @node.name, *args
    class << proxy.node?; include SpacedNode; end  if SpacedNode  === @node
    class << proxy.node?; include CompactNode; end if CompactNode === @node
    proxy
  else
    name = @node.name.to_s.gsub('-', '_')
    if CompactNode === @node and @node.name != :pre
      if SpacedNode === @node
        @builder.__send__ "_#{name}_!", *args, &block
      else
        @builder.__send__ "_#{name}!", *args, &block
      end
    elsif SpacedNode === @node
      @builder.__send__ "_#{name}_", *args, &block
    else
      @builder.__send__ "_#{name}", *args, &block
    end
  end
end

Instance Method Details

#node?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/wunderbar/cssproxy.rb', line 12

def node?
  @node
end