Sha256: 768fde7859e5ed996a133ce37834bbea8810d0d0bf27f5801f8d9a62a07b262f

Contents?: true

Size: 1.13 KB

Versions: 3

Compression:

Stored size: 1.13 KB

Contents

module Wunderbar
  # 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.
  class CssProxy < BasicObject
    def initialize(builder, node)
      @builder = builder
      @node = node
    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)
      empty = args.empty?
      attrs = @node.attrs

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

      attrs.merge! args.pop.to_hash if args.last.respond_to? :to_hash
      args.push(attrs)

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

      if empty and not block
        @builder.proxiable_tag! @node.name, *args
      elsif SpacedNode === @node
        @builder.__send__ "_#{@node.name}_", *args, &block
      else
        @builder.__send__ "_#{@node.name}", *args, &block
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
wunderbar-0.18.2 lib/wunderbar/cssproxy.rb
wunderbar-0.18.1 lib/wunderbar/cssproxy.rb
wunderbar-0.18.0 lib/wunderbar/cssproxy.rb