Class: Markaby::CssProxy

Inherits:
Object
  • Object
show all
Defined in:
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(builder, stream, sym) ⇒ CssProxy

Returns a new instance of CssProxy.


7
8
9
10
11
12
13
14
15
16
# File 'lib/markaby/cssproxy.rb', line 7

def initialize(builder, stream, sym)
  @builder = builder
  @stream  = stream
  @sym     = sym
  @attrs   = {}

  @original_stream_length = @stream.length

  @builder.tag! sym
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(id_or_class, *args, &block) ⇒ Object (private)

Adds attributes to an element. Bang methods set the :id attribute. Other methods add to the :class attribute.


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/markaby/cssproxy.rb', line 26

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)

  while @stream.length > @original_stream_length
    @stream.pop
  end

  if block
    @builder.tag! @sym, *args, &block
  else
    @builder.tag! @sym, *args
  end

  self
end

Instance Method Details

#respond_to?(sym, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)

18
19
20
# File 'lib/markaby/cssproxy.rb', line 18

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