Class: HTTParrot::Widget

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/httparrot/widget.rb

Instance Method Summary collapse

Constructor Details

#initialize(defaults = {}) ⇒ Widget

Returns a new instance of Widget.



11
12
13
14
15
# File 'lib/httparrot/widget.rb', line 11

def initialize(defaults = {})
  @parent_overwrite = false 
  @headers = {}
  super(defaults)
end

Instance Method Details

#headerObject



17
18
19
# File 'lib/httparrot/widget.rb', line 17

def header
  @headers
end

#parent(parental_class, defaults = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/httparrot/widget.rb', line 21

def parent(parental_class, defaults={})
  case
  when parental_class.is_a?(Symbol) then
    parental_class = HTTParrot::ResponseFactory.build(parental_class, defaults)
  when !parental_class.respond_to?(:to_hash) then
    raise "Parent must be a symbol or respond_to #to_hash"
  end 

  merge_parent(parental_class)
end

#parent!(parental_class, defaults = {}) ⇒ Object



32
33
34
35
36
# File 'lib/httparrot/widget.rb', line 32

def parent!(parental_class, defaults = {})
  @parent_overwrite = true
  parent(parental_class, defaults)
  @parent_overwrite = false
end

#rack_response(response_code = 200) ⇒ Object Also known as: to_rack, to_rack_response



38
39
40
41
# File 'lib/httparrot/widget.rb', line 38

def rack_response(response_code = 200)
  rendered_response = self.to_s
  return [response_code, @headers.merge({"Content-Length" => rendered_response.size.to_s}), [rendered_response]]
end

#to_hashObject



45
46
47
# File 'lib/httparrot/widget.rb', line 45

def to_hash
  self.marshal_dump
end

#to_sObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/httparrot/widget.rb', line 49

def to_s
  set_template_file

  if @table.has_key?(:template_file) && !self.template_file.nil?
    @headers = {}
    file_string = File.read(self.template_file)
    current_template = ERB.new(file_string, nil, "<>")
    return current_template.result(binding)
  else
    warn_no_template
    return self.inspect
  end
end