Class: AbstractInterface::HamlBuilder

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, hash = OpenObject.new) ⇒ HamlBuilder

Returns a new instance of HamlBuilder.



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

def initialize template, hash = OpenObject.new
  @template = template
  @hash, @array = hash, []
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/abstract_interface/haml_builder.rb', line 8

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

Class Method Details

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



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
54
# File 'lib/abstract_interface/haml_builder.rb', line 28

def self.get_input template, value, &block
  value = value.is_a?(Hash) ? value.to_openobject : value
  
  block_value = if block 
    if block.arity <= 0
      template.should_not! :be_nil
      template.capture &block
    else
      b = 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 = 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/abstract_interface/haml_builder.rb', line 13

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

#get_valueObject

def add_item content, opt = {}, &block

opt[:content] = content
opt[:content] ||= @template.capture &block if block
add opt

end



24
25
26
# File 'lib/abstract_interface/haml_builder.rb', line 24

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