Class: Wunderbar::CssProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/wunderbar/cssproxy.rb

Overview

Class “lifted” from Markaby to store element options. Methods called against the CssProxy object are added as element classes or IDs.

Modified to accept args for empty, non-void elements, and to capture and restore indentation state.

See the README for examples.

Instance Method Summary collapse

Constructor Details

#initialize(builder, stream, sym, args) ⇒ CssProxy

Returns a new instance of CssProxy.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/wunderbar/cssproxy.rb', line 10

def initialize(builder, stream, sym, args)
  @builder = builder
  @indent  = builder.indentation_state!
  @stream  = stream
  @sym     = sym
  @args    = args
  @attrs   = {}

  @original_stream_length = @stream.length

  @builder.tag! sym, *args
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.



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
65
# File 'lib/wunderbar/cssproxy.rb', line 31

def method_missing(id_or_class, *args, &block)
  if id_or_class.to_s =~ /(.*)!$/
    @attrs[:id] = $1
  else
    id = id_or_class
    @attrs[:class] = @attrs[:class] ? "#{@attrs[:class]} #{id}".strip : id
  end

  unless args.empty?
    if args.last.respond_to? :to_hash
      @attrs.merge! args.pop.to_hash
    end
  end

  args.push(@attrs)
  args = @args + args unless block or String === args.first

  while @stream.length > @original_stream_length
    @stream.pop
  end

  begin
    indent = @builder.indentation_state! @indent

    if block
      @builder.tag! @sym, *args, &block
    else
      @builder.tag! @sym, *args
    end
  ensure
    @builder.indentation_state! indent
  end

  self
end

Instance Method Details

#respond_to?(sym, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/wunderbar/cssproxy.rb', line 23

def respond_to?(sym, include_private = false)
  include_private || !private_methods.map { |m| m.to_sym }.include?(sym.to_sym) ? true : false
end