Sha256: b84f2b8abb1fa95f97027af76f58304fce53a263c2a3bfe18b342db6a3af243b

Contents?: true

Size: 1.7 KB

Versions: 33

Compression:

Stored size: 1.7 KB

Contents

module Wunderbar
  # 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.
  class CssProxy
    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

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

  private

    # Adds attributes to an element.  Bang methods set the :id attribute.
    # Other methods add to the :class attribute.
    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
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
wunderbar-0.16.3 lib/wunderbar/cssproxy.rb
wunderbar-0.16.2 lib/wunderbar/cssproxy.rb
wunderbar-0.16.1 lib/wunderbar/cssproxy.rb
wunderbar-0.16.0 lib/wunderbar/cssproxy.rb
wunderbar-0.15.0 lib/wunderbar/cssproxy.rb
wunderbar-0.14.7 lib/wunderbar/cssproxy.rb
wunderbar-0.14.6 lib/wunderbar/cssproxy.rb
wunderbar-0.14.5 lib/wunderbar/cssproxy.rb
wunderbar-0.14.4 lib/wunderbar/cssproxy.rb
wunderbar-0.14.3 lib/wunderbar/cssproxy.rb
wunderbar-0.14.2 lib/wunderbar/cssproxy.rb
wunderbar-0.14.1 lib/wunderbar/cssproxy.rb
wunderbar-0.14.0 lib/wunderbar/cssproxy.rb
wunderbar-0.13.0 lib/wunderbar/cssproxy.rb
wunderbar-0.12.3 lib/wunderbar/cssproxy.rb
wunderbar-0.12.1 lib/wunderbar/cssproxy.rb
wunderbar-0.12.0 lib/wunderbar/cssproxy.rb
wunderbar-0.11.1 lib/wunderbar/cssproxy.rb
wunderbar-0.11.0 lib/wunderbar/cssproxy.rb
wunderbar-0.10.9 lib/wunderbar/cssproxy.rb