Class: Markaby::CssProxy

Inherits:
Object show all
Defined in:
lib/gems/markaby-0.5/lib/markaby/cssproxy.rb

Overview

Class used by Markaby::Builder to store element options. Methods called against the CssProxy object are added as element classes or IDs.

See the README for examples.

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &blk) ⇒ CssProxy

Creates a CssProxy object. The opts and block passed in are stored until the element is created by Builder.tag!



10
11
12
13
# File 'lib/gems/markaby-0.5/lib/markaby/cssproxy.rb', line 10

def initialize(opts = {}, &blk)
  @opts = opts
  @blk = blk
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id_or_class, *args, &blk) ⇒ Object

Adds attributes to an element. Bang methods set the :id attribute. Other methods add to the :class attribute. If a block is supplied, it is executed with a merged hash (@opts + args).



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gems/markaby-0.5/lib/markaby/cssproxy.rb', line 27

def method_missing(id_or_class, *args, &blk)
  idc = id_or_class.to_s
  case idc
  when "pass"
  when /!$/
    @opts[:id] = $`
  else 
    @opts[:class] = "#{@opts[:class]} #{idc}".strip
  end
  if args.empty? and blk.nil?
    self
  else
    if args.last.respond_to? :to_hash
      @opts.merge!(args.pop.to_hash)
    end
    args.push @opts
    @blk.call(args, blk)
  end
end

Instance Method Details

#merge!(opts) ⇒ Object

Adds attributes to an element, for internal use only. For example, if you want to write a wrapper which sets a bunch of default attributes for a certain tag. Like the default ‘img’ method included with Markaby automatically sets an empty alt attribute.



19
20
21
22
# File 'lib/gems/markaby-0.5/lib/markaby/cssproxy.rb', line 19

def merge!(opts)
  @opts.merge! opts
  self
end

#to_strObject Also known as: to_s



47
48
49
# File 'lib/gems/markaby-0.5/lib/markaby/cssproxy.rb', line 47

def to_str
  @blk.call([[@opts]]).to_s
end