Sha256: 039bb5481edf656f2ecab80dd7b30f9033a4a5f13047b08b7f8f7b2000c0ca65

Contents?: true

Size: 1.83 KB

Versions: 10

Compression:

Stored size: 1.83 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

    def 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
      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

      if args.last.respond_to? :to_hash
        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
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
wunderbar-0.22.4 lib/wunderbar/cssproxy.rb
wunderbar-0.22.3 lib/wunderbar/cssproxy.rb
wunderbar-0.22.2 lib/wunderbar/cssproxy.rb
wunderbar-0.22.1 lib/wunderbar/cssproxy.rb
wunderbar-0.22.0 lib/wunderbar/cssproxy.rb
wunderbar-0.21.4 lib/wunderbar/cssproxy.rb
wunderbar-0.21.3 lib/wunderbar/cssproxy.rb
wunderbar-0.21.2 lib/wunderbar/cssproxy.rb
wunderbar-0.21.1 lib/wunderbar/cssproxy.rb
wunderbar-0.21.0 lib/wunderbar/cssproxy.rb