Class: Rad::Face::HamlBuilder

Inherits:
BasicObject
Defined in:
lib/face/haml_builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, hash = {}) ⇒ HamlBuilder

Returns a new instance of HamlBuilder.



2
3
4
5
6
# File 'lib/face/haml_builder.rb', line 2

def initialize template, hash = {}
  @template = template
  @hash = ::Rad::Face::HtmlOpenObject.initialize_from(hash)
  @array = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, value = nil, &block) ⇒ Object



8
9
10
11
# File 'lib/face/haml_builder.rb', line 8

def method_missing m, value = nil, &block      
  @hash[m] = ::Rad::Face::HamlBuilder.get_input @template, value, &block
  nil
end

Class Method Details

.get_input(template, value, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/face/haml_builder.rb', line 22

def self.get_input template, value, &block
  value = ::Rad::Face::HtmlOpenObject.initialize_from(value) if value.is_a? ::Hash

  block_value = if block 
    if block.arity <= 0
      template.must_be.defined
      template.capture &block
    else
      b = ::Rad::Face::HamlBuilder.new template
      block.call b
      b.get_value
    end
  else
    nil
  end

  if value and block_value
    if block_value.is_a? ::Hash
      value.merge! block_value
    else
      raise "Invalid usage!" if value.include? :content
      value.content = block_value
    end
  end

  value || block_value
end

Instance Method Details

#add(value = nil, &block) ⇒ Object



13
14
15
16
# File 'lib/face/haml_builder.rb', line 13

def add value = nil, &block      
  @array << ::Rad::Face::HamlBuilder.get_input(@template, value, &block)
  nil
end

#get_valueObject



18
19
20
# File 'lib/face/haml_builder.rb', line 18

def get_value
  !@array.empty? ? @array : @hash
end