| 8 | | # Creates a CssProxy object. The +opts+ and +block+ passed in are |
| 9 | | # stored until the element is created by Builder.tag! |
| 10 | | def initialize(opts = {}, &blk) |
| 11 | | @opts = opts |
| 12 | | @blk = blk |
| | 8 | # Creates a CssProxy object. |
| | 9 | def initialize(builder, stream, sym) |
| | 10 | @builder, @stream, @sym, @attrs = builder, stream, sym, {} |
| | 11 | |
| | 12 | @original_stream_length = @stream.length |
| | 13 | |
| | 14 | @builder.tag! @sym |
| 14 | | |
| 15 | | # Adds attributes to an element, for internal use only. For example, if you |
| 16 | | # want to write a wrapper which sets a bunch of default attributes for a certain |
| 17 | | # tag. Like the default `img' method included with Markaby automatically sets an |
| 18 | | # empty alt attribute. |
| 19 | | def merge!(opts) |
| 20 | | @opts.merge! opts |
| 21 | | self |
| | 16 | |
| | 17 | # Adds attributes to an element. Bang methods set the :id attribute. |
| | 18 | # Other methods add to the :class attribute. |
| | 19 | def method_missing(id_or_class, *args, &block) |
| | 20 | if (idc = id_or_class.to_s) =~ /!$/ |
| | 21 | @attrs[:id] = $` |
| | 22 | else |
| | 23 | @attrs[:class] = @attrs[:class].nil? ? idc : "#{@attrs[:class]} #{idc}".strip |
| | 24 | end |
| | 25 | |
| | 26 | unless args.empty? |
| | 27 | if args.last.respond_to? :to_hash |
| | 28 | @attrs.merge! args.pop.to_hash |
| | 29 | end |
| | 30 | end |
| | 31 | |
| | 32 | args.push(@attrs) |
| | 33 | |
| | 34 | while @stream.length > @original_stream_length |
| | 35 | @stream.pop |
| | 36 | end |
| | 37 | |
| | 38 | if block |
| | 39 | @builder.tag! @sym, *args, &block |
| | 40 | else |
| | 41 | @builder.tag! @sym, *args |
| | 42 | end |
| | 43 | |
| | 44 | return self |
| 23 | | |
| 24 | | # Adds attributes to an element. Bang methods set the :id attribute. |
| 25 | | # Other methods add to the :class attribute. If a block is supplied, |
| 26 | | # it is executed with a merged hash (@opts + args). |
| 27 | | def method_missing(id_or_class, *args, &blk) |
| 28 | | idc = id_or_class.to_s |
| 29 | | case idc |
| 30 | | when "pass" |
| 31 | | when /!$/ |
| 32 | | @opts[:id] = $` |
| 33 | | else |
| 34 | | @opts[:class] = "#{@opts[:class]} #{idc}".strip |
| 35 | | end |
| 36 | | if args.empty? and blk.nil? |
| 37 | | self |
| 38 | | else |
| 39 | | if args.last.respond_to? :to_hash |
| 40 | | @opts.merge!(args.pop.to_hash) |
| 41 | | end |
| 42 | | args.push @opts |
| 43 | | @blk.call(args, blk) |
| 44 | | end |
| 45 | | end |
| 46 | | |
| 47 | | def to_str |
| 48 | | @blk.call([[@opts]]).to_s |
| 49 | | end |
| 50 | | alias_method :to_s, :to_str |